Search in sources :

Example 26 with ArrayDataSchema

use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.

the class TestCollectionArgumentBuilder method complexArrayArgument.

@DataProvider(name = "complexArrayArgument")
private Object[][] complexArrayArgument() {
    Map<String, String> map1 = new HashMap<String, String>();
    map1.put("a", "A1");
    map1.put("b", "111");
    Map<String, String> map2 = new HashMap<String, String>();
    map2.put("a", "A2");
    map2.put("b", "222");
    Map<String, String> map3 = new HashMap<String, String>();
    map3.put("a", "A3");
    map3.put("b", "333");
    DataList data = new DataList();
    data.add(new DataMap(map1));
    data.add(new DataMap(map2));
    data.add(new DataMap(map3));
    return new Object[][] { { new Parameter<MyComplexKey[]>("myComplexKeys", MyComplexKey[].class, new ArrayDataSchema(DataTemplateUtil.getSchema(MyComplexKey.class)), false, null, Parameter.ParamType.QUERY, true, new AnnotationSet(new Annotation[] {})), "myComplexKeys", "{b=111, a=A1}", data, new Object[] { new MyComplexKey[] { new MyComplexKey().setA("A1").setB(111L), new MyComplexKey().setA("A2").setB(222L), new MyComplexKey().setA("A3").setB(333L) } } } };
}
Also used : DataList(com.linkedin.data.DataList) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) HashMap(java.util.HashMap) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) DataMap(com.linkedin.data.DataMap) DataProvider(org.testng.annotations.DataProvider)

Example 27 with ArrayDataSchema

use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.

the class CompatibilityChecker method check.

private void check(DataSchema older, DataSchema newer) {
    Checked toCheck = new Checked(older, newer);
    if (_checked.contains(toCheck)) {
        return;
    }
    _checked.add(toCheck);
    if (older == newer) {
        return;
    }
    int pathCount = 1;
    if (_options.getMode() == CompatibilityOptions.Mode.DATA) {
        older = older.getDereferencedDataSchema();
        while (newer.getType() == DataSchema.Type.TYPEREF) {
            TyperefDataSchema typerefDataSchema = ((TyperefDataSchema) newer);
            _path.addLast(typerefDataSchema.getFullName());
            _path.addLast(DataSchemaConstants.REF_KEY);
            pathCount++;
            newer = typerefDataSchema.getRef();
        }
    }
    if (newer.getType() == DataSchema.Type.TYPEREF) {
        _path.addLast(((TyperefDataSchema) newer).getFullName());
    } else {
        _path.addLast(newer.getUnionMemberKey());
    }
    switch(newer.getType()) {
        case TYPEREF:
            if (isSameType(older, newer))
                checkTyperef((TyperefDataSchema) older, (TyperefDataSchema) newer);
            break;
        case RECORD:
            if (isSameType(older, newer))
                checkRecord((RecordDataSchema) older, (RecordDataSchema) newer);
            break;
        case ARRAY:
            if (isSameType(older, newer))
                checkArray((ArrayDataSchema) older, (ArrayDataSchema) newer);
            break;
        case MAP:
            if (isSameType(older, newer))
                checkMap((MapDataSchema) older, (MapDataSchema) newer);
            break;
        case ENUM:
            if (isSameType(older, newer))
                checkEnum((EnumDataSchema) older, (EnumDataSchema) newer);
            break;
        case FIXED:
            if (isSameType(older, newer))
                checkFixed((FixedDataSchema) older, (FixedDataSchema) newer);
            break;
        case UNION:
            if (isSameType(older, newer))
                checkUnion((UnionDataSchema) older, (UnionDataSchema) newer);
            break;
        default:
            if (newer instanceof PrimitiveDataSchema)
                checkPrimitive(older, newer);
            else
                throw new IllegalStateException("Unknown schema type " + newer.getType() + ", checking old schema " + older + ", new schema " + newer);
            break;
    }
    for (; pathCount > 0; pathCount--) {
        _path.removeLast();
    }
    return;
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema)

Example 28 with ArrayDataSchema

use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.

the class SchemaSampleDataGenerator method buildData.

