Search in sources :

Example 6 with EnumDataSchema

use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.

the class TestDataElement method testDataElement.

@Test(dataProvider = "dataElementFactories")
public void testDataElement(DataElementFactory factory) throws IOException {
    RecordDataSchema fooSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(fooSchemaText);
    ArrayDataSchema arraySchema = (ArrayDataSchema) fooSchema.getField("array").getType();
    String fooText = " {" + "   \"int\" : 34," + "   \"string\" : \"abc\"," + "   \"array\" : [" + "     { \"int\" : 56 }," + "     { \"string\" : \"xyz\" }," + "     { \"array\" : [" + "       { \"int\" : 78 }" + "     ] }" + "   ]," + "   \"enumField\": \"SYMBOL1\"," + "   \"unionField\": {" + "       \"string\":\"unionString\"" + "   }," + "   \"mapField\": {" + "    \"key1\":\"value1\"," + "    \"key2\":\"value2\"" + "   }," + "   \"typeRefField\": \"42\"," + "   \"typeRefFieldToMap\": {" + "    \"key1\":\"value1\"," + "    \"key2\":\"value2\"" + "   }," + "   \"unionWithAliasesField\" : {" + "       \"stringFieldInUnionWithAliases\" : \"stringInUnionWithAlias\"" + "   }" + " }";
    DataMap foo = TestUtil.dataMapFromString(fooText);
    DataElement root = factory.create(foo, DataElement.ROOT_NAME, fooSchema, null);
    DataElement int1 = factory.create(foo.get("int"), "int", fooSchema.getField("int").getType(), root);
    DataElement string1 = factory.create(foo.get("string"), "string", fooSchema.getField("string").getType(), root);
    DataElement array1 = factory.create(foo.get("array"), "array", fooSchema.getField("array").getType(), root);
    MapDataSchema mapDataSchema = (MapDataSchema) fooSchema.getField("mapField").getType();
    StringDataSchema stringDataSchema = (StringDataSchema) mapDataSchema.getValues();
    DataElement mapFieldElement = factory.create(foo.get("mapField"), "mapField", mapDataSchema, root);
    DataElement mapValueInMapField = factory.create(mapFieldElement.getChild("key1"), "key1", stringDataSchema, mapFieldElement);
    EnumDataSchema enumDataSchema = (EnumDataSchema) fooSchema.getField("enumField").getType();
    DataElement enumField = factory.create(foo.get("enumField"), "enumField", enumDataSchema, root);
    DataElement unionField = factory.create(foo.get("unionField"), "unionField", fooSchema.getField("unionField").getType(), root);
    UnionDataSchema unionFieldSchema = (UnionDataSchema) fooSchema.getField("unionField").getType();
    DataElement unionFieldString = factory.create(unionField.getChild("string"), "string", unionFieldSchema.getTypeByMemberKey("string"), unionField);
    TyperefDataSchema typerefDataSchema = (TyperefDataSchema) fooSchema.getField("typeRefField").getType();
    DataElement typeRefField = factory.create(foo.get("typeRefField"), "typeRefField", typerefDataSchema, root);
    TyperefDataSchema typeRefToMapDataSchema = (TyperefDataSchema) fooSchema.getField("typeRefFieldToMap").getType();
    DataElement typeRefToMapField = factory.create(foo.get("typeRefFieldToMap"), "typeRefFieldToMap", typeRefToMapDataSchema, root);
    DataElement mapValueInTypeReffedMapField = factory.create(typeRefToMapField.getChild("key1"), "key1", stringDataSchema, typeRefToMapField);
    DataElement unionWithAliasesField = factory.create(foo.get("unionWithAliasesField"), "unionWithAliasesField", fooSchema.getField("unionWithAliasesField").getType(), root);
    UnionDataSchema unionWithAliasesSchema = (UnionDataSchema) fooSchema.getField("unionWithAliasesField").getType();
    DataElement stringFieldInUnionWithAliases = factory.create(unionWithAliasesField.getChild("stringFieldInUnionWithAliases"), "stringFieldInUnionWithAliases", unionWithAliasesSchema.getTypeByMemberKey("stringFieldInUnionWithAliases"), unionWithAliasesField);
    DataElement foo20 = factory.create(array1.getChild(0), 0, arraySchema.getItems(), array1);
    DataElement foo21 = factory.create(array1.getChild(1), 1, arraySchema.getItems(), array1);
    DataElement foo22 = factory.create(array1.getChild(2), 2, arraySchema.getItems(), array1);
    DataElement int20 = factory.create(foo20.getChild("int"), "int", fooSchema.getField("int").getType(), foo20);
    DataElement string21 = factory.create(foo21.getChild("string"), "string", fooSchema.getField("string").getType(), foo21);
    DataElement array22 = factory.create(foo22.getChild("array"), "array", fooSchema.getField("array").getType(), foo22);
    DataElement foo30 = factory.create(array22.getChild(0), 0, arraySchema.getItems(), array22);
    DataElement int30 = factory.create(foo30.getChild("int"), "int", fooSchema.getField("int").getType(), foo30);
    // test path
    Object[][] testPathInput = { { root, foo, fooSchema, new Object[] {}, "" }, { int1, foo.get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "int" }, "/int" }, { string1, foo.get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "string" }, "/string" }, { array1, foo.get("array"), arraySchema, new Object[] { "array" }, "/array" }, { mapFieldElement, foo.get("mapField"), mapDataSchema, new Object[] { "mapField" }, "/mapField" }, { mapValueInMapField, ((DataMap) foo.get("mapField")).get("key1"), stringDataSchema, new Object[] { "mapField", "key1" }, "/mapField/*" }, { mapValueInTypeReffedMapField, ((DataMap) foo.get("typeRefFieldToMap")).get("key1"), stringDataSchema, new Object[] { "typeRefFieldToMap", "key1" }, "/typeRefFieldToMap/*" }, { enumField, foo.get("enumField"), enumDataSchema, new Object[] { "enumField" }, "/enumField" }, { unionFieldString, ((DataMap) foo.get("unionField")).get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "unionField", "string" }, "/unionField/string" }, { typeRefField, foo.get("typeRefField"), typerefDataSchema, new Object[] { "typeRefField" }, "/typeRefField" }, { stringFieldInUnionWithAliases, ((DataMap) foo.get("unionWithAliasesField")).get("stringFieldInUnionWithAliases"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "unionWithAliasesField", "stringFieldInUnionWithAliases" }, "/unionWithAliasesField/stringFieldInUnionWithAliases" }, { foo20, ((DataList) foo.get("array")).get(0), fooSchema, new Object[] { "array", 0 }, "/array/*" }, { foo21, ((DataList) foo.get("array")).get(1), fooSchema, new Object[] { "array", 1 }, "/array/*" }, { foo22, ((DataList) foo.get("array")).get(2), fooSchema, new Object[] { "array", 2 }, "/array/*" }, { int20, ((DataMap) ((DataList) foo.get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 0, "int" }, "/array/*/int" }, { string21, ((DataMap) ((DataList) foo.get("array")).get(1)).get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "array", 1, "string" }, "/array/*/string" }, { array22, ((DataMap) ((DataList) foo.get("array")).get(2)).get("array"), arraySchema, new Object[] { "array", 2, "array" }, "/array/*/array" }, { foo30, ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0), fooSchema, new Object[] { "array", 2, "array", 0 }, "/array/*/array/*" }, { int30, ((DataMap) ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 2, "array", 0, "int" }, "/array/*/array/*/int" } };
    ArrayList<Object> pathAsList = new ArrayList<>();
    for (Object[] row : testPathInput) {
        DataElement element = (DataElement) row[0];
        // test value
        Object expectedValue = row[1];
        assertSame(expectedValue, element.getValue());
        // test schema
        DataSchema expectedSchema = (DataSchema) row[2];
        assertSame(expectedSchema, element.getSchema());
        // test name
        Object[] expectedPath = (Object[]) row[3];
        Object expectedName = expectedPath.length == 0 ? DataElement.ROOT_NAME : expectedPath[expectedPath.length - 1];
        assertEquals(expectedName, element.getName());
        // test path
        Object[] path = element.path();
        element.pathAsList(pathAsList);
        StringBuilder builder = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        assertEquals(expectedPath.length, path.length);
        assertEquals(expectedPath.length, pathAsList.size());
        for (int i = 0; i < expectedPath.length; i++) {
            assertEquals(path[i], expectedPath[i]);
            assertEquals(pathAsList.get(i), expectedPath[i]);
            builder.append('*').append(expectedPath[i]);
            builder2.append(DataElement.SEPARATOR).append(expectedPath[i]);
        }
        assertEquals(builder.toString(), element.pathAsString('*'));
        assertEquals(builder2.toString(), element.pathAsString());
        // test copyChain
        DataElement copy = element.copyChain();
        assertElementChainEquals(copy, element.copyChain(), null);
        // test DataElementUtil.element
        DataElement elementFromUtil = DataElementUtil.element(root, path);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root, pathAsList);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root, element.pathAsString());
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root, element.pathAsString('*'), '*');
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), path);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), pathAsList);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString());
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString('*'), '*');
        assertElementChainEquals(elementFromUtil, element, root);
        // test pathSpec
        String pathSpecString = (String) row[4];
        assertEquals(element.getSchemaPathSpec().toString(), pathSpecString);
    }
}
Also used : MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayList(java.util.ArrayList) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) DataMap(com.linkedin.data.DataMap) StringDataSchema(com.linkedin.data.schema.StringDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) StringDataSchema(com.linkedin.data.schema.StringDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) DataList(com.linkedin.data.DataList) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Test(org.testng.annotations.Test)

