use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class AbstractDefaultDataTranslator method translate.
protected Object translate(List<Object> path, Object value, DataSchema dataSchema) {
dataSchema = dataSchema.getDereferencedDataSchema();
DataSchema.Type type = dataSchema.getType();
Object result;
switch(type) {
case NULL:
if (value != Data.NULL) {
throw new IllegalArgumentException(message(path, "value must be null for null schema"));
}
result = value;
break;
case BOOLEAN:
result = ((Boolean) value).booleanValue();
break;
case INT:
result = ((Number) value).intValue();
break;
case LONG:
result = ((Number) value).longValue();
break;
case FLOAT:
result = ((Number) value).floatValue();
break;
case DOUBLE:
result = ((Number) value).doubleValue();
break;
case STRING:
result = (String) value;
break;
case BYTES:
Class<?> clazz = value.getClass();
if (clazz != String.class && clazz != ByteString.class) {
throw new IllegalArgumentException(message(path, "bytes value %1$s is not a String or ByteString", value));
}
result = value;
break;
case ENUM:
String enumValue = (String) value;
EnumDataSchema enumDataSchema = (EnumDataSchema) dataSchema;
if (!enumDataSchema.getSymbols().contains(enumValue)) {
throw new IllegalArgumentException(message(path, "enum value %1$s not one of %2$s", value, enumDataSchema.getSymbols()));
}
result = value;
break;
case FIXED:
clazz = value.getClass();
ByteString byteString;
if (clazz == String.class) {
byteString = ByteString.copyAvroString((String) value, true);
} else if (clazz == ByteString.class) {
byteString = (ByteString) value;
} else {
throw new IllegalArgumentException(message(path, "fixed value %1$s is not a String or ByteString", value));
}
FixedDataSchema fixedDataSchema = (FixedDataSchema) dataSchema;
if (fixedDataSchema.getSize() != byteString.length()) {
throw new IllegalArgumentException(message(path, "ByteString size %1$d != FixedDataSchema size %2$d", byteString.length(), fixedDataSchema.getSize()));
}
result = byteString;
break;
case MAP:
DataMap map = (DataMap) value;
DataSchema valueDataSchema = ((MapDataSchema) dataSchema).getValues();
Map<String, Object> resultMap = new DataMap(map.size() * 2);
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
path.add(key);
Object entryAvroValue = translate(path, entry.getValue(), valueDataSchema);
path.remove(path.size() - 1);
resultMap.put(key, entryAvroValue);
}
result = resultMap;
break;
case ARRAY:
DataList list = (DataList) value;
DataList resultList = new DataList(list.size());
DataSchema elementDataSchema = ((ArrayDataSchema) dataSchema).getItems();
for (int i = 0; i < list.size(); i++) {
path.add(i);
Object entryAvroValue = translate(path, list.get(i), elementDataSchema);
path.remove(path.size() - 1);
resultList.add(entryAvroValue);
}
result = resultList;
break;
case RECORD:
DataMap recordMap = (DataMap) value;
RecordDataSchema recordDataSchema = (RecordDataSchema) dataSchema;
DataMap resultRecordMap = new DataMap(recordDataSchema.getFields().size() * 2);
for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
String fieldName = field.getName();
Object fieldValue = recordMap.get(fieldName);
path.add(fieldName);
Object resultFieldValue = translateField(path, fieldValue, field);
path.remove(path.size() - 1);
if (resultFieldValue != null) {
resultRecordMap.put(fieldName, resultFieldValue);
}
}
result = resultRecordMap;
break;
case UNION:
result = translateUnion(path, value, (UnionDataSchema) dataSchema);
break;
default:
throw new IllegalStateException(message(path, "schema type unknown %1$s", type));
}
return result;
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class PegasusUnionToAvroRecordConvertCallback method buildDiscriminatorEnumField.
private RecordDataSchema.Field buildDiscriminatorEnumField(String parentRecordFullName, List<String> memberKeys, StringBuilder errorMessageBuilder) {
Name enumName = new Name(parentRecordFullName + SchemaTranslator.CONTAINER_RECORD_DISCRIMINATOR_ENUM_SUFFIX, errorMessageBuilder);
EnumDataSchema enumDataSchema = new EnumDataSchema(enumName);
enumDataSchema.setSymbols(memberKeys, errorMessageBuilder);
RecordDataSchema.Field field = new RecordDataSchema.Field(enumDataSchema);
field.setName(DataSchemaConstants.DISCRIMINATOR_FIELD, errorMessageBuilder);
field.setDoc("Contains the name of the field that has its value set.");
field.setDeclaredInline(true);
field.setOptional(false);
return field;
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class PdlSchemaParser method parseEnum.
private EnumDataSchema parseEnum(NamedTypeDeclarationContext context, EnumDeclarationContext enumDecl) throws ParseException {
Name name = toName(enumDecl.name);
EnumDataSchema schema = new EnumDataSchema(name);
// This is useful to set the doc and the aliases, but the properties are overwritten later (see below)
Map<String, Object> props = setDocAndProperties(context, schema);
bindNameToSchema(name, schema.getAliases(), schema);
List<EnumSymbolDeclarationContext> symbolDecls = enumDecl.enumDecl.symbolDecls;
List<String> symbols = new ArrayList<>(symbolDecls.size());
Map<String, Object> symbolDocs = new HashMap<>();
DataMap deprecatedSymbols = new DataMap();
DataMap symbolProperties = new DataMap();
for (EnumSymbolDeclarationContext symbolDecl : symbolDecls) {
symbols.add(symbolDecl.symbol.value);
recordLocation(symbolDecl.symbol.value, symbolDecl);
if (symbolDecl.doc != null) {
symbolDocs.put(symbolDecl.symbol.value, symbolDecl.doc.value);
}
for (PropDeclarationContext prop : symbolDecl.props) {
String symbol = symbolDecl.symbol.value;
Object value = parsePropValue(prop);
if (equalsSingleSegmentProperty(prop, DataSchemaConstants.DEPRECATED_KEY)) {
deprecatedSymbols.put(symbol, value);
} else {
List<String> path = new ArrayList<>(prop.path.size() + 1);
path.add(symbol);
path.addAll(prop.path);
addPropertiesAtPath(prop, symbolProperties, path, value);
}
}
}
schema.setSymbols(symbols, errorMessageBuilder());
if (!symbolDocs.isEmpty()) {
schema.setSymbolDocs(symbolDocs, errorMessageBuilder());
}
if (!deprecatedSymbols.isEmpty()) {
props.put(DataSchemaConstants.DEPRECATED_SYMBOLS_KEY, deprecatedSymbols);
}
if (!symbolProperties.isEmpty()) {
props.put(DataSchemaConstants.SYMBOL_PROPERTIES_KEY, symbolProperties);
}
// Overwrite the properties now that we've computed the special symbol properties
schema.setProperties(props);
return schema;
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class ResolvedPropertiesReaderVisitor method callbackOnContext.
@Override
public void callbackOnContext(TraverserContext context, DataSchemaTraverse.Order order) {
if (order == DataSchemaTraverse.Order.POST_ORDER) {
return;
}
DataSchema currentSchema = context.getCurrentSchema();
if ((currentSchema.isPrimitive() || (currentSchema instanceof EnumDataSchema) || (currentSchema instanceof FixedDataSchema))) {
Map<String, Object> resolvedProperties = currentSchema.getResolvedProperties();
_leafFieldsToResolvedPropertiesMap.put(new PathSpec(context.getSchemaPathSpec()), resolvedProperties);
if (LOG.isDebugEnabled()) {
String mapStringified = resolvedProperties.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
LOG.debug(String.format("/%s ::: %s", String.join("/", context.getSchemaPathSpec()), mapStringified));
}
}
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class TestPdlSchemaParser method testEnumParserLocations.
@Test
public void testEnumParserLocations() {
PdlSchemaParser parser = new PdlSchemaParser(new DefaultDataSchemaResolver(), true);
parser.parse(getClass().getResourceAsStream("TestEnumForParserContextLocations.pdl"));
List<DataSchema> topLevelSchemas = parser.topLevelDataSchemas();
Assert.assertEquals(topLevelSchemas.size(), 1, "Expected 1 top-level schema to be parsed.");
EnumDataSchema topSchema = (EnumDataSchema) topLevelSchemas.get(0);
Map<Object, PdlSchemaParser.ParseLocation> locations = parser.getParseLocations();
checkParseLocationForEnum(locations, topSchema);
}
Aggregations