use of com.linkedin.avroutil1.model.AvroSchema in project rest.li by linkedin.
the class TestSchemaTranslator method testFromAvroSchema.
@Test(dataProvider = "fromAvroSchemaData")
public void testFromAvroSchema(String avroText, String schemaText) throws Exception {
AvroToDataSchemaTranslationOptions[] options = { new AvroToDataSchemaTranslationOptions(AvroToDataSchemaTranslationMode.TRANSLATE), new AvroToDataSchemaTranslationOptions(AvroToDataSchemaTranslationMode.RETURN_EMBEDDED_SCHEMA), new AvroToDataSchemaTranslationOptions(AvroToDataSchemaTranslationMode.VERIFY_EMBEDDED_SCHEMA) };
// test generating Pegasus schema from Avro schema
for (AvroToDataSchemaTranslationOptions option : options) {
DataSchema schema = SchemaTranslator.avroToDataSchema(avroText, option);
String schemaTextFromAvro = SchemaToJsonEncoder.schemaToJson(schema, JsonBuilder.Pretty.SPACES);
assertEquals(TestUtil.dataMapFromString(schemaTextFromAvro), TestUtil.dataMapFromString(schemaText));
Schema avroSchema = AvroCompatibilityHelper.parse(avroText, new SchemaParseConfiguration(false, false), null).getMainSchema();
String preTranslateAvroSchema = avroSchema.toString();
schema = SchemaTranslator.avroToDataSchema(avroSchema, option);
schemaTextFromAvro = SchemaToJsonEncoder.schemaToJson(schema, JsonBuilder.Pretty.SPACES);
assertEquals(TestUtil.dataMapFromString(schemaTextFromAvro), TestUtil.dataMapFromString(schemaText));
String postTranslateAvroSchema = avroSchema.toString();
assertEquals(preTranslateAvroSchema, postTranslateAvroSchema);
}
}
use of com.linkedin.avroutil1.model.AvroSchema 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.AvroSchema in project avro-util by linkedin.
the class AvscSchemaWriter method writeDefaultValue.
protected JsonValue writeDefaultValue(AvroSchema fieldSchema, AvroLiteral literal) {
AvroType type = fieldSchema.type();
String temp;
switch(type) {
case NULL:
// noinspection unused (kept as a sanity check)
AvroNullLiteral nullLiteral = (AvroNullLiteral) literal;
return JsonValue.NULL;
case BOOLEAN:
AvroBooleanLiteral boolLiteral = (AvroBooleanLiteral) literal;
return boolLiteral.getValue() ? JsonValue.TRUE : JsonValue.FALSE;
case INT:
AvroIntegerLiteral intLiteral = (AvroIntegerLiteral) literal;
return Json.createValue(intLiteral.getValue());
case LONG:
AvroLongLiteral longLiteral = (AvroLongLiteral) literal;
return Json.createValue(longLiteral.getValue());
case FLOAT:
AvroFloatLiteral floatLiteral = (AvroFloatLiteral) literal;
return Json.createValue(floatLiteral.getValue());
case DOUBLE:
AvroDoubleLiteral doubleLiteral = (AvroDoubleLiteral) literal;
return Json.createValue(doubleLiteral.getValue());
case STRING:
AvroStringLiteral stringLiteral = (AvroStringLiteral) literal;
return Json.createValue(stringLiteral.getValue());
case BYTES:
AvroBytesLiteral bytesLiteral = (AvroBytesLiteral) literal;
// spec says "values for bytes and fixed fields are JSON strings, where Unicode code points
// 0-255 are mapped to unsigned 8-bit byte values 0-255", and this is how its done
temp = new String(bytesLiteral.getValue(), StandardCharsets.ISO_8859_1);
return Json.createValue(temp);
case ENUM:
AvroEnumLiteral enumLiteral = (AvroEnumLiteral) literal;
return Json.createValue(enumLiteral.getValue());
case FIXED:
AvroFixedLiteral fixedLiteral = (AvroFixedLiteral) literal;
// spec says "values for bytes and fixed fields are JSON strings, where Unicode code points
// 0-255 are mapped to unsigned 8-bit byte values 0-255", and this is how its done
temp = new String(fixedLiteral.getValue(), StandardCharsets.ISO_8859_1);
return Json.createValue(temp);
case ARRAY:
AvroArrayLiteral arrayLiteral = (AvroArrayLiteral) literal;
List<AvroLiteral> array = arrayLiteral.getValue();
AvroArraySchema arraySchema = (AvroArraySchema) arrayLiteral.getSchema();
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
for (AvroLiteral element : array) {
arrayBuilder.add(writeDefaultValue(arraySchema.getValueSchema(), element));
}
return arrayBuilder.build();
default:
throw new UnsupportedOperationException("writing default values for " + type + " not implemented yet");
}
}
use of com.linkedin.avroutil1.model.AvroSchema in project avro-util by linkedin.
the class AvscSchemaWriter method pathForSchema.
protected Path pathForSchema(AvroSchema maybeNamed) {
if (!(maybeNamed instanceof AvroNamedSchema)) {
return null;
}
AvroNamedSchema namedSchema = (AvroNamedSchema) maybeNamed;
AvroName name = namedSchema.getName();
if (!name.hasNamespace()) {
return Paths.get(name.getSimpleName() + "." + AvscFile.SUFFIX);
}
String fullname = name.getFullname();
String[] parts = fullname.split("\\.");
String[] pathParts = new String[parts.length - 1];
for (int i = 1; i < parts.length; i++) {
if (i == parts.length - 1) {
pathParts[i - 1] = parts[i] + "." + AvscFile.SUFFIX;
} else {
pathParts[i - 1] = parts[i];
}
}
return Paths.get(parts[0], pathParts);
}
use of com.linkedin.avroutil1.model.AvroSchema in project avro-util by linkedin.
the class AvscWriterContext method schemaEncountered.
/**
* registers a (possibly known) named schema
* @param schema a name schema encountered during avsc generation
* @return true if schema has already been encountered (and hence defined) before, false if schema is new (seen 1st time)
*/
public boolean schemaEncountered(AvroNamedSchema schema) {
String fullName = schema.getFullName();
AvroSchema alreadySeen = known.get(fullName);
if (alreadySeen == null) {
known.put(fullName, schema);
return false;
}
// make sure we dont have a different redefinition
if (!alreadySeen.equals(schema)) {
throw new IllegalStateException("schema " + fullName + " at " + schema.getCodeLocation() + " already seen (and different) at " + alreadySeen.getCodeLocation());
}
return true;
}
Aggregations