Search in sources :

Example 11 with DataComplex

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);
}
Also used : DataComplex(com.linkedin.data.DataComplex) HashMap(java.util.HashMap) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 12 with DataComplex

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;
}
Also used : DataList(com.linkedin.data.DataList) DataComplex(com.linkedin.data.DataComplex) DataMap(com.linkedin.data.DataMap)

Aggregations

DataComplex (com.linkedin.data.DataComplex)12 DataMap (com.linkedin.data.DataMap)8 ByteString (com.linkedin.data.ByteString)5 DataList (com.linkedin.data.DataList)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 TestUtil.noCommonDataComplex (com.linkedin.data.TestUtil.noCommonDataComplex)2 URIDetails (com.linkedin.restli.internal.testutils.URIDetails)2 ListIterator (java.util.ListIterator)2 DataProvider (org.testng.annotations.DataProvider)2 TestUtil.asMap (com.linkedin.data.TestUtil.asMap)1 DataElement (com.linkedin.data.element.DataElement)1 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1 AbstractMap (java.util.AbstractMap)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1