use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class URIParamUtils method compoundKeyToDataMap.
/**
* Create a DataMap representation of this CompoundKey. If any of its fields are CustomTypes,
* they will be coerced down to their base type before being placed into the map.
* It is distinct from {@link CompoundKey#toDataMap(java.util.Map)} because we may not know the
* field types when we need to do this transition internally. As a result, it may be slightly slower.
*
* @return a {@link DataMap} representation of this {@link CompoundKey}
* @see {@link CompoundKey#toDataMap(java.util.Map)}
*/
public static DataMap compoundKeyToDataMap(CompoundKey compoundKey) {
DataMap dataMap = new DataMap(compoundKey.getNumParts());
for (String key : compoundKey.getPartKeys()) {
Object value = compoundKey.getPart(key);
Class<?> valueClass = value.getClass();
if (DataTemplateUtil.hasCoercer(valueClass) || valueClass.isEnum()) {
@SuppressWarnings("unchecked") Object coercedValue = DataTemplateUtil.coerceInput(value, (Class<Object>) valueClass, Object.class);
dataMap.put(key, coercedValue);
} else {
dataMap.put(key, value);
}
}
return dataMap;
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class URIParamUtils method parseUriParams.
public static DataMap parseUriParams(Map<String, List<String>> queryParameters) throws PathSegment.PathSegmentSyntaxException {
DataMap dataMap = new DataMap();
for (Map.Entry<String, List<String>> entry : queryParameters.entrySet()) {
String key = entry.getKey();
Object value;
List<String> values = entry.getValue();
if (values.size() > 1) {
throw new PathSegment.PathSegmentSyntaxException("unexpected repeated query param in URI: " + key);
}
String encodedValue = values.get(0);
if (RestConstants.PROJECTION_PARAMETERS.contains(key)) {
//don't decode it.
value = encodedValue;
} else {
try {
value = URIElementParser.parse(encodedValue);
} catch (PathSegment.PathSegmentSyntaxException e) {
throw new PathSegment.PathSegmentSyntaxException("error while parsing query param '" + key + "'\n" + e.getMessage());
}
}
dataMap.put(key, value);
}
return dataMap;
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestDataCompare method testNull.
public void testNull() {
DataMap nullFieldMap = toDataMap("nullField", Data.NULL);
assertFalse(DataCompare.compare(nullFieldMap, nullFieldMap).hasError());
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestPatchGeneration method testRoundtripDeleteField.
@Test
void testRoundtripDeleteField() throws Exception {
Group g1 = new Group();
g1.setId(17);
g1.setDescription("Some description");
Group g2 = new Group(g1.data().copy());
g2.removeDescription();
PatchTree update = PatchCreator.diff(g1, g2);
//"{$delete=[description]}"
final DataMap deleteMap = new DataMap();
final DataList descriptionList = new DataList();
descriptionList.add("description");
deleteMap.put(PatchConstants.DELETE_COMMAND, descriptionList);
assertEquals(update.getDataMap(), deleteMap, "PatchTree DataMap should be correct");
assertFalse(g1.equals(g2));
DataComplexProcessor processor = new DataComplexProcessor(new Patch(), update.getDataMap(), g1.data());
processor.run(false);
assertEquals(g1, g2);
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestPatchGeneration method testNegativeMask.
@Test
public void testNegativeMask() throws Exception {
MaskTree mask = MaskCreator.createNegativeMask(Group.fields().badge(), Group.fields().id(), Group.fields().owner().id());
//"{id=0, owner={id=0}, badge=0}"
final DataMap idOwnerBadgeMap = new DataMap();
idOwnerBadgeMap.put("id", 0);
idOwnerBadgeMap.put("badge", 0);
final DataMap idMap = new DataMap();
idMap.put("id", 0);
idOwnerBadgeMap.put("owner", idMap);
Assert.assertEquals(mask.getDataMap(), idOwnerBadgeMap, "The MaskTree DataMap should match");
//The ordering might be different but the URI should look something like:
//"-id,owner:(-id),-badge";
final String actualEncodedMaskURI = URIMaskUtil.encodeMaskForURI(mask);
final Set<String> maskURISet = new HashSet<String>(Arrays.asList(actualEncodedMaskURI.split(",")));
final Set<String> expectedURISet = new HashSet<String>();
expectedURISet.add("-id");
expectedURISet.add("owner:(-id)");
expectedURISet.add("-badge");
Assert.assertEquals(maskURISet, expectedURISet, "The encoded mask should be correct");
}
Aggregations