Search in sources :

Example 31 with DataSchema

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

Example 32 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 33 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 34 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 35 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)

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