use of com.linkedin.avroutil1.model.AvroUnionSchema in project avro-util by linkedin.
the class AvscParserTest method testSelfReference.
@Test
public void testSelfReference() throws Exception {
String avsc = TestUtil.load("schemas/LongList.avsc");
AvscParser parser = new AvscParser();
AvscParseResult result = parser.parse(avsc);
Assert.assertNull(result.getParseError());
AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
// schema.next[1] == schema
AvroSchemaField nextField = schema.getField("next");
AvroUnionSchema union = (AvroUnionSchema) nextField.getSchema();
SchemaOrRef secondBranch = union.getTypes().get(1);
Assert.assertSame(secondBranch.getSchema(), schema);
}
use of com.linkedin.avroutil1.model.AvroUnionSchema in project avro-util by linkedin.
the class AvscSchemaWriter method writeSchema.
protected JsonValue writeSchema(AvroSchema schema, AvscWriterContext context, AvscWriterConfig config) {
AvroType type = schema.type();
JsonObjectBuilder definitionBuilder;
switch(type) {
case ENUM:
case FIXED:
case RECORD:
return writeNamedSchema((AvroNamedSchema) schema, context, config);
case ARRAY:
AvroArraySchema arraySchema = (AvroArraySchema) schema;
definitionBuilder = Json.createObjectBuilder();
definitionBuilder.add("type", "array");
definitionBuilder.add("items", writeSchema(arraySchema.getValueSchema(), context, config));
emitJsonProperties(schema, context, config, definitionBuilder);
return definitionBuilder.build();
case MAP:
AvroMapSchema mapSchema = (AvroMapSchema) schema;
definitionBuilder = Json.createObjectBuilder();
definitionBuilder.add("type", "map");
definitionBuilder.add("values", writeSchema(mapSchema.getValueSchema(), context, config));
emitJsonProperties(schema, context, config, definitionBuilder);
return definitionBuilder.build();
case UNION:
AvroUnionSchema unionSchema = (AvroUnionSchema) schema;
JsonArrayBuilder unionBuilder = Json.createArrayBuilder();
for (SchemaOrRef unionBranch : unionSchema.getTypes()) {
// will throw if unresolved ref
AvroSchema branchSchema = unionBranch.getSchema();
unionBuilder.add(writeSchema(branchSchema, context, config));
}
return unionBuilder.build();
default:
AvroPrimitiveSchema primitiveSchema = (AvroPrimitiveSchema) schema;
if (!primitiveSchema.hasProperties()) {
return Json.createValue(primitiveSchema.type().name().toLowerCase(Locale.ROOT));
}
definitionBuilder = Json.createObjectBuilder();
definitionBuilder.add("type", primitiveSchema.type().toTypeName());
emitJsonProperties(primitiveSchema, context, config, definitionBuilder);
return definitionBuilder.build();
}
}
use of com.linkedin.avroutil1.model.AvroUnionSchema in project avro-util by linkedin.
the class AvscFileParseContext method resolveReferences.
private void resolveReferences(AvroSchema schema) {
AvroType type = schema.type();
switch(type) {
case RECORD:
AvroRecordSchema recordSchema = (AvroRecordSchema) schema;
List<AvroSchemaField> fields = recordSchema.getFields();
for (AvroSchemaField field : fields) {
SchemaOrRef fieldSchema = field.getSchemaOrRef();
resolveReferences(fieldSchema);
}
break;
case UNION:
AvroUnionSchema unionSchema = (AvroUnionSchema) schema;
List<SchemaOrRef> types = unionSchema.getTypes();
for (SchemaOrRef unionType : types) {
resolveReferences(unionType);
}
break;
case ARRAY:
AvroArraySchema arraySchema = (AvroArraySchema) schema;
SchemaOrRef arrayValuesType = arraySchema.getValueSchemaOrRef();
resolveReferences(arrayValuesType);
break;
case MAP:
AvroMapSchema mapSchema = (AvroMapSchema) schema;
SchemaOrRef mapValuesType = mapSchema.getValueSchemaOrRef();
resolveReferences(mapValuesType);
break;
default:
break;
}
}
use of com.linkedin.avroutil1.model.AvroUnionSchema in project avro-util by linkedin.
the class AvscParser method parseUnionSchema.
private SchemaOrRef parseUnionSchema(JsonArrayExt arrayNode, AvscFileParseContext context, boolean topLevel) {
CodeLocation codeLocation = locationOf(context.getUri(), arrayNode);
List<SchemaOrRef> unionTypes = new ArrayList<>(arrayNode.size());
for (JsonValue jsonValue : arrayNode) {
JsonValueExt unionNode = (JsonValueExt) jsonValue;
SchemaOrRef type = parseSchemaDeclOrRef(unionNode, context, false);
unionTypes.add(type);
}
AvroUnionSchema unionSchema = new AvroUnionSchema(codeLocation);
unionSchema.setTypes(unionTypes);
context.defineSchema(unionSchema, topLevel);
return new SchemaOrRef(codeLocation, unionSchema);
}
Aggregations