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);
}
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;
}
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;
}
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);
}
}
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));
}
}
Aggregations