Example 7 with EnumDataSchema

use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.

the class TestEnum method testEnum.

private static <T extends Enum<T>> void testEnum(Class<T> enumClass) {
    try {
        assertTrue(Enum.class.isAssignableFrom(enumClass));
        // has embedded EnumDataSchema
        DataSchema schema = DataTemplateUtil.getSchema(enumClass);
        assertNotNull(schema);
        assertTrue(schema instanceof EnumDataSchema);
        // get symbols
        EnumDataSchema enumSchema = (EnumDataSchema) schema;
        Set<String> schemaSymbols = new HashSet<>(enumSchema.getSymbols());
        assertNotNull(schemaSymbols);
        for (String symbol : schemaSymbols) {
            // IllegalArgumentException thrown if not valid symbol
            Enum.valueOf(enumClass, symbol);
        }
        // IllegalArgumentException thrown if not valid symbol
        Enum.valueOf(enumClass, "$UNKNOWN");
    } catch (Exception exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) HashSet(java.util.HashSet)

Example 8 with EnumDataSchema

use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.

the class TemplateSpecGenerator method generateNamedSchema.

private ClassTemplateSpec generateNamedSchema(NamedDataSchema schema) {
    pushCurrentLocation(_schemaResolver.nameToDataSchemaLocations().get(schema.getFullName()));
    // make sure no duplicate template spec classname which should be binding name of the schema
    final String className = schema.getBindingName();
    checkForClassNameConflict(className, schema);
    final ClassTemplateSpec templateClass;
    switch(schema.getType()) {
        case RECORD:
            templateClass = generateRecord((RecordDataSchema) schema);
            break;
        case ENUM:
            templateClass = generateEnum((EnumDataSchema) schema);
            break;
        case FIXED:
            templateClass = generateFixed((FixedDataSchema) schema);
            break;
        default:
            throw unrecognizedSchemaType(null, null, schema);
    }
    popCurrentLocation();
    return templateClass;
}
Also used : ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema)

