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