use of com.linkedin.avroutil1.parser.avsc.AvscParseResult 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.parser.avsc.AvscParseResult in project avro-util by linkedin.
the class AvscParser method parse.
private AvscParseResult parse(AvscFileParseContext context, Reader reader) {
JsonReaderExt jsonReader = new JsonReaderWithLocations(reader, null);
JsonValueExt root;
AvscParseResult result = new AvscParseResult();
try {
root = jsonReader.readValue();
} catch (JsonParsingException e) {
Throwable rootCause = Util.rootCause(e);
String message = rootCause.getMessage();
if (message != null && message.startsWith("Unexpected char 47")) {
// 47 is ascii for '/' (utf-8 matches ascii on "low" characters). we are going to assume this means
// someone tried putting a //comment into an avsc source
result.recordError(new JsonParseException("comments not supported in json at" + e.getLocation(), e));
} else {
result.recordError(new JsonParseException("json parse error at " + e.getLocation(), e));
}
return result;
} catch (Exception e) {
result.recordError(new JsonParseException("unknown json parse error", e));
return result;
}
try {
SchemaOrRef schemaOrRef = parseSchemaDeclOrRef(root, context, true);
context.resolveReferences();
result.recordParseComplete(context);
} catch (Exception parseIssue) {
result.recordError(parseIssue);
}
return result;
}
Aggregations