Search in sources :

Example 21 with AvscParseResult

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);
            }
        }
    }
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef)

Example 22 with AvscParseResult

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;
}
Also used : SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) JsonReaderWithLocations(com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations) JsonReaderExt(com.linkedin.avroutil1.parser.jsonpext.JsonReaderExt) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) ParseException(com.linkedin.avroutil1.parser.exceptions.ParseException) FileNotFoundException(java.io.FileNotFoundException) JsonParsingException(javax.json.stream.JsonParsingException) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) JsonParsingException(javax.json.stream.JsonParsingException)

Aggregations

Test (org.testng.annotations.Test)18 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)14 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)9 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)6 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)6 AvscParseResult (com.linkedin.avroutil1.parser.avsc.AvscParseResult)6 AvscParser (com.linkedin.avroutil1.parser.avsc.AvscParser)6 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)5 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)4 UnresolvedReferenceException (com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException)4 JavaFileObject (javax.tools.JavaFileObject)4 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)3 AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)3 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)3 JsonParseException (com.linkedin.avroutil1.parser.exceptions.JsonParseException)3 BigDecimal (java.math.BigDecimal)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3