private static Object buildData(ParentSchemas parentSchemas, DataSchema schema, String fieldName, DataGenerationOptions spec) {
    spec = preventRecursionIntoAlreadyTraversedSchemas(parentSchemas, spec, schema);
    parentSchemas.incrementReferences(schema);
    final DataSchema derefSchema = schema.getDereferencedDataSchema();
    final SampleDataCallback callback = spec.getCallback();
    Object data = null;
    switch(derefSchema.getType()) {
        case BOOLEAN:
            data = callback.getBoolean(fieldName);
            break;
        case INT:
            data = callback.getInteger(fieldName);
            break;
        case LONG:
            data = callback.getLong(fieldName);
            break;
        case FLOAT:
            data = callback.getFloat(fieldName);
            break;
        case DOUBLE:
            data = callback.getDouble(fieldName);
            break;
        case BYTES:
            data = callback.getBytes(fieldName);
            break;
        case STRING:
            data = callback.getString(fieldName);
            break;
        case NULL:
            data = Data.NULL;
            break;
        case FIXED:
            data = callback.getFixed(fieldName, (FixedDataSchema) derefSchema);
            break;
        case ENUM:
            data = callback.getEnum(fieldName, (EnumDataSchema) derefSchema);
            break;
        case ARRAY:
            final DataList dataList = new DataList(spec.getArraySize());
            for (int i = 0; i < spec.getArraySize(); i++) {
                final Object item = buildData(parentSchemas, ((ArrayDataSchema) derefSchema).getItems(), fieldName, spec);
                dataList.add(item);
            }
            data = dataList;
            break;
        case RECORD:
            data = buildRecordData(parentSchemas, (RecordDataSchema) derefSchema, spec);
            break;
        case MAP:
            final DataMap dataMap = new DataMap();
            for (int i = 0; i < spec.getArraySize(); i++) {
                final Object item = buildData(parentSchemas, ((MapDataSchema) derefSchema).getValues(), fieldName, spec);
                dataMap.put("mapField_" + _random.nextInt(), item);
            }
            data = dataMap;
            break;
        case UNION:
            final UnionDataSchema unionSchema = (UnionDataSchema) derefSchema;
            final List<DataSchema> types = removeAlreadyTraversedSchemasFromUnionMemberList(parentSchemas, unionSchema.getTypes());
            final int unionIndex = _random.nextInt(types.size());
            final DataSchema unionItemSchema = types.get(unionIndex);
            data = buildData(parentSchemas, unionItemSchema, fieldName, spec);
            if (data != null) {
                final DataMap unionMap = new DataMap();
                unionMap.put(unionItemSchema.getUnionMemberKey(), data);
                data = unionMap;
            }
            break;
        case TYPEREF:
            data = buildData(parentSchemas, derefSchema, fieldName, spec);
            break;
    }
    parentSchemas.decrementReferences(schema);
    return data;
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Example 29 with ArrayDataSchema

use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.

the class JavaDataTemplateGenerator method generateArray.

protected void generateArray(JDefinedClass arrayClass, ArrayTemplateSpec arrayDataTemplateSpec) throws JClassAlreadyExistsException {
    final JClass itemJClass = generate(arrayDataTemplateSpec.getItemClass());
    final JClass dataJClass = generate(arrayDataTemplateSpec.getItemDataClass());
    if (CodeUtil.isDirectType(arrayDataTemplateSpec.getSchema().getItems())) {
        arrayClass._extends(_directArrayBaseClass.narrow(itemJClass));
    } else {
        extendWrappingArrayBaseClass(itemJClass, arrayClass);
    }
    /** see {@link #schemaForArrayItemsOrMapValues} */
    final DataSchema bareSchema = new ArrayDataSchema(schemaForArrayItemsOrMapValues(arrayDataTemplateSpec.getCustomInfo(), arrayDataTemplateSpec.getSchema().getItems()));
    final JVar schemaField = generateSchemaField(arrayClass, bareSchema);
    generateConstructorWithNoArg(arrayClass, _dataListClass);
    generateConstructorWithInitialCapacity(arrayClass, _dataListClass);
    generateConstructorWithCollection(arrayClass, itemJClass);
    generateConstructorWithArg(arrayClass, schemaField, _dataListClass, itemJClass, dataJClass);
    if (_pathSpecMethods) {
        generatePathSpecMethodsForCollection(arrayClass, arrayDataTemplateSpec.getSchema(), itemJClass, "items");
    }
    generateCustomClassInitialization(arrayClass, arrayDataTemplateSpec.getCustomInfo());
    if (_copierMethods) {
        generateCopierMethods(arrayClass);
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) JClass(com.sun.codemodel.JClass) JVar(com.sun.codemodel.JVar)

Example 30 with ArrayDataSchema

use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.

the class TestArrayTemplate method testBooleanArray.

@Test
public void testBooleanArray() {
    ArrayDataSchema schema = (ArrayDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"array\", \"items\" : \"boolean\" }");
    // must be unique
    List<Boolean> input = Arrays.asList(true, false);
    List<Boolean> adds = Arrays.asList(false, true, true, false);
    List<Object> badInput = asList(1, 2L, 3f, 4.0, "hello", ByteString.empty(), new StringMap(), new StringArray(), null);
    List<Object> badOutput = asList(1, 2L, 3f, 4.0, "hello", ByteString.empty(), new DataMap(), new DataList());
    testArray(BooleanArray.class, schema, input, adds);
    testArrayBadInput(BooleanArray.class, schema, input, badInput, badOutput);
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)42 Test (org.testng.annotations.Test)20 DataList (com.linkedin.data.DataList)16 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)16 DataMap (com.linkedin.data.DataMap)15 DataSchema (com.linkedin.data.schema.DataSchema)15 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)11 MapDataSchema (com.linkedin.data.schema.MapDataSchema)10 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)8 ArrayList (java.util.ArrayList)7 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)6 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)6 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)5 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)5 TestDataTemplateUtil (com.linkedin.data.template.TestDataTemplateUtil)5 ByteString (com.linkedin.data.ByteString)3 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)3 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)2 Name (com.linkedin.data.schema.Name)2 StringArray (com.linkedin.data.template.StringArray)2