Search in sources :

Example 36 with DataMap

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

the class URIElementParser method parseMap.

private static DataMap parseMap(Queue<Token> tokenQueue) throws PathSegment.PathSegmentSyntaxException {
    DataMap map = new DataMap();
    Token firstToken = tokenQueue.poll();
    assertExpectation(firstToken, GrammarMarker.MAP_START);
    Token nextToken = tokenQueue.peek();
    if (!nextToken.grammarEquals(GrammarMarker.OBJ_END)) {
        parseMapElements(tokenQueue, map);
    }
    Token lastToken = tokenQueue.poll();
    assertExpectation(lastToken, GrammarMarker.OBJ_END);
    return map;
}
Also used : DataMap(com.linkedin.data.DataMap)

Example 37 with DataMap

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

the class URIMaskUtil method decodeMaskUriFormat.

/**
   * Return a MaskTree decoded from the URI-formatted String input.
   *
   * @param toparse StringBuilder containing a URI-formatted String
   *                representation of an encoded MaskTree
   * @return a MaskTree
   * @throws IllegalMaskException if syntax in the input is malformed
   */
public static MaskTree decodeMaskUriFormat(StringBuilder toparse) throws IllegalMaskException {
    ParseState state = ParseState.PARSE_FIELDS;
    DataMap result = new DataMap();
    Deque<DataMap> stack = new ArrayDeque<DataMap>();
    stack.addLast(result);
    while (toparse.length() > 0) {
        switch(state) {
            case TRAVERSE:
                if (toparse.indexOf(",") != 0) {
                    throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while traversing");
                }
                toparse.delete(0, 1);
                state = ParseState.PARSE_FIELDS;
                break;
            case DESCEND:
                if (toparse.indexOf(":(") != 0) {
                    throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while descending");
                }
                toparse.delete(0, 2);
                state = ParseState.PARSE_FIELDS;
                break;
            case PARSE_FIELDS:
                Integer maskValue = null;
                if (toparse.charAt(0) == '-') {
                    maskValue = MaskOperation.NEGATIVE_MASK_OP.getRepresentation();
                    toparse.delete(0, 1);
                } else {
                    maskValue = MaskOperation.POSITIVE_MASK_OP.getRepresentation();
                }
                int nextToken = -1;
                StringBuilder field = new StringBuilder();
                for (int ii = 0; ii < toparse.length(); ++ii) {
                    char c = toparse.charAt(ii);
                    switch(c) {
                        case ',':
                            state = ParseState.TRAVERSE;
                            nextToken = ii;
                            break;
                        case ':':
                            if (toparse.charAt(ii + 1) != '(') {
                                throw new IllegalMaskException("Malformed mask syntax: expected '(' token");
                            }
                            state = ParseState.DESCEND;
                            nextToken = ii;
                            break;
                        case ')':
                            state = ParseState.ASCEND;
                            nextToken = ii;
                            break;
                        default:
                            field.append(c);
                            break;
                    }
                    if (nextToken != -1) {
                        break;
                    }
                }
                if (toparse.length() != field.length()) {
                    if (nextToken == -1) {
                        throw new IllegalMaskException("Malformed mask syntax: expected closing token");
                    }
                    toparse.delete(0, nextToken);
                } else {
                    toparse.delete(0, toparse.length());
                }
                if (state == ParseState.DESCEND) {
                    if (field.length() == 0) {
                        throw new IllegalMaskException("Malformed mask syntax: empty parent field name");
                    }
                    DataMap subTree = new DataMap();
                    stack.peekLast().put(field.toString().trim(), subTree);
                    stack.addLast(subTree);
                } else if (field.length() != 0) {
                    stack.peekLast().put(field.toString().trim(), maskValue);
                }
                break;
            case ASCEND:
                if (toparse.indexOf(")") != 0) {
                    throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while ascending");
                }
                if (stack.isEmpty()) {
                    throw new IllegalMaskException("Malformed mask syntax: unexpected ')' token");
                }
                toparse.delete(0, 1);
                stack.removeLast();
                state = ParseState.PARSE_FIELDS;
                break;
        }
    }
    if (stack.size() != 1) {
        throw new IllegalMaskException("Malformed mask syntax: unmatched nesting");
    }
    result = stack.removeLast();
    return new MaskTree(result);
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ArrayDeque(java.util.ArrayDeque) DataMap(com.linkedin.data.DataMap)

Example 38 with DataMap

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

the class RestSpecCodec method serializeTypeFields.

private void serializeTypeFields(DataMap data, PathSpec path) throws IOException {
    for (Map.Entry<String, Object> entry : data.entrySet()) {
        final PathSpec currentElement = new PathSpec(path.getPathComponents(), entry.getKey());
        if (isPegasusTypeField(currentElement) && entry.getValue() instanceof DataMap) {
            final String value = new String(_dataCodec.mapToBytes((DataMap) entry.getValue()), RestConstants.DEFAULT_CHARSET);
            data.put(entry.getKey(), value);
        } else if (entry.getValue() instanceof DataMap) {
            serializeTypeFields((DataMap) entry.getValue(), currentElement);
        } else if (entry.getValue() instanceof DataList) {
            for (Object o : (DataList) entry.getValue()) {
                if (o instanceof DataMap) {
                    serializeTypeFields((DataMap) o, currentElement);
                }
            }
        }
    }
}
Also used : DataList(com.linkedin.data.DataList) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap)

