use of com.linkedin.data.schema.DataSchema 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.DataSchema in project rest.li by linkedin.
the class DataSchemaAnnotationValidator method validate.
@Override
public void validate(ValidatorContext context) {
DataElement element = context.dataElement();
DataSchema schema = element.getSchema();
if (schema != null) {
validateSchema(context, schema);
}
DataElement parentElement = element.getParent();
if (parentElement != null) {
DataSchema parentSchema = parentElement.getSchema();
// validators.
if (parentSchema != null && parentSchema.getType() == DataSchema.Type.RECORD) {
Object name = element.getName();
if (name.getClass() == String.class) {
RecordDataSchema recordDataSchema = (RecordDataSchema) parentSchema;
RecordDataSchema.Field field = recordDataSchema.getField((String) name);
if (field != null) {
getAndInvokeValidatorList(context, field);
}
}
}
}
}
use of com.linkedin.data.schema.DataSchema 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);
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class Filters method removeByPredicate.
/**
* Remove parts of a {@link NamedDataSchema} that matches the specified predicate.
*
* This method obtains a {@link DataMap} representation of the {@link NamedDataSchema}
* by invoking {@link com.linkedin.data.schema.util.Conversions#dataSchemaToDataMap}.
* Then it performs an pre-order traversal of this {@link DataMap} and evaluates the
* provided predicate on each Data object visited. If the predicate evaluates to true, the
* matching Data object will be removed. After the {@link DataMap} has been traversed and
* matching Data objects have been removed, the provided {@link SchemaParser} will be used
* to parse the JSON representation of the filtered {@link DataMap}. If there are no
* parsing errors, this method returns the {@link NamedDataSchema} parsed from the JSON representation.
*
* If there are parsing errors, the errors may be obtained from the provided {@link SchemaParser}.
*
* @param schema provides the {@link NamedDataSchema} to be filtered.
* @param predicate provides the {@link Predicate} to be evaluated.
* @param parser provides the {@link SchemaParser} to be used to parse the filtered {@link DataMap}.
* @return a filtered {@link NamedDataSchema} if the filtered schema is valid, else return null.
*/
public static NamedDataSchema removeByPredicate(NamedDataSchema schema, Predicate predicate, PegasusSchemaParser parser) {
DataMap schemaAsDataMap = dataSchemaToDataMap(schema);
DataMap map = (DataMap) Builder.create(schemaAsDataMap, null, IterationOrder.PRE_ORDER).filterBy(predicate).remove();
if (map == null) {
parser.errorMessageBuilder().append(NO_SCHEMA_LEFT);
}
DataSchema resultDataSchema = dataMapToDataSchema(map, parser);
if (resultDataSchema == null) {
parser.errorMessageBuilder().append(INVALID_SCHEMA_LEFT + map);
}
return (NamedDataSchema) resultDataSchema;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestAbstractGenerator method verify.
private void verify(String[] args, Map.Entry<File, Map.Entry<String, String>> entry, Exception exc, File targetDir, boolean debug) throws IOException {
if (debug) {
out.println(entry);
if (exc != null)
out.println(exc);
}
String pdscFileName = (entry.getValue().getKey());
if (_expectedSchemas.contains(pdscFileName)) {
File expectedOutputFile = schemaOutputFile(targetDir.getCanonicalPath(), entry.getValue().getValue());
assertTrue(expectedOutputFile.exists());
FileInputStream is = new FileInputStream(expectedOutputFile);
byte[] bytes = new byte[is.available()];
try {
is.read(bytes);
} finally {
is.close();
}
String fileSchemaText = new String(bytes);
DataSchema fileSchema = DataTemplateUtil.parseSchema(fileSchemaText);
assertTrue(fileSchema instanceof NamedDataSchema);
// run the generator again
// verify that output file has not changed
// test up-to-date
long beforeLastModified = expectedOutputFile.lastModified();
TestGenerator.main(args, debug);
long afterLastModified = expectedOutputFile.lastModified();
assertEquals(beforeLastModified, afterLastModified, expectedOutputFile.getPath());
} else if (_badPegasusSchemas.containsKey(pdscFileName)) {
assertTrue(exc != null);
String message = exc.getMessage();
assertTrue(message.contains(_badPegasusSchemas.get(pdscFileName)));
}
}
Aggregations