Search in sources :

Example 1 with AvroType

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

the class AvscParser method parseSimplePrimitiveOrRef.

private SchemaOrRef parseSimplePrimitiveOrRef(JsonStringExt stringNode, AvscFileParseContext context) {
    CodeLocation codeLocation = locationOf(context.getUri(), stringNode);
    String typeString = stringNode.getString();
    AvroType avroType = AvroType.fromJson(typeString);
    // TODO - screen for reserved words??
    if (avroType == null) {
        // assume it's a ref
        return new SchemaOrRef(codeLocation, typeString);
    }
    if (avroType.isPrimitive()) {
        // no logical type information, string representation or props in the schema if we got here
        return new SchemaOrRef(codeLocation, AvroPrimitiveSchema.forType(codeLocation, avroType, null, null, 0, 0, JsonPropertiesContainer.EMPTY));
    }
    // if we got here it means we found something like "record" as a type literal. which is not valid syntax
    throw new AvroSyntaxException("Illegal avro type \"" + typeString + "\" at " + codeLocation.getStart() + ". " + "Expecting a primitive type, an inline type definition or a reference the the fullname of a named type defined elsewhere");
}
Also used : CodeLocation(com.linkedin.avroutil1.model.CodeLocation) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroType(com.linkedin.avroutil1.model.AvroType) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)

Example 2 with AvroType

use of com.linkedin.avroutil1.model.AvroType 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 3 with AvroType

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

the class AvscParser method parseSchemaName.

private AvroName parseSchemaName(JsonObjectExt objectNode, AvscFileParseContext context, AvroType avroType) {
    Located<String> nameStr = getRequiredString(objectNode, "name", () -> avroType + " is a named type");
    Located<String> namespaceStr = getOptionalString(objectNode, "namespace");
    String name = nameStr.getValue();
    String namespace = namespaceStr != null ? namespaceStr.getValue() : null;
    AvroName schemaName;
    if (name.contains(".")) {
        // the name specified is a full name (namespace included)
        context.addIssue(AvscIssues.useOfFullName(new CodeLocation(context.getUri(), nameStr.getLocation(), nameStr.getLocation()), avroType, name));
        if (namespace != null) {
            // namespace will be ignored, but it's confusing to even list it
            context.addIssue(AvscIssues.ignoredNamespace(new CodeLocation(context.getUri(), namespaceStr.getLocation(), namespaceStr.getLocation()), avroType, namespace, name));
        }
        // TODO - validate names (no ending in dot, no spaces, etc)
        int lastDot = name.lastIndexOf('.');
        schemaName = new AvroName(name.substring(lastDot + 1), name.substring(0, lastDot));
    } else {
        String inheritedNamespace = namespace != null ? namespace : context.getCurrentNamespace();
        schemaName = new AvroName(name, inheritedNamespace);
    }
    return schemaName;
}
Also used : CodeLocation(com.linkedin.avroutil1.model.CodeLocation) AvroName(com.linkedin.avroutil1.model.AvroName)

Example 4 with AvroType

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

the class AvscParser method parseLogicalType.

private Parsed<AvroLogicalType> parseLogicalType(// type declaration node
JsonObjectExt objectNode, AvscFileParseContext context, AvroType avroType, CodeLocation codeLocation) {
    Parsed<AvroLogicalType> result;
    Located<String> logicalTypeStr = getOptionalString(objectNode, "logicalType");
    AvroLogicalType logicalType;
    if (logicalTypeStr != null) {
        logicalType = AvroLogicalType.fromJson(logicalTypeStr.getValue());
        if (logicalType == null) {
            return new Parsed<>(AvscIssues.unknownLogicalType(locationOf(context.getUri(), logicalTypeStr), logicalTypeStr.getValue()));
        } else {
            // we have a logicalType, see if it matches the type
            if (!logicalType.getParentTypes().contains(avroType)) {
                return new Parsed<>(AvscIssues.mismatchedLogicalType(locationOf(context.getUri(), logicalTypeStr), logicalType, avroType));
            }
            return new Parsed<>(logicalType);
        }
    } else {
        // no logical type (also no issues)
        return new Parsed<>((AvroLogicalType) null);
    }
}
Also used : AvroLogicalType(com.linkedin.avroutil1.model.AvroLogicalType)

Example 5 with AvroType

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

the class AvscParser method parseSimplePrimitiveOrRef.

private SchemaOrRef parseSimplePrimitiveOrRef(JsonStringExt stringNode, AvscFileParseContext context, boolean topLevel) {
    CodeLocation codeLocation = locationOf(context.getUri(), stringNode);
    String typeString = stringNode.getString();
    AvroType avroType = AvroType.fromTypeName(typeString);
    if (avroType == null) {
        // assume it's a ref
        return new SchemaOrRef(codeLocation, typeString, context.getCurrentNamespace());
    }
    if (avroType.isPrimitive()) {
        // no logical type information, string representation or props in the schema if we got here
        AvroPrimitiveSchema primitiveSchema = AvroPrimitiveSchema.forType(codeLocation, avroType, null, null, 0, 0, JsonPropertiesContainer.EMPTY);
        context.defineSchema(primitiveSchema, topLevel);
        return new SchemaOrRef(codeLocation, primitiveSchema);
    }
    // if we got here it means we found something like "record" as a type literal. which is not valid syntax
    throw new AvroSyntaxException("Illegal avro type \"" + typeString + "\" at " + codeLocation.getStart() + ". " + "Expecting a primitive type, an inline type definition or a reference the the fullname of a named type defined elsewhere");
}
Also used : AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) CodeLocation(com.linkedin.avroutil1.model.CodeLocation) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroType(com.linkedin.avroutil1.model.AvroType) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)

Aggregations

AvroType (com.linkedin.avroutil1.model.AvroType)8 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)7 CodeLocation (com.linkedin.avroutil1.model.CodeLocation)6 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)5 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)5 JsonValueExt (com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)5 AvroName (com.linkedin.avroutil1.model.AvroName)4 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)4 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)3 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)3 AvroLiteral (com.linkedin.avroutil1.model.AvroLiteral)3 AvroLogicalType (com.linkedin.avroutil1.model.AvroLogicalType)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)3 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)3 JsonArrayExt (com.linkedin.avroutil1.parser.jsonpext.JsonArrayExt)3 ArrayList (java.util.ArrayList)3 JsonArrayBuilder (javax.json.JsonArrayBuilder)3 JsonValue (javax.json.JsonValue)3 AvroArrayLiteral (com.linkedin.avroutil1.model.AvroArrayLiteral)2