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");
}
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");
}
}
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;
}
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);
}
}
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");
}
Aggregations