Search in sources :

Example 1 with AvroJavaStringRepresentation

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

the class AvscParser method parseDecoratedPrimitiveSchema.

private AvroPrimitiveSchema parseDecoratedPrimitiveSchema(JsonObjectExt primitiveNode, AvscFileParseContext context, AvroType avroType, CodeLocation codeLocation, JsonPropertiesContainer props) {
    AvroLogicalType logicalType = null;
    int scale = 0;
    int precision = 0;
    Parsed<AvroLogicalType> logicalTypeResult = parseLogicalType(primitiveNode, context, avroType, codeLocation);
    if (logicalTypeResult.hasIssues()) {
        context.addIssues(logicalTypeResult.getIssues());
    }
    // might be null
    logicalType = logicalTypeResult.getData();
    Parsed<AvroJavaStringRepresentation> stringRepResult = parseStringRepresentation(primitiveNode, context, avroType, codeLocation);
    AvroJavaStringRepresentation stringRep = null;
    if (stringRepResult.hasIssues()) {
        context.addIssues(stringRepResult.getIssues());
    }
    if (AvroType.STRING.equals(avroType) && stringRepResult.hasData()) {
        stringRep = stringRepResult.getData();
    }
    Located<Integer> precisionResult = getOptionalInteger(primitiveNode, "precision", context);
    Located<Integer> scaleResult = getOptionalInteger(primitiveNode, "scale", context);
    boolean scaleAndPrecisionExpected = AvroType.BYTES.equals(avroType) && AvroLogicalType.DECIMAL.equals(logicalType);
    if (scaleAndPrecisionExpected) {
        boolean scaleAndPrecisionOK = true;
        int precisionValue = 0;
        int scaleValue = 0;
        // precision is actually required
        if (precisionResult == null) {
            context.addIssue(AvscIssues.precisionRequiredAndNotSet(codeLocation, avroType, logicalType));
            scaleAndPrecisionOK = false;
        }
        if (scaleAndPrecisionOK && scaleResult != null) {
            precisionValue = precisionResult.getValue();
            scaleValue = scaleResult.getValue();
            if (precisionValue < scaleValue) {
                context.addIssue(AvscIssues.precisionSmallerThanScale(locationOf(context.getUri(), precisionResult), precisionValue, locationOf(context.getUri(), scaleResult), scaleValue));
                scaleAndPrecisionOK = false;
            }
        }
        if (scaleAndPrecisionOK) {
            scale = scaleValue;
            precision = precisionValue;
        } else {
            // "cancel" the logicalType
            logicalType = null;
        }
    }
    return new AvroPrimitiveSchema(codeLocation, avroType, logicalType, stringRep, scale, precision, props);
}
Also used : BigInteger(java.math.BigInteger) AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) AvroLogicalType(com.linkedin.avroutil1.model.AvroLogicalType) AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation)

Example 2 with AvroJavaStringRepresentation

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

the class AvscParser method parseStringRepresentation.

private Parsed<AvroJavaStringRepresentation> parseStringRepresentation(// type declaration node
JsonObjectExt objectNode, AvscFileParseContext context, AvroType avroType, CodeLocation codeLocation) {
    Parsed<AvroJavaStringRepresentation> result = new Parsed<>();
    Located<String> repStr = getOptionalString(objectNode, "avro.java.string");
    AvroJavaStringRepresentation representation;
    if (repStr != null) {
        representation = AvroJavaStringRepresentation.fromJson(repStr.getValue());
        if (representation != null) {
            result.setData(representation);
        } else {
            result.recordIssue(AvscIssues.unknownJavaStringRepresentation(locationOf(context.getUri(), repStr), repStr.getValue()));
        }
        // does string rep even make sense here? is type even a string?
        if (!AvroType.STRING.equals(avroType)) {
            result.recordIssue(AvscIssues.stringRepresentationOnNonString(locationOf(context.getUri(), repStr), repStr.getValue(), avroType));
        }
    }
    return result;
}
Also used : AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation)

Aggregations

AvroJavaStringRepresentation (com.linkedin.avroutil1.model.AvroJavaStringRepresentation)2 AvroLogicalType (com.linkedin.avroutil1.model.AvroLogicalType)1 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)1 BigInteger (java.math.BigInteger)1