Search in sources :

Example 1 with AvroSchema

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);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) DataSchema(com.linkedin.data.schema.DataSchema) Schema(org.apache.avro.Schema) Test(org.testng.annotations.Test)

Example 2 with AvroSchema

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);
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema) JsonValue(javax.json.JsonValue) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) AvroLiteral(com.linkedin.avroutil1.model.AvroLiteral)

Example 3 with AvroSchema

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");
    }
}
Also used : AvroLongLiteral(com.linkedin.avroutil1.model.AvroLongLiteral) AvroBooleanLiteral(com.linkedin.avroutil1.model.AvroBooleanLiteral) AvroStringLiteral(com.linkedin.avroutil1.model.AvroStringLiteral) AvroDoubleLiteral(com.linkedin.avroutil1.model.AvroDoubleLiteral) AvroNullLiteral(com.linkedin.avroutil1.model.AvroNullLiteral) AvroFloatLiteral(com.linkedin.avroutil1.model.AvroFloatLiteral) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) AvroArrayLiteral(com.linkedin.avroutil1.model.AvroArrayLiteral) AvroType(com.linkedin.avroutil1.model.AvroType) AvroFixedLiteral(com.linkedin.avroutil1.model.AvroFixedLiteral) JsonArrayBuilder(javax.json.JsonArrayBuilder) AvroIntegerLiteral(com.linkedin.avroutil1.model.AvroIntegerLiteral) AvroBytesLiteral(com.linkedin.avroutil1.model.AvroBytesLiteral) AvroLiteral(com.linkedin.avroutil1.model.AvroLiteral) AvroEnumLiteral(com.linkedin.avroutil1.model.AvroEnumLiteral)

Example 4 with AvroSchema

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

Example 5 with AvroSchema

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

Aggregations

AvroSchema (com.linkedin.avroutil1.model.AvroSchema)11 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)5 AvroType (com.linkedin.avroutil1.model.AvroType)5 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)4 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)4 AvroLiteral (com.linkedin.avroutil1.model.AvroLiteral)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)3 JsonArrayBuilder (javax.json.JsonArrayBuilder)3 Test (org.testng.annotations.Test)3 AvroArrayLiteral (com.linkedin.avroutil1.model.AvroArrayLiteral)2 AvroBooleanLiteral (com.linkedin.avroutil1.model.AvroBooleanLiteral)2 AvroBytesLiteral (com.linkedin.avroutil1.model.AvroBytesLiteral)2 AvroDoubleLiteral (com.linkedin.avroutil1.model.AvroDoubleLiteral)2 AvroEnumLiteral (com.linkedin.avroutil1.model.AvroEnumLiteral)2 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)2 AvroFixedLiteral (com.linkedin.avroutil1.model.AvroFixedLiteral)2 AvroFloatLiteral (com.linkedin.avroutil1.model.AvroFloatLiteral)2 AvroIntegerLiteral (com.linkedin.avroutil1.model.AvroIntegerLiteral)2 AvroLongLiteral (com.linkedin.avroutil1.model.AvroLongLiteral)2