Search in sources :

Example 86 with DataSchema

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());
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema)

Example 87 with DataSchema

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);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema)

Example 88 with DataSchema

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;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) MessageList(com.linkedin.data.message.MessageList) ArrayList(java.util.ArrayList) List(java.util.List) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse)

Example 89 with DataSchema

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);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Field(java.lang.reflect.Field)

Example 90 with DataSchema

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);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) DataList(com.linkedin.data.DataList) ArrayList(java.util.ArrayList) List(java.util.List) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)131 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)82 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)53 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)48 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)44 DataMap (com.linkedin.data.DataMap)43 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)41 MapDataSchema (com.linkedin.data.schema.MapDataSchema)40 Test (org.testng.annotations.Test)37 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)36 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)24 ByteString (com.linkedin.data.ByteString)15 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)15 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)14 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)14 ArrayList (java.util.ArrayList)12 DataList (com.linkedin.data.DataList)11 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)9 SchemaParser (com.linkedin.data.schema.SchemaParser)9 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)9