Search in sources :

Example 31 with DataMap

use of com.linkedin.data.DataMap in project rest.li by linkedin.

the class QueryParamsDataMap method queryString.

/**
   * Create a map of query string parameters (name, value) from the provided DataMap, in
   * the same manner as parseDataMapKeys() below created a DataMap from a map of query
   * parameters.
   *
   * @param dataMap a dataMap
   * @return the map of query string parameters.
   */
public static Map<String, List<String>> queryString(DataMap dataMap) {
    Map<String, List<String>> result = new HashMap<String, List<String>>();
    DataMap processedDataMap = processProjections(dataMap, result);
    iterate("", processedDataMap, result);
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataList(com.linkedin.data.DataList) List(java.util.List) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap)

Example 32 with DataMap

use of com.linkedin.data.DataMap in project rest.li by linkedin.

the class QueryParamsDataMap method convertToDataCollection.

/**
   * The method recursively traverses the input Map and transforms it as follows:
   * - wherever encounters instances of ListMap (Map<Integer,Object>), converts
   *   those to DataList, preserving key order but ignoring any "holes" in key sequences.
   * - wherever encounters instances of MapMap (Map<String,Object>) converts them
   *   into DataMap.
   *
   * This is done since while parsing out indexed query parameters it's convenient to
   * parse them into a map due to arbitrary order in which they may appear, while if they
   * are defined in the schema as a list, a DataList is expected during validation.
   *
   * @param map the Map to transform
   * @return DataMap or DataList, depending on the type of the input Map
   */
private static DataComplex convertToDataCollection(Map<?, ?> map) {
    // recursively on every value that is itself a map
    if (map instanceof MapMap) {
        DataMap result = new DataMap();
        MapMap mapMap = (MapMap) map;
        for (Entry<String, Object> entry : mapMap.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof Map<?, ?>)
                value = convertToDataCollection((Map<?, ?>) value);
            result.put(entry.getKey(), value);
        }
        return result;
    }
    // convert this map into a list preserving key order.
    if (map instanceof ListMap) {
        DataList result = new DataList();
        ListMap listMap = (ListMap) map;
        List<Integer> sortedKeys = new ArrayList<Integer>(listMap.keySet());
        Collections.sort(sortedKeys);
        for (Integer key : sortedKeys) {
            Object object = map.get(key);
            if (object instanceof Map<?, ?>)
                object = convertToDataCollection((Map<?, ?>) object);
            result.add(object);
        }
        return result;
    }
    throw new IllegalArgumentException("Only MapMap or ListMap input argument types are allowed");
}
Also used : MapMap(com.linkedin.restli.internal.common.PathSegment.MapMap) DataList(com.linkedin.data.DataList) ArrayList(java.util.ArrayList) ByteString(com.linkedin.data.ByteString) HashMap(java.util.HashMap) ListMap(com.linkedin.restli.internal.common.PathSegment.ListMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) MapMap(com.linkedin.restli.internal.common.PathSegment.MapMap) ListMap(com.linkedin.restli.internal.common.PathSegment.ListMap) DataMap(com.linkedin.data.DataMap)

Example 33 with DataMap

use of com.linkedin.data.DataMap in project rest.li by linkedin.

the class QueryParamsDataMap method processIndividualProjection.

private static DataMap processIndividualProjection(final DataMap dataMap, final Map<String, List<String>> result, final String projectionKey) {
    final DataMap projectionsMap = dataMap.getDataMap(projectionKey);
    final String encodedFields = URIMaskUtil.encodeMaskForURI(projectionsMap);
    result.put(projectionKey, Collections.singletonList(encodedFields));
    final DataMap dataMapClone;
    try {
        dataMapClone = dataMap.clone();
        dataMapClone.remove(projectionKey);
    } catch (CloneNotSupportedException e) {
        // should never be reached
        throw new AssertionError(e);
    }
    return dataMapClone;
}
Also used : ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap)

Example 34 with DataMap

use of com.linkedin.data.DataMap in project rest.li by linkedin.

the class ResponseUtils method convertKey.

