Search in sources :

Example 6 with SchemaOrRef

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);
}
Also used : SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 7 with SchemaOrRef

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

Example 8 with SchemaOrRef

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();
    }
}
Also used : AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvroType(com.linkedin.avroutil1.model.AvroType) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 9 with SchemaOrRef

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;
    }
}
Also used : AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroType(com.linkedin.avroutil1.model.AvroType) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField)

Example 10 with SchemaOrRef

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

Aggregations

SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)12 AvroType (com.linkedin.avroutil1.model.AvroType)5 CodeLocation (com.linkedin.avroutil1.model.CodeLocation)5 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)5 JsonValueExt (com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)5 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)4 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)4 AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)4 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)3 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)2 JsonPropertiesContainer (com.linkedin.avroutil1.model.JsonPropertiesContainer)2 ArrayList (java.util.ArrayList)2 JsonValue (javax.json.JsonValue)2 Test (org.testng.annotations.Test)2 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)1 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)1 AvroLiteral (com.linkedin.avroutil1.model.AvroLiteral)1 AvroLogicalType (com.linkedin.avroutil1.model.AvroLogicalType)1