Search in sources :

Example 1 with MapDataSchema

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

the class DataElementUtil method element.

/**
   * Get the {@link DataElement} by following the specified path starting from
   * the provided {@link DataElement}.
   *
   * @param element provides the {@link DataElement} that is the starting point.
   * @param path provides the path components through an {@link Iterable}.
   * @return the {@link DataElement} if the path can be followed, else return null.
   * @throws IllegalArgumentException if the provided path's syntax is incorrect, Data object does not match provided
   *                                  {@link DataSchema}.
   */
public static DataElement element(DataElement element, Iterable<Object> path) throws IllegalArgumentException {
    DataElement currentElement = element;
    for (Object component : path) {
        Object name;
        if (currentElement.getValue().getClass() == DataMap.class && component.getClass() != String.class) {
            name = component.toString();
        } else if (currentElement.getValue().getClass() == DataList.class && component.getClass() != Integer.class) {
            try {
                name = Integer.parseInt(component.toString());
            } catch (NumberFormatException e) {
                return null;
            }
        } else {
            name = component;
        }
        Object childValue = currentElement.getChild(name);
        if (childValue == null) {
            return null;
        }
        DataSchema childSchema = null;
        DataSchema schema = currentElement.getSchema();
        if (schema != null) {
            schema = schema.getDereferencedDataSchema();
            switch(schema.getType()) {
                case ARRAY:
                    childSchema = ((ArrayDataSchema) schema).getItems();
                    break;
                case MAP:
                    childSchema = ((MapDataSchema) schema).getValues();
                    break;
                case UNION:
                    childSchema = ((UnionDataSchema) schema).getType((String) name);
                    break;
                case RECORD:
                    RecordDataSchema.Field field = ((RecordDataSchema) schema).getField((String) name);
                    if (field != null) {
                        childSchema = field.getType();
                    }
                    break;
                default:
                    throw new IllegalArgumentException(currentElement.pathAsString() + " has schema of type " + schema.getType() + " which cannot have child, but has child with name \"" + name + "\"");
            }
        }
        currentElement = new SimpleDataElement(childValue, name, childSchema, currentElement);
    }
    return currentElement;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Example 2 with MapDataSchema

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

the class TestMap method testStringArrayMap.

@Test
public void testStringArrayMap() {
    TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new MapTest(), "stringArrayMap");
    @SuppressWarnings("unchecked") Class<StringArrayMap> templateClass = (Class<StringArrayMap>) fieldInfo.getFieldClass();
    MapDataSchema schema = (MapDataSchema) fieldInfo.getField().getType();
    Map<String, StringArray> input = new HashMap<String, StringArray>();
    for (int i = 0; i < 5; ++i) {
        String key = "input" + i;
        input.put(key, new StringArray());
        input.get(key).add("subinput" + i);
    }
    Map<String, StringArray> adds = new HashMap<String, StringArray>();
    for (int i = 0; i < 5; ++i) {
        String key = "add" + i;
        adds.put(key, new StringArray());
        adds.get(key).add("subadd" + i);
    }
    TestMapTemplate.testMap(templateClass, schema, input, adds);
}
Also used : HashMap(java.util.HashMap) MapDataSchema(com.linkedin.data.schema.MapDataSchema) Test(org.testng.annotations.Test)

Example 3 with MapDataSchema

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

the class TestMap method testIntegerMap.

@Test
public void testIntegerMap() {
    TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new MapTest(), "intMap");
    @SuppressWarnings("unchecked") Class<IntegerMap> templateClass = (Class<IntegerMap>) fieldInfo.getFieldClass();
    MapDataSchema schema = (MapDataSchema) fieldInfo.getField().getType();
    Map<String, Integer> input = asMap("one", 1, "three", 3, "five", 5, "seven", 7, "eleven", 11);
    Map<String, Integer> adds = asMap("thirteen", 13, "seventeen", 17, "nineteen", 19);
    TestMapTemplate.testMap(templateClass, schema, input, adds);
}
Also used : MapDataSchema(com.linkedin.data.schema.MapDataSchema) Test(org.testng.annotations.Test)

Example 4 with MapDataSchema

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

the class TestMap method testStringMapMap.

@Test
public void testStringMapMap() {
    TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new MapTest(), "stringMapMap");
    @SuppressWarnings("unchecked") Class<StringMapMap> templateClass = (Class<StringMapMap>) fieldInfo.getFieldClass();
    MapDataSchema schema = (MapDataSchema) fieldInfo.getField().getType();
    Map<String, StringMap> input = new HashMap<String, StringMap>();
    for (int i = 0; i < 5; ++i) {
        String key = "input" + i;
        input.put(key, new StringMap());
        input.get(key).put("subinput" + i, "subinputvalue" + i);
    }
    Map<String, StringMap> adds = new HashMap<String, StringMap>();
    for (int i = 0; i < 5; ++i) {
        String key = "add" + i;
        adds.put(key, new StringMap());
        adds.get(key).put("subadd" + i, "subaddvalue" + i);
    }
    TestMapTemplate.testMap(templateClass, schema, input, adds);
}
Also used : HashMap(java.util.HashMap) MapDataSchema(com.linkedin.data.schema.MapDataSchema) Test(org.testng.annotations.Test)

Example 5 with MapDataSchema

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

the class TestMap method testStringArrayMap.

@Test
public void testStringArrayMap() {
    TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new MapTest(), "stringArrayMap");
    @SuppressWarnings("unchecked") Class<StringArrayMap> templateClass = (Class<StringArrayMap>) fieldInfo.getFieldClass();
    MapDataSchema schema = (MapDataSchema) fieldInfo.getField().getType();
    Map<String, StringArray> input = new HashMap<String, StringArray>();
    for (int i = 0; i < 5; ++i) {
        String key = "input" + i;
        input.put(key, new StringArray());
        input.get(key).add("subinput" + i);
    }
    Map<String, StringArray> adds = new HashMap<String, StringArray>();
    for (int i = 0; i < 5; ++i) {
        String key = "add" + i;
        adds.put(key, new StringArray());
        adds.get(key).add("subadd" + i);
    }
    TestMapTemplate.testMap(templateClass, schema, input, adds);
}
Also used : TestDataTemplateUtil(com.linkedin.data.template.TestDataTemplateUtil) StringArrayMap(com.linkedin.data.template.StringArrayMap) HashMap(java.util.HashMap) MapDataSchema(com.linkedin.data.schema.MapDataSchema) StringArray(com.linkedin.data.template.StringArray) Test(org.testng.annotations.Test)

Aggregations

MapDataSchema (com.linkedin.data.schema.MapDataSchema)30 Test (org.testng.annotations.Test)17 DataMap (com.linkedin.data.DataMap)13 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)12 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)11 DataSchema (com.linkedin.data.schema.DataSchema)10 ByteString (com.linkedin.data.ByteString)9 DataList (com.linkedin.data.DataList)9 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)9 HashMap (java.util.HashMap)9 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)7 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)7 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)6 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)4 TestDataTemplateUtil (com.linkedin.data.template.TestDataTemplateUtil)4 Map (java.util.Map)3 TestUtil.asMap (com.linkedin.data.TestUtil.asMap)2 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)2 IntegerMap (com.linkedin.data.template.IntegerMap)2