arjun4084346 commented on a change in pull request #2800: [GOBBLIN-933] add
support for array of unions in json schema_new
URL: https://github.com/apache/incubator-gobblin/pull/2800#discussion_r344320991
##########
File path:
gobblin-core/src/main/java/org/apache/gobblin/converter/avro/JsonElementConversionWithAvroSchemaFactory.java
##########
@@ -210,4 +216,70 @@ public Schema schema() {
return this.schema;
}
}
-}
+
+ public static class UnionConverter extends ComplexConverter {
+ private final List<Schema> schemas;
+ private final List<JsonElementConverter> converters;
+ private final Schema schemaNode;
+
+ public UnionConverter(String fieldName, boolean nullable, String
sourceType, Schema schemaNode,
+ WorkUnitState state, List<String> ignoreFields) throws
UnsupportedDateTypeException {
+ super(fieldName, nullable, sourceType);
+ this.schemas = schemaNode.getTypes();
+ converters = new ArrayList<>();
+ for(Schema schema: schemas) {
+ converters.add(getConvertor(fieldName, schema.getType().getName(),
schemaNode, state, isNullable(), ignoreFields));
+ }
+ this.schemaNode = schemaNode;
+ }
+
+ @Override
+ Object convertField(JsonElement value) {
+ for(JsonElementConverter converter: converters)
+ {
+ try {
+ Object o = converter.convert(value);
+ return o;
+ } catch (Exception e){}
+ }
+ throw new RuntimeException(StringFormatter.format("Cannot convert %s to
avro using schema %s", value.getAsString(), schemaNode.toString()).toString());
+ }
+
+ @Override
+ public Schema.Type getTargetType() {
+ if(schemas.size() == 2 && isNullable())
+ {
+ if(schemas.get(0).getType() == Schema.Type.NULL) {
+ return schemas.get(1).getType();
+ } else {
+ return schemas.get(0).getType();
+ }
+ }
+ return Schema.Type.UNION;
+ }
+
+ @Override
+ public Schema schema() {
+ if(schemas.size() == 2 && isNullable())
Review comment:
Ok
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services