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