use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.
the class AvscSchemaWriter method emitRecordFields.
protected void emitRecordFields(AvroRecordSchema schema, AvscWriterContext context, AvscWriterConfig config, JsonObjectBuilder output) {
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
List<AvroSchemaField> fields = schema.getFields();
for (AvroSchemaField field : fields) {
JsonObjectBuilder fieldBuilder = Json.createObjectBuilder();
fieldBuilder.add("name", field.getName());
if (field.hasDoc()) {
fieldBuilder.add("doc", field.getDoc());
}
AvroSchema fieldSchema = field.getSchema();
fieldBuilder.add("type", writeSchema(fieldSchema, context, config));
if (field.hasDefaultValue()) {
AvroLiteral defaultValue = field.getDefaultValue();
JsonValue defaultValueLiteral = writeDefaultValue(fieldSchema, defaultValue);
fieldBuilder.add("default", defaultValueLiteral);
}
// TODO - order
// TODO - aliases
arrayBuilder.add(fieldBuilder);
}
output.add("fields", arrayBuilder);
}
use of com.linkedin.avroutil1.model.AvroSchemaField 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);
}
use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.
the class AvscParserTest method testParsingDefaultValues.
@Test
public void testParsingDefaultValues() throws Exception {
String avsc = TestUtil.load("schemas/TestRecordWithDefaultValues.avsc");
AvscParser parser = new AvscParser();
AvscParseResult result = parser.parse(avsc);
Assert.assertNull(result.getParseError());
AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
for (AvroSchemaField field : schema.getFields()) {
Assert.assertNotNull(field.getDefaultValue(), "field " + field.getName() + " has a null default");
}
}
use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.
the class AvscParserTest method testParsingHorribleDefaultValues.
@Test
public void testParsingHorribleDefaultValues() throws Exception {
String avsc = TestUtil.load("schemas/TestRecordWithHorribleDefaultValues.avsc");
AvscParser parser = new AvscParser();
AvscParseResult result = parser.parse(avsc);
Assert.assertNull(result.getParseError());
AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
Assert.assertNotNull(schema);
for (AvroSchemaField field : schema.getFields()) {
List<AvscIssue> issuesWithField = result.getIssues(field);
Assert.assertFalse(issuesWithField.isEmpty(), "field " + field.getName() + " has no issues?!");
Assert.assertFalse(issuesWithField.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
Assert.assertNull(field.getDefaultValue());
}
}
use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.
the class AvscParserTest method testParsingLogicalTypes.
@Test
public void testParsingLogicalTypes() throws Exception {
String avsc = TestUtil.load("schemas/TestRecordWithLogicalTypes.avsc");
AvscParser parser = new AvscParser();
AvscParseResult result = parser.parse(avsc);
Assert.assertNull(result.getParseError());
AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
Assert.assertNotNull(schema);
for (AvroSchemaField field : schema.getFields()) {
switch(field.getName()) {
case "bytesDecimalField":
Assert.assertEquals(field.getSchema().type(), AvroType.BYTES);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DECIMAL);
Assert.assertEquals(((AvroPrimitiveSchema) field.getSchema()).getScale(), 2);
Assert.assertEquals(((AvroPrimitiveSchema) field.getSchema()).getPrecision(), 4);
break;
case "fixedDecimalField":
Assert.assertEquals(field.getSchema().type(), AvroType.FIXED);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DECIMAL);
break;
case "fixedDurationField":
Assert.assertEquals(field.getSchema().type(), AvroType.FIXED);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DURATION);
break;
case "stringUUIDField":
Assert.assertEquals(field.getSchema().type(), AvroType.STRING);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.UUID);
break;
case "intDateField":
Assert.assertEquals(field.getSchema().type(), AvroType.INT);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DATE);
break;
case "intTimeMillisField":
Assert.assertEquals(field.getSchema().type(), AvroType.INT);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIME_MILLIS);
break;
case "longTimeMicrosField":
Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIME_MICROS);
break;
case "longTimestampMillisField":
Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIMESTAMP_MILLIS);
break;
case "longTimestampMicrosField":
Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIMESTAMP_MICROS);
break;
case "longLocalTimestampMillisField":
Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.LOCAL_TIMESTAMP_MILLIS);
break;
case "longLocalTimestampMicrosField":
Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.LOCAL_TIMESTAMP_MICROS);
break;
default:
break;
}
}
}
Aggregations