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)));
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestDataSchemaResolver method testClasspathResourceDataSchemaResolver.
@Test
public void testClasspathResourceDataSchemaResolver() {
final ClasspathResourceDataSchemaResolver resolver = new ClasspathResourceDataSchemaResolver(SchemaParserFactory.instance());
final PegasusSchemaParser parser = new SchemaParser(resolver);
final String existingSchemaName = "com.linkedin.data.schema.ValidationDemo";
final String nonExistSchemaName = "Non-Existing Schema";
final DataSchema existSchema = parser.lookupName(existingSchemaName);
assertNotNull(existSchema);
assertTrue(existSchema instanceof RecordDataSchema);
assertEquals(((RecordDataSchema) existSchema).getFullName(), existingSchemaName);
final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
assertNull(nonExistSchema);
assertTrue(parser.errorMessage().contains(nonExistSchemaName));
}
Aggregations