public static Object convertKey(String rawKey, TypeSpec<?> keyType, Map<String, CompoundKey.TypeInfo> keyParts, ComplexKeySpec<?, ?> complexKeyType, ProtocolVersion version) {
    Class<?> keyBindingClass = keyType.getType();
    Object result;
    if (TyperefInfo.class.isAssignableFrom(keyType.getType())) {
        TyperefDataSchema schema = (TyperefDataSchema) keyType.getSchema();
        if (!schema.getDereferencedDataSchema().isPrimitive()) {
            throw new IllegalArgumentException("Typeref must reference a primitive type when used as a key type.");
        }
        // Coerce the raw key string to the referenced primitive type.
        DataSchema.Type dereferencedType = schema.getDereferencedType();
        Class<?> primitiveClass = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(dereferencedType);
        result = ValueConverter.coerceString(rawKey, primitiveClass);
        // Identify the binding class for the typeref.
        keyBindingClass = CustomTypeUtil.getJavaCustomTypeClassFromSchema(schema);
        if (keyBindingClass == null) {
            keyBindingClass = primitiveClass;
        }
    } else if (CompoundKey.class.isAssignableFrom(keyType.getType())) {
        DataMap keyDataMap;
        if (version.compareTo(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()) >= 0) {
            try {
                keyDataMap = (DataMap) URIElementParser.parse(rawKey);
            } catch (PathSegment.PathSegmentSyntaxException e) {
                throw new IllegalStateException(rawKey + " is not a valid value for the resource key", e);
            }
        } else {
            keyDataMap = parseKey(rawKey);
        }
        result = CompoundKey.fromValues(keyDataMap, keyParts);
    } else if (ComplexResourceKey.class.isAssignableFrom(keyType.getType())) {
        try {
            ComplexResourceKey<RecordTemplate, RecordTemplate> complexResourceKey = ComplexResourceKey.parseString(rawKey, complexKeyType, version);
            result = QueryParamsDataMap.fixUpComplexKeySingletonArray(complexResourceKey);
        } catch (PathSegment.PathSegmentSyntaxException e) {
            throw new IllegalStateException(rawKey + " is not a valid value for the resource key", e);
        }
    } else {
        try {
            result = ValueConverter.coerceString(rawKey, keyType.getType());
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException(rawKey + " is not a valid value for resource key type " + keyType.getType().getName(), e);
        }
    }
    return DataTemplateUtil.coerceOutput(result, keyBindingClass);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate)

Example 35 with DataMap

use of com.linkedin.data.DataMap in project rest.li by linkedin.

the class ResponseUtils method parseKey.

//TODO: replace with generic QueryParam <=> DataMap codec
private static DataMap parseKey(String rawKey) {
    Map<String, List<String>> fields = UriComponent.decodeQuery(rawKey, true);
    DataMap result = new DataMap((int) Math.ceil(fields.size() / 0.75f));
    for (Map.Entry<String, List<String>> entry : fields.entrySet()) {
        if (entry.getValue().size() == 1) {
            result.put(entry.getKey(), entry.getValue().get(0));
        } else {
            CheckedUtil.putWithoutChecking(result, entry.getKey(), new DataList(entry.getValue()));
        }
    }
    return result;
}
Also used : DataList(com.linkedin.data.DataList) DataList(com.linkedin.data.DataList) List(java.util.List) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Aggregations

DataMap (com.linkedin.data.DataMap)471 Test (org.testng.annotations.Test)238 DataList (com.linkedin.data.DataList)130 ByteString (com.linkedin.data.ByteString)110 HashMap (java.util.HashMap)56 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)49 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)47 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)46 DataSchema (com.linkedin.data.schema.DataSchema)45 Map (java.util.Map)45 ArrayList (java.util.ArrayList)31 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)23 PathSpec (com.linkedin.data.schema.PathSpec)21 RecordTemplate (com.linkedin.data.template.RecordTemplate)21 DataProvider (org.testng.annotations.DataProvider)20 HashSet (java.util.HashSet)19 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)18 PatchTree (com.linkedin.data.transform.patch.request.PatchTree)18 CompoundKey (com.linkedin.restli.common.CompoundKey)18 IOException (java.io.IOException)18