Search in sources :

Example 61 with DataSchema

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

the class TemplateSpecGenerator method getImmediateCustomInfo.

private CustomInfoSpec getImmediateCustomInfo(DataSchema schema) {
    if (_immediateCustomMap.containsKey(schema)) {
        return _immediateCustomMap.get(schema);
    }
    CustomInfoSpec immediate = null;
    for (DataSchema current = schema; current != null; current = dereferenceIfTyperef(current)) {
        final CustomClasses customClasses = getCustomClasses(current);
        if (customClasses != null) {
            immediate = new CustomInfoSpec((NamedDataSchema) schema, (NamedDataSchema) current, customClasses.customClass, customClasses.customCoercerClass);
            break;
        }
    }
    // immediate may be null
    _immediateCustomMap.put(schema, immediate);
    return immediate;
}
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) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec)

Example 62 with DataSchema

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

the class TemplateSpecGenerator method generateUnion.

private UnionTemplateSpec generateUnion(UnionDataSchema schema, UnionTemplateSpec unionClass) {
    final Map<CustomInfoSpec, Object> customInfoMap = new IdentityHashMap<CustomInfoSpec, Object>(schema.getTypes().size() * 2);
    for (DataSchema memberType : schema.getTypes()) {
        final UnionTemplateSpec.Member newMember = new UnionTemplateSpec.Member();
        unionClass.getMembers().add(newMember);
        newMember.setSchema(memberType);
        if (memberType.getDereferencedType() != DataSchema.Type.NULL) {
            newMember.setClassTemplateSpec(processSchema(memberType, unionClass, memberType.getUnionMemberKey()));
            newMember.setDataClass(determineDataClass(memberType, unionClass, memberType.getUnionMemberKey()));
            final CustomInfoSpec customInfo = getImmediateCustomInfo(memberType);
            if (customInfo != null) {
                if (!customInfoMap.containsKey(customInfo)) {
                    customInfoMap.put(customInfo, null);
                }
                newMember.setCustomInfo(customInfo);
            }
        }
    }
    return unionClass;
}
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) IdentityHashMap(java.util.IdentityHashMap) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec)

Example 63 with DataSchema

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

the class TemplateSpecGenerator method determineDataClass.

private ClassTemplateSpec determineDataClass(DataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
    final ClassTemplateSpec result;
    final DataSchema dereferencedSchema = schema.getDereferencedDataSchema();
    if (dereferencedSchema.getType() == DataSchema.Type.ENUM) {
        result = PrimitiveTemplateSpec.getInstance(DataSchema.Type.STRING);
    } else if (CodeUtil.isDirectType(dereferencedSchema)) {
        result = getPrimitiveClassForSchema((PrimitiveDataSchema) dereferencedSchema, enclosingClass, memberName);
    } else {
        result = null;
    }
    return result;
}
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) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec)

Example 64 with DataSchema

use of com.linkedin.data.schema.DataSchema 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 65 with DataSchema

use of com.linkedin.data.schema.DataSchema 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)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)131 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)82 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)53 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)48 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)44 DataMap (com.linkedin.data.DataMap)43 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)41 MapDataSchema (com.linkedin.data.schema.MapDataSchema)40 Test (org.testng.annotations.Test)37 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)36 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)24 ByteString (com.linkedin.data.ByteString)15 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)15 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)14 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)14 ArrayList (java.util.ArrayList)12 DataList (com.linkedin.data.DataList)11 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)9 SchemaParser (com.linkedin.data.schema.SchemaParser)9 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)9