Search in sources :

Example 1 with AvroMapSchema

use of com.linkedin.avroutil1.model.AvroMapSchema in project avro-util by linkedin.

the class AvscParserTest method testSimpleParse.

@Test
public void testSimpleParse() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecord.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroSchema schema = result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    Assert.assertEquals(schema.type(), AvroType.RECORD);
    AvroRecordSchema recordSchema = (AvroRecordSchema) schema;
    Assert.assertEquals(recordSchema.getFullName(), "com.acme.TestRecord");
    List<AvroSchemaField> fields = recordSchema.getFields();
    Assert.assertNotNull(fields);
    Assert.assertEquals(fields.size(), 11);
    Assert.assertEquals(fields.get(0).getPosition(), 0);
    Assert.assertEquals(fields.get(0).getName(), "booleanField");
    Assert.assertEquals(fields.get(0).getSchema().type(), AvroType.BOOLEAN);
    Assert.assertEquals(fields.get(1).getPosition(), 1);
    Assert.assertEquals(fields.get(1).getName(), "intField");
    Assert.assertEquals(fields.get(1).getSchema().type(), AvroType.INT);
    Assert.assertEquals(fields.get(2).getPosition(), 2);
    Assert.assertEquals(fields.get(2).getName(), "longField");
    Assert.assertEquals(fields.get(2).getSchema().type(), AvroType.LONG);
    Assert.assertEquals(fields.get(3).getPosition(), 3);
    Assert.assertEquals(fields.get(3).getName(), "floatField");
    Assert.assertEquals(fields.get(3).getSchema().type(), AvroType.FLOAT);
    Assert.assertEquals(fields.get(4).getPosition(), 4);
    Assert.assertEquals(fields.get(4).getName(), "doubleField");
    Assert.assertEquals(fields.get(4).getSchema().type(), AvroType.DOUBLE);
    Assert.assertEquals(fields.get(5).getPosition(), 5);
    Assert.assertEquals(fields.get(5).getName(), "bytesField");
    Assert.assertEquals(fields.get(5).getSchema().type(), AvroType.BYTES);
    Assert.assertEquals(fields.get(6).getPosition(), 6);
    Assert.assertEquals(fields.get(6).getName(), "stringField");
    Assert.assertEquals(fields.get(6).getSchema().type(), AvroType.STRING);
    Assert.assertEquals(fields.get(7).getPosition(), 7);
    Assert.assertEquals(fields.get(7).getName(), "enumField");
    Assert.assertEquals(fields.get(7).getSchema().type(), AvroType.ENUM);
    AvroEnumSchema simpleEnumSchema = (AvroEnumSchema) fields.get(7).getSchema();
    Assert.assertEquals(simpleEnumSchema.getFullName(), "innerNamespace.SimpleEnum");
    Assert.assertEquals(simpleEnumSchema.getSymbols(), Arrays.asList("A", "B", "C"));
    Assert.assertEquals(fields.get(8).getPosition(), 8);
    Assert.assertEquals(fields.get(8).getName(), "fixedField");
    Assert.assertEquals(fields.get(8).getSchema().type(), AvroType.FIXED);
    Assert.assertEquals(((AvroFixedSchema) fields.get(8).getSchema()).getFullName(), "com.acme.SimpleFixed");
    Assert.assertEquals(((AvroFixedSchema) fields.get(8).getSchema()).getSize(), 7);
    Assert.assertEquals(fields.get(9).getPosition(), 9);
    Assert.assertEquals(fields.get(9).getName(), "strArrayField");
    Assert.assertEquals(fields.get(9).getSchema().type(), AvroType.ARRAY);
    Assert.assertEquals(((AvroArraySchema) fields.get(9).getSchema()).getValueSchema().type(), AvroType.NULL);
    Assert.assertEquals(fields.get(10).getPosition(), 10);
    Assert.assertEquals(fields.get(10).getName(), "enumMapField");
    Assert.assertEquals(fields.get(10).getSchema().type(), AvroType.MAP);
    AvroSchema mapValueSchema = ((AvroMapSchema) fields.get(10).getSchema()).getValueSchema();
    Assert.assertSame(mapValueSchema, simpleEnumSchema);
}
Also used : AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 2 with AvroMapSchema

use of com.linkedin.avroutil1.model.AvroMapSchema 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 AvroMapSchema

use of com.linkedin.avroutil1.model.AvroMapSchema 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 AvroMapSchema

use of com.linkedin.avroutil1.model.AvroMapSchema in project avro-util by linkedin.

the class AvscParser method parseCollectionSchema.

private AvroCollectionSchema parseCollectionSchema(JsonObjectExt objectNode, AvscFileParseContext context, AvroType avroType, CodeLocation codeLocation, JsonPropertiesContainer props) {
    switch(avroType) {
        case ARRAY:
            JsonValueExt arrayItemsNode = getRequiredNode(objectNode, "items", () -> "array declarations must have an items property");
            SchemaOrRef arrayItemSchema = parseSchemaDeclOrRef(arrayItemsNode, context, false);
            return new AvroArraySchema(codeLocation, arrayItemSchema, props);
        case MAP:
            JsonValueExt mapValuesNode = getRequiredNode(objectNode, "values", () -> "map declarations must have a values property");
            SchemaOrRef mapValueSchema = parseSchemaDeclOrRef(mapValuesNode, context, false);
            return new AvroMapSchema(codeLocation, mapValueSchema, props);
        default:
            throw new IllegalStateException("unhandled: " + avroType + " for object at " + codeLocation.getStart());
    }
}
Also used : AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)

Aggregations

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