use of com.linkedin.avroutil1.model.SchemaOrRef 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.SchemaOrRef in project avro-util by linkedin.
the class AvroParseContext method resolveReferences.
public void resolveReferences() {
sealed = true;
// build up an index of FQCNs (also find dups)
knownNamedSchemas = new HashMap<>(individualResults.size());
duplicates = new HashMap<>(1);
for (AvscParseResult singleFile : individualResults) {
Throwable error = singleFile.getParseError();
if (error != null) {
// dont touch files with outright failures
continue;
}
Map<String, AvroSchema> namedInFile = singleFile.getDefinedNamedSchemas();
namedInFile.forEach((fqcn, schema) -> {
AvscParseResult firstDefinition = knownNamedSchemas.putIfAbsent(fqcn, singleFile);
if (firstDefinition != null) {
// TODO - find dups in aliases as well ?
// this is a dup
duplicates.compute(fqcn, (k, dups) -> {
if (dups == null) {
dups = new ArrayList<>(2);
dups.add(firstDefinition);
}
dups.add(singleFile);
return dups;
});
}
});
}
// TODO - add context-level issues for dups
// resolve any unresolved references in individual file results from other files
externalReferences = new ArrayList<>();
for (AvscParseResult singleFile : individualResults) {
List<SchemaOrRef> externalRefs = singleFile.getExternalReferences();
for (SchemaOrRef ref : externalRefs) {
String simpleName = ref.getRef();
AvscParseResult simpleNameResolution = knownNamedSchemas.get(simpleName);
AvscParseResult inheritedNameResolution = null;
String inheritedName = ref.getInheritedName();
if (inheritedName != null) {
inheritedNameResolution = knownNamedSchemas.get(inheritedName);
}
// the inherited namespace).
if (inheritedNameResolution != null) {
ref.setResolvedTo(inheritedNameResolution.getDefinedNamedSchemas().get(inheritedName));
if (simpleNameResolution != null) {
String msg = "ERROR: Two different schemas found for reference " + simpleName + " with inherited name " + inheritedName + ". Only one should exist.";
singleFile.addIssue(new AvscIssue(ref.getCodeLocation(), IssueSeverity.WARNING, msg, new IllegalStateException(msg)));
}
} else if (simpleNameResolution != null) {
ref.setResolvedTo(simpleNameResolution.getDefinedNamedSchemas().get(simpleName));
} else {
// fqcn is unresolved in this context
externalReferences.add(ref);
}
}
}
}
use of com.linkedin.avroutil1.model.SchemaOrRef 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.SchemaOrRef 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.SchemaOrRef in project avro-util by linkedin.
the class AvscFileParseContext method resolveReferences.
private void resolveReferences(SchemaOrRef possiblyRef) {
if (possiblyRef.isResolved()) {
// either an already- resolved reference or an inline definition
if (possiblyRef.getDecl() != null) {
// recurse into inline definitions
resolveReferences(possiblyRef.getDecl());
}
} else {
// unresolved (and so must be a) reference
String simpleName = possiblyRef.getRef();
AvroSchema simpleNameResolution = definedNamedSchemas.get(simpleName);
AvroSchema inheritedNameResolution = null;
String inheritedName = possiblyRef.getInheritedName();
if (inheritedName != null) {
inheritedNameResolution = definedNamedSchemas.get(inheritedName);
}
if (inheritedNameResolution != null) {
possiblyRef.setResolvedTo(inheritedNameResolution);
if (simpleNameResolution != null) {
String msg = "Two different schemas found for reference " + simpleName + " with inherited name " + inheritedName + ". Only one should exist.";
AvscIssue issue = new AvscIssue(possiblyRef.getCodeLocation(), IssueSeverity.WARNING, msg, new IllegalStateException(msg));
issues.add(issue);
}
} else if (simpleNameResolution != null) {
possiblyRef.setResolvedTo(simpleNameResolution);
} else {
externalReferences.add(possiblyRef);
}
}
}
Aggregations