Search in sources :

Example 1 with MaskComposition

use of com.linkedin.data.transform.filter.MaskComposition in project rest.li by linkedin.

the class MaskTree method addOperation.

/**
 * Add an operation to this {@link MaskTree}, given a path indicating the field to which the operation
 * applies and a {@link MaskOperation} representing the operation to be applied.
 * @param path the path of the field to which the operation applies
 * @param op the MaskOperation to be performed on the field
 */
public void addOperation(PathSpec path, MaskOperation op) {
    List<String> segments = path.getPathComponents();
    Map<String, Object> attributes = path.getPathAttributes();
    final DataMap fieldMask = new DataMap();
    // map variable contains DataMap, into which current segment will be put
    DataMap map = fieldMask;
    for (int ii = 0; ii < segments.size() - 1; ++ii) {
        String segment = Escaper.escapePathSegment(segments.get(ii));
        DataMap childMap = new DataMap();
        map.put(segment, childMap);
        map = childMap;
    }
    String lastSegment = Escaper.escapePathSegment(segments.get(segments.size() - 1));
    Object start = attributes.get(PathSpec.ATTR_ARRAY_START);
    Object count = attributes.get(PathSpec.ATTR_ARRAY_COUNT);
    if (start != null || count != null) {
        DataMap childMap = new DataMap();
        map.put(lastSegment, childMap);
        if (start != null) {
            childMap.put(FilterConstants.START, start);
        }
        if (count != null) {
            childMap.put(FilterConstants.COUNT, count);
        }
    } else {
        map.put(lastSegment, op.getRepresentation());
    }
    // compose existing tree with mask for specific field
    try {
        new DataComplexProcessor(new MaskComposition(), fieldMask, _representation).run(false);
    } catch (DataProcessingException e) {
        throw new IllegalStateException("error while building mask tree", e);
    }
}
Also used : MaskComposition(com.linkedin.data.transform.filter.MaskComposition) DataComplexProcessor(com.linkedin.data.transform.DataComplexProcessor) DataMap(com.linkedin.data.DataMap) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Aggregations

DataMap (com.linkedin.data.DataMap)1 DataComplexProcessor (com.linkedin.data.transform.DataComplexProcessor)1 DataProcessingException (com.linkedin.data.transform.DataProcessingException)1 MaskComposition (com.linkedin.data.transform.filter.MaskComposition)1