use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildArrayDataSchemaByProjection.
/**
* Build a new {@link ArrayDataSchema} schema that contains only the masked fields.
*/
private static ArrayDataSchema buildArrayDataSchemaByProjection(ArrayDataSchema originalSchema, DataMap maskMap) {
if (maskMap.containsKey(FilterConstants.WILDCARD)) {
DataSchema newItemsSchema = reuseOrBuildDataSchema(originalSchema.getItems(), maskMap.get(FilterConstants.WILDCARD));
ArrayDataSchema newSchema = new ArrayDataSchema(newItemsSchema);
if (originalSchema.getProperties() != null) {
newSchema.setProperties(originalSchema.getProperties());
}
return newSchema;
}
throw new IllegalArgumentException("Missing wildcard key in projection mask: " + maskMap.keySet());
}
use of com.linkedin.data.schema.ArrayDataSchema 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.ArrayDataSchema 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).getTypeByMemberKey((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.ArrayDataSchema in project rest.li by linkedin.
the class PdlSchemaParser method parseArray.
private ArrayDataSchema parseArray(ArrayDeclarationContext array) throws ParseException {
ArrayDataSchema schema = new ArrayDataSchema(toDataSchema(array.typeParams.items));
schema.setItemsDeclaredInline(isDeclaredInline(array.typeParams.items));
return schema;
}
use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class DynamicRecordTemplate method putArray.
/**
* Puts an array field value by doing the necessary unwrapping at the items level.
* @param field specifies the field to put the value for.
* @param fieldDef specifies the field definition to put the value for.
* @param value provides the value to put for the specified field.
* @param <T> provides the type of the value.
*/
@SuppressWarnings({ "unchecked" })
private <T> void putArray(RecordDataSchema.Field field, FieldDef<T> fieldDef, T value) {
DataList data = new DataList();
Class<?> itemType = null;
ArrayDataSchema arrayDataSchema = null;
if (fieldDef.getDataSchema() instanceof ArrayDataSchema) {
arrayDataSchema = (ArrayDataSchema) fieldDef.getDataSchema();
DataSchema itemSchema = arrayDataSchema.getItems();
if (itemSchema instanceof TyperefDataSchema) {
itemType = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(itemSchema.getDereferencedType());
} else {
itemType = fieldDef.getType().getComponentType();
}
} else {
throw new IllegalArgumentException("Field " + fieldDef.getName() + " does not have an array schema; although the data is an array.");
}
boolean isDataTemplate = DataTemplate.class.isAssignableFrom(itemType);
List<Object> items;
if (value instanceof DataList) {
items = (List<Object>) value;
} else {
items = Arrays.asList((Object[]) value);
}
for (Object item : items) {
if (isDataTemplate) {
Object itemData;
if (item instanceof DataMap) {
itemData = item;
} else {
itemData = ((DataTemplate) item).data();
}
data.add(itemData);
} else {
data.add(DataTemplateUtil.coerceInput(item, (Class<Object>) item.getClass(), itemType.isEnum() ? String.class : itemType));
}
}
putDirect(field, DataList.class, data, SetMode.DISALLOW_NULL);
}
Aggregations