use of com.linkedin.data.DataComplex in project rest.li by linkedin.
the class AbstractFilter method filterDataMap.
private Object filterDataMap(DataMap opNode, DataMap valueDataMap, NodeMode defaultMode, DataMap complexWildCard) {
assert opNode != null;
final Map<String, Object> result = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : valueDataMap.entrySet()) {
final String name = entry.getKey();
final Object childValue = entry.getValue();
// make sure that mask is of correct type if it is defined
if (!isValidMaskType(opNode.get(Escaper.replaceAll(name, "$", "$$")))) {
onError(name, "mask value for field %2$s should be of type Integer or DataMap, instead it is of type: %1$s, ", opNode.get(Escaper.replaceAll(name, "$", "$$")), name);
// in not fast-fail mode just skip this entry
continue;
}
Object operation = FilterConstants.POSITIVE;
// _explicitFieldMode can only have value of high priority: either
// show_high or hide_high
final NodeMode explicitFieldMode = getExplicitNodeMode(opNode, name);
if (explicitFieldMode != null) {
if (areFieldsExplicitlyRemoved(explicitFieldMode)) {
// if item was explicitly hidden, filter it out with all descendants
operation = FilterConstants.NEGATIVE;
} else if (complexWildCard != null) // apply complex wildcard if it was specified
// there is no need for further evaluation of mask, because this field
// was explicitly masked with positive mask
{
if (childValue instanceof DataComplex) {
final DataMap composed = compose(name, complexWildCard, wildcard(1));
if (composed != null) {
operation = composed;
}
}
// else
// data is of primitive type, and is selected with mask = 1, but there also
// exist
// a wildcard mask, in this case we don't report it as an error
}
} else {
// field was not explicitly masked
final Object opChild = opNode.get(Escaper.replaceAll(name, "$", "$$"));
if (opChild == null) {
// and $* if it was defined and field was not filtered out
if (areFieldsExplicitlyRemoved(defaultMode) || areFieldsImplicitlyRemoved(defaultMode, complexWildCard)) {
operation = FilterConstants.NEGATIVE;
} else if (complexWildCard != null) {
if (childValue instanceof DataComplex) {
operation = complexWildCard;
} else if (needsRemoving(defaultMode, complexWildCard)) {
operation = FilterConstants.NEGATIVE;
}
}
} else {
// precondition:
assert (opChild.getClass() == DataMap.class) : opChild;
final Object rawWildcard = opNode.get(FilterConstants.WILDCARD);
final DataMap effectiveComplexWildcard = ((rawWildcard != null && rawWildcard.equals(POSITIVE)) ? wildcard(POSITIVE) : (DataMap) rawWildcard);
// effectiveMask contains complex mask composed with wildcard if wildcard is
// defined
final DataMap effectiveMask = ((effectiveComplexWildcard == null) ? (DataMap) opChild : compose(name, (DataMap) opChild, effectiveComplexWildcard));
// 2. filter was complex
if (needsRemoving(defaultMode, effectiveMask)) {
operation = FilterConstants.NEGATIVE;
} else {
if (childValue instanceof DataComplex) {
operation = effectiveMask;
} else {
onError(name, "data is of primitive value: %1$s, but filter: %2$s is complex", childValue, opChild);
}
}
}
}
if (isValidDataMapFieldOperation(result, name, operation)) {
result.put(name, operation);
}
}
return onFilterDataMap(valueDataMap, result);
}
use of com.linkedin.data.DataComplex in project rest.li by linkedin.
the class CopyFilter method onFilterDataList.
@Override
protected Object onFilterDataList(DataList data, int start, int count, Object operation) {
if (operation == FilterConstants.NEGATIVE) {
return EMPTY_DATALIST;
}
final int end = Math.min(data.size(), start + count);
final int size = Math.max(end - start, 0);
final DataList resultList = new DataList(size);
for (int i = start; i < end; ++i) {
final Class<?> operationClass = operation.getClass();
final Object original = data.get(i);
final Object value;
if (operationClass == Integer.class) {
value = original;
} else {
assert (operationClass == DataMap.class);
assert (original instanceof DataComplex);
value = filter(original, (DataMap) operation);
}
CheckedUtil.addWithoutChecking(resultList, value);
}
return resultList;
}
Aggregations