Search in sources :

Example 11 with DataSchema

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);
                }
            }
        }
    }
}
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) DataElement(com.linkedin.data.element.DataElement) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 12 with DataSchema

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

Example 13 with DataSchema

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;
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Conversions.dataMapToDataSchema(com.linkedin.data.schema.util.Conversions.dataMapToDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataMap(com.linkedin.data.DataMap) Conversions.dataSchemaToDataMap(com.linkedin.data.schema.util.Conversions.dataSchemaToDataMap)

Example 14 with DataSchema

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)));
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 15 with DataSchema

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));
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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