Search in sources :

Example 11 with SchemaOrRef

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

Example 12 with SchemaOrRef

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

the class AvscParser method parse.

private AvscParseResult parse(AvscFileParseContext context, Reader reader) {
    JsonReaderExt jsonReader = new JsonReaderWithLocations(reader, null);
    JsonValueExt root;
    AvscParseResult result = new AvscParseResult();
    try {
        root = jsonReader.readValue();
    } catch (JsonParsingException e) {
        Throwable rootCause = Util.rootCause(e);
        String message = rootCause.getMessage();
        if (message != null && message.startsWith("Unexpected char 47")) {
            // 47 is ascii for '/' (utf-8 matches ascii on "low" characters). we are going to assume this means
            // someone tried putting a //comment into an avsc source
            result.recordError(new JsonParseException("comments not supported in json at" + e.getLocation(), e));
        } else {
            result.recordError(new JsonParseException("json parse error at " + e.getLocation(), e));
        }
        return result;
    } catch (Exception e) {
        result.recordError(new JsonParseException("unknown json parse error", e));
        return result;
    }
    try {
        SchemaOrRef schemaOrRef = parseSchemaDeclOrRef(root, context, true);
        context.resolveReferences();
        result.recordParseComplete(context);
    } catch (Exception parseIssue) {
        result.recordError(parseIssue);
    }
    return result;
}
Also used : SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) JsonReaderWithLocations(com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations) JsonReaderExt(com.linkedin.avroutil1.parser.jsonpext.JsonReaderExt) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) ParseException(com.linkedin.avroutil1.parser.exceptions.ParseException) FileNotFoundException(java.io.FileNotFoundException) JsonParsingException(javax.json.stream.JsonParsingException) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) JsonParsingException(javax.json.stream.JsonParsingException)

Example 13 with SchemaOrRef

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

the class AvscParser method parseUnionSchema.

private SchemaOrRef parseUnionSchema(JsonArrayExt arrayNode, AvscFileParseContext context, boolean topLevel) {
    CodeLocation codeLocation = locationOf(context.getUri(), arrayNode);
    List<SchemaOrRef> unionTypes = new ArrayList<>(arrayNode.size());
    for (JsonValue jsonValue : arrayNode) {
        JsonValueExt unionNode = (JsonValueExt) jsonValue;
        SchemaOrRef type = parseSchemaDeclOrRef(unionNode, context, false);
        unionTypes.add(type);
    }
    AvroUnionSchema unionSchema = new AvroUnionSchema(codeLocation);
    unionSchema.setTypes(unionTypes);
    context.defineSchema(unionSchema, topLevel);
    return new SchemaOrRef(codeLocation, unionSchema);
}
Also used : CodeLocation(com.linkedin.avroutil1.model.CodeLocation) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) ArrayList(java.util.ArrayList) JsonValue(javax.json.JsonValue) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)

Aggregations

SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)12 AvroType (com.linkedin.avroutil1.model.AvroType)5 CodeLocation (com.linkedin.avroutil1.model.CodeLocation)5 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)5 JsonValueExt (com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)5 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)4 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)4 AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)4 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)3 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)2 JsonPropertiesContainer (com.linkedin.avroutil1.model.JsonPropertiesContainer)2 ArrayList (java.util.ArrayList)2 JsonValue (javax.json.JsonValue)2 Test (org.testng.annotations.Test)2 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)1 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)1 AvroLiteral (com.linkedin.avroutil1.model.AvroLiteral)1 AvroLogicalType (com.linkedin.avroutil1.model.AvroLogicalType)1