Search in sources :

Example 11 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class RestUtils method trimRecordTemplate.

/**
   * This method recursively removes all values from a DataMap
   * that do not match some field in the schema via an all positive
   * projection generated from the schema unless whitelisted by the
   * override parameter.
   *
   * Readonly datamaps should not invoke this.
   * An exception will be thrown if this is done.
   *
   * @param dataMap represents the DataMap that will be trimmed.
   * @param schema represents the schema that the datamap should match.
   * @param override represents the MaskTree that defines how fields outside the schema should be preserved.
   * @param failOnMismatch true if an exception should be thrown if there is a data-schema mismatch.
   */
public static void trimRecordTemplate(DataMap dataMap, RecordDataSchema schema, MaskTree override, final boolean failOnMismatch) {
    if (dataMap == null || schema == null) {
        return;
    }
    DataMap overrideResults = RestUtils.projectFields(dataMap, ProjectionMode.AUTOMATIC, override);
    Builder.create(dataMap, schema, IterationOrder.PRE_ORDER).filterBy(new Predicate() {

        @Override
        public boolean evaluate(DataElement element) {
            if (failOnMismatch && element.getSchema() == null) {
                throw new IllegalArgumentException("Schema validation did not succeed: " + element.getValue().toString());
            }
            return element.getSchema() == null;
        }
    }).remove();
    CheckedUtil.putAllWithoutChecking(dataMap, overrideResults);
}
Also used : DataElement(com.linkedin.data.element.DataElement) DataMap(com.linkedin.data.DataMap) Predicate(com.linkedin.data.it.Predicate)

Example 12 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class RestLiDataValidator method checkNewRecordsAreNotMissingFields.

private ValidationResult checkNewRecordsAreNotMissingFields(RecordTemplate entity, MessageList<Message> messages) {
    for (Message message : messages) {
        Object[] path = message.getPath();
        if (path[path.length - 1].toString().equals(PatchConstants.SET_COMMAND)) {
            // Replace $set with the field name to get the full path
            path[path.length - 1] = message.getFormat();
            DataElement element = DataElementUtil.element(new SimpleDataElement(entity.data(), entity.schema()), path);
            ValidationResult result = ValidateDataAgainstSchema.validate(element, new ValidationOptions());
            if (!result.isValid()) {
                return result;
            }
        }
    }
    return null;
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) Message(com.linkedin.data.message.Message) SimpleDataElement(com.linkedin.data.element.SimpleDataElement) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions)

Example 13 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class Remover method remove.

/**
   * Removes the Data objects returned by the {@link DataIterator}.
   * This method mutates the Data object and it's descendants.
   *
   * @param root provides the root containing the Data objects that will be removed.
   * @param it provides the iterator of Data objects to be removed.
   * @return null if the input Data object is removed, else return the input root.
   */
public static Object remove(Object root, DataIterator it) {
    DataElement element;
    // construct the list of Data objects to remove
    // don't remove in place because iterator behavior with removals while iterating is undefined
    ArrayList<ToRemove> removeList = new ArrayList<ToRemove>();
    while ((element = it.next()) != null) {
        ToRemove toRemove = new ToRemove(element);
        removeList.add(toRemove);
    }
    // perform actual removal in reverse order to make sure deleting array elements starts with higher indices
    for (int i = removeList.size() - 1; i >= 0; i--) {
        ToRemove toRemove = removeList.get(i);
        if (toRemove.isRoot()) {
            root = null;
        } else {
            toRemove.remove();
        }
    }
    return root;
}
Also used : DataElement(com.linkedin.data.element.DataElement) ArrayList(java.util.ArrayList)

Example 14 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class Builder method iterate.

public void iterate(Callback callback) {
    DataIterator it = dataIterator();
    DataElement element;
    while ((element = it.next()) != null) {
        callback.callback(element);
    }
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement)

Example 15 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class StrlenValidator method validate.

@Override
public void validate(ValidatorContext ctx) {
    DataElement element = ctx.dataElement();
    Object value = element.getValue();
    String str = String.valueOf(value);
    int strlen = str.length();
    if ((strlen < _min) || (strlen > _max)) {
        ctx.addResult(new Message(element.path(), "length of \"%1$s\" is out of range %2$d...%3$d", str, _min, _max));
    }
}
Also used : DataElement(com.linkedin.data.element.DataElement) Message(com.linkedin.data.message.Message)

Aggregations

DataElement (com.linkedin.data.element.DataElement)20 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)11 ByteString (com.linkedin.data.ByteString)7 Test (org.testng.annotations.Test)7 DataMap (com.linkedin.data.DataMap)5 Message (com.linkedin.data.message.Message)5 DataSchema (com.linkedin.data.schema.DataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 ArrayList (java.util.ArrayList)4 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)3 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)2 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 DataComplex (com.linkedin.data.DataComplex)1 TestUtil.asList (com.linkedin.data.TestUtil.asList)1 Predicate (com.linkedin.data.it.Predicate)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 CoercionMode (com.linkedin.data.schema.validation.CoercionMode)1 RequiredMode (com.linkedin.data.schema.validation.RequiredMode)1