Example 39 with DataMap

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

the class TestComplexResourceKey method testEquals.

@Test
public void testEquals() throws CloneNotSupportedException {
    DataMap keyMap = new DataMap();
    keyMap.put("keyField1", "keyValue1");
    EmptyRecord key1 = new EmptyRecord(keyMap);
    DataMap paramMap = new DataMap();
    paramMap.put("paramField1", "paramValue1");
    EmptyRecord param1 = new EmptyRecord(paramMap);
    ComplexResourceKey<EmptyRecord, EmptyRecord> complexKey1 = new ComplexResourceKey<EmptyRecord, EmptyRecord>(key1, param1);
    EmptyRecord key2 = key1.copy();
    EmptyRecord param2 = param1.copy();
    ComplexResourceKey<EmptyRecord, EmptyRecord> complexKey2 = new ComplexResourceKey<EmptyRecord, EmptyRecord>(key2, param2);
    Assert.assertTrue(complexKey1.equals(complexKey2));
    // Different key part
    complexKey2.key.data().put("keyField1", "keyValue2");
    Assert.assertFalse(complexKey1.equals(complexKey2));
    complexKey2.key.data().put("keyField1", "keyValue1");
    // Different param part
    complexKey2.params.data().put("paramField1", "paramValue2");
    Assert.assertFalse(complexKey1.equals(complexKey2));
    complexKey2.params.data().put("paramField1", "paramValue1");
    // One param null, other not
    complexKey1 = new ComplexResourceKey<EmptyRecord, EmptyRecord>(key1, null);
    complexKey2 = new ComplexResourceKey<EmptyRecord, EmptyRecord>(key2, param2);
    Assert.assertFalse(complexKey1.equals(complexKey2));
    Assert.assertFalse(complexKey2.equals(complexKey1));
    // Both param null
    complexKey2 = new ComplexResourceKey<EmptyRecord, EmptyRecord>(key2, null);
    Assert.assertTrue(complexKey1.equals(complexKey2));
}
Also used : DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 40 with DataMap

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

the class TestComplexResourceKey method testMakeReadOnly.

@Test
public void testMakeReadOnly() {
    DataMap keyDataMap = new DataMap();
    keyDataMap.put("key", "key-value");
    EmptyRecord key = new EmptyRecord(keyDataMap);
    DataMap paramsDataMap = new DataMap();
    paramsDataMap.put("params", "params-value");
    EmptyRecord params = new EmptyRecord(paramsDataMap);
    ComplexResourceKey<EmptyRecord, EmptyRecord> complexResourceKey = new ComplexResourceKey<EmptyRecord, EmptyRecord>(key, params);
    complexResourceKey.makeReadOnly();
    try {
        key.data().put("key", "new key value");
        Assert.fail("Should not be able to update the key after the ComplexResourceKey has been made read only!");
    } catch (UnsupportedOperationException e) {
    }
    try {
        params.data().put("params", "new params value");
        Assert.fail("Should not be able to update the params after the ComplexResourceKey has been made read only!");
    } catch (UnsupportedOperationException e) {
    }
}
Also used : DataMap(com.linkedin.data.DataMap) 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