Search in sources :

Example 1 with AvroUnionSchema

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);
}
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 2 with AvroUnionSchema

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();
    }
}
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 3 with AvroUnionSchema

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;
    }
}
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 4 with AvroUnionSchema

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);
}
Also used : CodeLocation(com.linkedin.avroutil1.model.CodeLocation) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) ArrayList(java.util.ArrayList) JsonValue(javax.json.JsonValue) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)

Aggregations

AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)4 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)4 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)2 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)2 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)2 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)2 AvroType (com.linkedin.avroutil1.model.AvroType)2 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)1 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)1 CodeLocation (com.linkedin.avroutil1.model.CodeLocation)1 JsonValueExt (com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)1 ArrayList (java.util.ArrayList)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 JsonValue (javax.json.JsonValue)1 Test (org.testng.annotations.Test)1