Search in sources :

Example 1 with EnumDataSchema

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;
}
Also used : ByteString(com.linkedin.data.ByteString) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 2 with EnumDataSchema

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;
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Name(com.linkedin.data.schema.Name)

Example 3 with EnumDataSchema

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;
}
Also used : EnumSymbolDeclarationContext(com.linkedin.data.grammar.PdlParser.EnumSymbolDeclarationContext) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) PropDeclarationContext(com.linkedin.data.grammar.PdlParser.PropDeclarationContext) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Name(com.linkedin.data.schema.Name) DataMap(com.linkedin.data.DataMap)

Example 4 with EnumDataSchema

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));
        }
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) Logger(org.slf4j.Logger) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) PathSpec(com.linkedin.data.schema.PathSpec) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) PathSpec(com.linkedin.data.schema.PathSpec)

Example 5 with EnumDataSchema

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);
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) Test(org.testng.annotations.Test)

Aggregations

EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)21 DataSchema (com.linkedin.data.schema.DataSchema)11 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)10 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)7 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)7 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)7 DataMap (com.linkedin.data.DataMap)6 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)6 MapDataSchema (com.linkedin.data.schema.MapDataSchema)6 Test (org.testng.annotations.Test)6 Name (com.linkedin.data.schema.Name)5 ByteString (com.linkedin.data.ByteString)3 DataList (com.linkedin.data.DataList)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)2 StringDataSchema (com.linkedin.data.schema.StringDataSchema)2 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)2