use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildMapDataSchemaByProjection.
/**
* Build a new {@link MapDataSchema} schema that contains only the masked fields.
*/
private static MapDataSchema buildMapDataSchemaByProjection(MapDataSchema originalSchema, DataMap maskMap) {
if (maskMap.containsKey(FilterConstants.WILDCARD)) {
DataSchema newValuesSchema = reuseOrBuildDataSchema(originalSchema.getValues(), maskMap.get(FilterConstants.WILDCARD));
MapDataSchema newSchema = new MapDataSchema(newValuesSchema);
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.DataSchema in project rest.li by linkedin.
the class DataSchemaAnnotationValidator method validateSchema.
private final void validateSchema(ValidatorContext context, DataSchema schema) {
if (schema.getType() == DataSchema.Type.TYPEREF) {
DataSchema refSchema = ((TyperefDataSchema) schema).getRef();
validateSchema(context, refSchema);
getAndInvokeValidatorList(context, schema);
} else {
getAndInvokeValidatorList(context, schema);
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class DataSchemaAnnotationValidator method buildSchemaValidators.
/**
* Build a cache of {@link Validator}s declared for the specified schema.
*
* @param schema to cache {@link Validator}s for.
* @return the cache if successful.
*/
private IdentityHashMap<Object, List<Validator>> buildSchemaValidators(DataSchema schema) {
final IdentityHashMap<Object, List<Validator>> map = new IdentityHashMap<Object, List<Validator>>();
DataSchemaTraverse traverse = new DataSchemaTraverse();
traverse.traverse(schema, new DataSchemaTraverse.Callback() {
@Override
public void callback(List<String> path, DataSchema schema) {
List<Validator> validatorList = map.get(schema);
if (validatorList == null) {
Object validateObject = schema.getProperties().get(VALIDATE);
if (validateObject == null) {
validatorList = NO_VALIDATORS;
} else {
validatorList = buildValidatorList(validateObject, path, schema);
}
map.put(schema, validatorList);
if (schema.getType() == DataSchema.Type.RECORD) {
RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
validateObject = field.getProperties().get(VALIDATE);
if (validateObject == null) {
validatorList = NO_VALIDATORS;
} else {
path.add(field.getName());
validatorList = buildValidatorList(validateObject, path, field);
path.remove(path.size() - 1);
}
map.put(field, validatorList);
}
}
}
}
});
return map;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class DataTemplateUtil method getSchema.
/**
* Gets the data schema for a given java type.
*
* @param type to get a schema for. Has to be primitive or a generated data template.
* @throws TemplateRuntimeException if the {@link DataSchema} for the specified type cannot be provided.
*/
public static DataSchema getSchema(Class<?> type) throws TemplateRuntimeException {
final DataSchema primitiveSchema = DataSchemaUtil.classToPrimitiveDataSchema(type);
if (primitiveSchema != null) {
return primitiveSchema;
}
try {
Field schemaField = type.getDeclaredField(SCHEMA_FIELD_NAME);
schemaField.setAccessible(true);
DataSchema schema = (DataSchema) schemaField.get(null);
if (schema == null) {
throw new TemplateRuntimeException("Schema field is not set in class: " + type.getName());
}
return schema;
} catch (IllegalAccessException e) {
throw new TemplateRuntimeException("Error accessing schema field in class: " + type.getName(), e);
} catch (NoSuchFieldException e) {
throw new TemplateRuntimeException("Error accessing schema field in class: " + type.getName(), e);
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestDataIterator method testDataSchemaNameEquals.
@Test(dataProvider = "orders")
public void testDataSchemaNameEquals(IterationOrder order) throws IOException {
String input = "{ \"a\" : 222, \"b\" : \"foo\", \"c\" : { \"a\" : 333, \"e\" : { \"b\" : \"bar\", \"d\" : [ 0, 1, 2 ], \"f\" : \"WXYZ\", \"g\" : \"ORANGE\" }, \"f\" : \"wxyz\" }, \"d\" : [ 10, 11, 12 ], \"g\" : \"APPLE\" }";
String schemaText = "{\n" + " \"name\" : \"foo\",\n" + " \"type\" : \"record\",\n" + " \"fields\" : [\n" + " { \"name\" : \"a\", \"type\" : \"int\" },\n" + " { \"name\" : \"b\", \"type\" : \"string\" },\n" + " { \"name\" : \"c\", \"type\" : \"foo\" },\n" + " { \"name\" : \"d\", \"type\" : { \"type\" : \"array\", \"items\" : \"int\" } },\n" + " { \"name\" : \"e\", \"type\" : \"foo\" },\n" + " { \"name\" : \"f\", \"type\" : { \"name\" : \"xx.F\", \"type\" : \"fixed\", \"size\" : 4 } },\n" + " { \"name\" : \"g\", \"type\" : { \"name\" : \"yy.G\", \"type\" : \"enum\", \"symbols\" : [ \"APPLE\", \"BANANA\", \"ORANGE\" ] } }\n" + " ]\n" + "}\n";
DataSchema schema = DataTemplateUtil.parseSchema(schemaText);
Object[][] tests = { { "xx.F", Arrays.asList("WXYZ", "wxyz") }, { "yy.G", Arrays.asList("ORANGE", "APPLE") } };
Object o = jsonToObject(input);
for (Object[] row : tests) {
Builder builder = Builder.create(o, schema, order).filterBy(dataSchemaNameEquals((String) row[0]));
@SuppressWarnings("unchecked") List<Object> expected = (List<Object>) row[1];
assertEqualsByValue(builder, expected);
}
}
Aggregations