Example 9 with EnumDataSchema

use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.

the class CompoundKey method fromValues.

/**
 * Initialize and return a CompoundKey.
 *
 * @param fieldValues DataMap representing the CompoundKey
 * @param fieldTypes Map of key name to it's {@link TypeInfo} (also accepts Class<?> for backward compatibility).
 * @return a CompoundKey
 */
public static CompoundKey fromValues(DataMap fieldValues, Map<String, CompoundKey.TypeInfo> fieldTypes) {
    CompoundKey result = new CompoundKey();
    for (Map.Entry<String, Object> entry : fieldValues.entrySet()) {
        Object fieldType = fieldTypes.get(entry.getKey());
        TypeInfo typeInfo = (TypeInfo) fieldType;
        DataSchema declaredSchema = typeInfo.getDeclared().getSchema();
        Class<?> declaredType;
        if (declaredSchema.getType() == DataSchema.Type.TYPEREF) {
            if (declaredSchema.getDereferencedDataSchema().isPrimitive()) {
                declaredType = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(declaredSchema.getDereferencedType());
            } else if (declaredSchema.getDereferencedType() == DataSchema.Type.ENUM) {
                try {
                    // there is no direct way to access the dereferenced class from TyperefInfo
                    declaredType = Class.forName(((EnumDataSchema) declaredSchema.getDereferencedDataSchema()).getFullName());
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new IllegalArgumentException("Compound key type must dereference to a primitive type or enum.");
            }
        } else {
            declaredType = typeInfo.getDeclaredType();
        }
        Object value = entry.getValue();
        if (!value.getClass().equals(declaredType)) {
            if (value.getClass().equals(String.class)) {
                // we coerce Strings to the dereferenced class
                value = ValueConverter.coerceString((String) value, declaredType);
            } else {
                throw new IllegalArgumentException("Value " + value + " is not a String or an object of " + declaredType.getSimpleName());
            }
        }
        value = DataTemplateUtil.coerceOutput(value, typeInfo.getBindingType());
        result.append(entry.getKey(), value, typeInfo);
    }
    return result;
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 10 with EnumDataSchema

use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.

the class TestActionArgumentBuilder method getEnumParams.

private List<Parameter<?>> getEnumParams() {
    EnumDataSchema simpleEnumSchema = new EnumDataSchema(new Name("com.linkedin.restli.common.test.SimpleEnum"));
    simpleEnumSchema.setSymbols(Arrays.asList("A", "B", "C"), null);
    return Collections.singletonList(new Parameter<>("simpleEnum", SimpleEnum.class, simpleEnumSchema, false, null, Parameter.ParamType.POST, true, new AnnotationSet(new Annotation[] {})));
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) SimpleEnum(com.linkedin.restli.common.test.SimpleEnum) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Name(com.linkedin.data.schema.Name)

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