Search in sources :

Example 41 with DataMap

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

Example 42 with 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;
}
Also used : DataList(com.linkedin.data.DataList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 43 with 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());
}
Also used : DataMap(com.linkedin.data.DataMap) TestDataBuilders.toDataMap(com.linkedin.restli.common.testutils.TestDataBuilders.toDataMap)

Example 44 with DataMap

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);
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) DataList(com.linkedin.data.DataList) DataComplexProcessor(com.linkedin.data.transform.DataComplexProcessor) Patch(com.linkedin.data.transform.patch.Patch) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 45 with DataMap

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");
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) DataMap(com.linkedin.data.DataMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

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