You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2022/06/06 23:39:45 UTC
[pulsar] branch master updated: [cleanup][schema] Remove useless code of class StructSchema (#15949)
This is an automated email from the ASF dual-hosted git repository.
mmerli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 64bb9ef76b3 [cleanup][schema] Remove useless code of class StructSchema (#15949)
64bb9ef76b3 is described below
commit 64bb9ef76b38b7eb63839abac5b633de88c90426
Author: lipenghui <pe...@apache.org>
AuthorDate: Tue Jun 7 07:39:39 2022 +0800
[cleanup][schema] Remove useless code of class StructSchema (#15949)
---
.../pulsar/client/impl/schema/StructSchema.java | 61 +---------------------
1 file changed, 1 insertion(+), 60 deletions(-)
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java
index 59015793b44..3b6e75ed752 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java
@@ -19,14 +19,10 @@
package org.apache.pulsar.client.impl.schema;
import static java.nio.charset.StandardCharsets.UTF_8;
-import java.lang.reflect.Field;
+import static org.apache.pulsar.client.impl.schema.util.SchemaUtil.parseAvroSchema;
import org.apache.avro.Schema;
-import org.apache.avro.reflect.ReflectData;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.pulsar.client.api.schema.SchemaDefinition;
import org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema;
import org.apache.pulsar.common.schema.SchemaInfo;
-import org.apache.pulsar.common.schema.SchemaType;
/**
* This is a base schema implementation for Avro Based `Struct` types.
@@ -55,59 +51,4 @@ public abstract class StructSchema<T> extends AbstractStructSchema<T> {
public Schema getAvroSchema() {
return schema;
}
-
-
- protected static Schema createAvroSchema(SchemaDefinition schemaDefinition) {
- Class pojo = schemaDefinition.getPojo();
-
- if (StringUtils.isNotBlank(schemaDefinition.getJsonDef())) {
- return parseAvroSchema(schemaDefinition.getJsonDef());
- } else if (pojo != null) {
- ThreadLocal<Boolean> validateDefaults = null;
-
- try {
- Field validateDefaultsField = Schema.class.getDeclaredField("VALIDATE_DEFAULTS");
- validateDefaultsField.setAccessible(true);
- validateDefaults = (ThreadLocal<Boolean>) validateDefaultsField.get(null);
- } catch (NoSuchFieldException | IllegalAccessException e) {
- throw new RuntimeException("Cannot disable validation of default values", e);
- }
-
- final boolean savedValidateDefaults = validateDefaults.get();
-
- try {
- // Disable validation of default values for compatibility
- validateDefaults.set(false);
- return extractAvroSchema(schemaDefinition, pojo);
- } finally {
- validateDefaults.set(savedValidateDefaults);
- }
- } else {
- throw new RuntimeException("Schema definition must specify pojo class or schema json definition");
- }
- }
-
- protected static Schema extractAvroSchema(SchemaDefinition schemaDefinition, Class pojo) {
- try {
- return parseAvroSchema(pojo.getDeclaredField("SCHEMA$").get(null).toString());
- } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException ignored) {
- return schemaDefinition.getAlwaysAllowNull() ? ReflectData.AllowNull.get().getSchema(pojo)
- : ReflectData.get().getSchema(pojo);
- }
- }
-
- protected static Schema parseAvroSchema(String schemaJson) {
- final Schema.Parser parser = new Schema.Parser();
- parser.setValidateDefaults(false);
- return parser.parse(schemaJson);
- }
-
- public static <T> SchemaInfo parseSchemaInfo(SchemaDefinition<T> schemaDefinition, SchemaType schemaType) {
- return SchemaInfoImpl.builder()
- .schema(createAvroSchema(schemaDefinition).toString().getBytes(UTF_8))
- .properties(schemaDefinition.getProperties())
- .name("")
- .type(schemaType).build();
- }
-
}