Search in sources :

Example 16 with ArrayDataSchema

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

the class TestArray method testRecordArray.

@Test
public void testRecordArray() {
    TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new ArrayTest(), "recordArray");
    @SuppressWarnings("unchecked") Class<RecordBarArray> templateClass = (Class<RecordBarArray>) fieldInfo.getFieldClass();
    ArrayDataSchema schema = (ArrayDataSchema) fieldInfo.getField().getType();
    List<RecordBar> input = new ArrayList<RecordBar>();
    for (int i = 0; i < 5; ++i) {
        input.add(new RecordBar());
        input.get(i).setLocation("input " + i);
    }
    List<RecordBar> adds = new ArrayList<RecordBar>();
    for (int i = 0; i < 5; ++i) {
        adds.add(new RecordBar());
        input.get(i).setLocation("add " + i);
    }
    TestArrayTemplate.testArray(templateClass, schema, input, adds);
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 17 with ArrayDataSchema

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

the class TestArray method testRecordArray.

@Test
public void testRecordArray() {
    TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new ArrayTest(), "recordArray");
    @SuppressWarnings("unchecked") Class<RecordBarArray> templateClass = (Class<RecordBarArray>) fieldInfo.getFieldClass();
    ArrayDataSchema schema = (ArrayDataSchema) fieldInfo.getField().getType();
    List<RecordBar> input = new ArrayList<RecordBar>();
    for (int i = 0; i < 5; ++i) {
        input.add(new RecordBar());
        input.get(i).setLocation("input " + i);
    }
    List<RecordBar> adds = new ArrayList<RecordBar>();
    for (int i = 0; i < 5; ++i) {
        adds.add(new RecordBar());
        input.get(i).setLocation("add " + i);
    }
    TestArrayTemplate.testArray(templateClass, schema, input, adds);
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TestDataTemplateUtil(com.linkedin.data.template.TestDataTemplateUtil) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 18 with ArrayDataSchema

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

the class TemplateSpecGenerator method generateArray.

private ArrayTemplateSpec generateArray(ArrayDataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
    final DataSchema itemSchema = schema.getItems();
    final ClassInfo classInfo = classInfoForUnnamed(enclosingClass, memberName, schema);
    if (classInfo.existingClass != null) {
        /* When type refs are used as item types inside some unnamed complex schemas like map and array,
       * the type refs are de-referenced and the underlying real type is used in the generated class.
       * In those cases the type refs are not processed by the class generation logic, an explicit
       * schema processing is necessary in order to processSchema the data template classes for those type
       * refs.
       */
        processSchema(itemSchema, enclosingClass, memberName);
        return (ArrayTemplateSpec) classInfo.existingClass;
    }
    final ArrayTemplateSpec arrayClass = (ArrayTemplateSpec) classInfo.definedClass;
    registerClassTemplateSpec(schema, arrayClass);
    arrayClass.setItemClass(processSchema(itemSchema, enclosingClass, memberName));
    arrayClass.setItemDataClass(determineDataClass(itemSchema, enclosingClass, memberName));
    final CustomInfoSpec customInfo = getImmediateCustomInfo(itemSchema);
    arrayClass.setCustomInfo(customInfo);
    return arrayClass;
}
Also used : FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) ArrayTemplateSpec(com.linkedin.pegasus.generator.spec.ArrayTemplateSpec)

Example 19 with ArrayDataSchema

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

the class TemplateSpecGenerator method classNameForUnnamedTraverse.

private ClassInfo classNameForUnnamedTraverse(ClassTemplateSpec enclosingClass, String memberName, DataSchema schema) {
    final DataSchema dereferencedDataSchema = schema.getDereferencedDataSchema();
    switch(dereferencedDataSchema.getType()) {
        case ARRAY:
            final ArrayDataSchema arraySchema = (ArrayDataSchema) dereferencedDataSchema;
            CustomInfoSpec customInfo = getImmediateCustomInfo(arraySchema.getItems());
            if (customInfo != null) {
                return new ClassInfo(customInfo.getCustomSchema().getNamespace(), customInfo.getCustomSchema().getName() + ARRAY_SUFFIX, customInfo.getCustomSchema().getPackage());
            } else {
                final ClassInfo classInfo = classNameForUnnamedTraverse(enclosingClass, memberName, arraySchema.getItems());
                classInfo.name += ARRAY_SUFFIX;
                return classInfo;
            }
        case MAP:
            final MapDataSchema mapSchema = (MapDataSchema) dereferencedDataSchema;
            customInfo = getImmediateCustomInfo(mapSchema.getValues());
            if (customInfo != null) {
                return new ClassInfo(customInfo.getCustomSchema().getNamespace(), customInfo.getCustomSchema().getName() + MAP_SUFFIX, customInfo.getCustomSchema().getPackage());
            } else {
                final ClassInfo classInfo = classNameForUnnamedTraverse(enclosingClass, memberName, mapSchema.getValues());
                classInfo.name += MAP_SUFFIX;
                return classInfo;
            }
        case UNION:
            if (schema.getType() == DataSchema.Type.TYPEREF) {
                DataSchema referencedDataSchema;
                TyperefDataSchema typerefDataSchema = (TyperefDataSchema) schema;
                while ((referencedDataSchema = typerefDataSchema.getDereferencedDataSchema()) != dereferencedDataSchema) {
                    typerefDataSchema = (TyperefDataSchema) referencedDataSchema;
                }
                return new ClassInfo(typerefDataSchema.getNamespace(), CodeUtil.capitalize(typerefDataSchema.getName()), typerefDataSchema.getPackage());
            } else {
                return new ClassInfo(enclosingClass.getFullName(), CodeUtil.capitalize(memberName));
            }
        case FIXED:
        case RECORD:
        case ENUM:
            final NamedDataSchema namedSchema = (NamedDataSchema) dereferencedDataSchema;
            // carry package override information for named schema.
            return new ClassInfo(namedSchema.getNamespace(), CodeUtil.capitalize(namedSchema.getName()), namedSchema.getPackage());
        case BOOLEAN:
            return new ClassInfo(_templatePackageName, "Boolean");
        case INT:
            return new ClassInfo(_templatePackageName, "Integer");
        case LONG:
            return new ClassInfo(_templatePackageName, "Long");
        case FLOAT:
            return new ClassInfo(_templatePackageName, "Float");
        case DOUBLE:
            return new ClassInfo(_templatePackageName, "Double");
        case STRING:
            return new ClassInfo(_templatePackageName, "String");
        case BYTES:
            return new ClassInfo(_templatePackageName, "ByteString");
        case NULL:
            throw nullTypeNotAllowed(enclosingClass, memberName);
        default:
            throw unrecognizedSchemaType(enclosingClass, memberName, dereferencedDataSchema);
    }
}
Also used : FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) MapDataSchema(com.linkedin.data.schema.MapDataSchema)

Example 20 with ArrayDataSchema

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

the class ExampleRequestResponseGenerator method generateFieldDefValue.

private Object generateFieldDefValue(FieldDef<?> fieldDef) {
    Object value = _dataGenerator.buildData(fieldDef.getName(), fieldDef.getDataSchema());
    DataSchema dereferencedDataSchema = fieldDef.getDataSchema().getDereferencedDataSchema();
    if (!dereferencedDataSchema.isPrimitive()) {
        switch(dereferencedDataSchema.getType()) {
            case FIXED:
                value = new FixedTemplatePlaceholder(value, (FixedDataSchema) dereferencedDataSchema);
                break;
            case ENUM:
                // just use the string value already generated.  Will be coerced by DataTemplateUtil.DynamicEnumCoercer.
                break;
            case ARRAY:
                value = new ArrayTemplatePlaceholder<Object>((DataList) value, (ArrayDataSchema) dereferencedDataSchema, Object.class);
                break;
            case RECORD:
                value = new RecordTemplatePlaceholder((DataMap) value, (RecordDataSchema) dereferencedDataSchema);
                break;
            case MAP:
                value = new MapTemplatePlaceholder<Object>((DataMap) value, (MapDataSchema) dereferencedDataSchema, Object.class);
                break;
            case UNION:
                value = new UnionTemplatePlaceholder(value, (UnionDataSchema) dereferencedDataSchema);
                break;
            case TYPEREF:
                throw new IllegalStateException("TYPEREF should not be returned for a dereferenced byte. schema: " + fieldDef.getDataSchema());
            default:
                throw new IllegalStateException("Unrecognized enum value: " + dereferencedDataSchema.getType());
        }
    }
    return value;
}
Also used : MapDataSchema(com.linkedin.data.schema.MapDataSchema) DataMap(com.linkedin.data.DataMap) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataList(com.linkedin.data.DataList) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

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