Search in sources :

Example 1 with CustomInfoSpec

use of com.linkedin.pegasus.generator.spec.CustomInfoSpec 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 2 with CustomInfoSpec

use of com.linkedin.pegasus.generator.spec.CustomInfoSpec 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<>(schema.getMembers().size() * 2);
    for (UnionDataSchema.Member member : schema.getMembers()) {
        DataSchema memberType = member.getType();
        final UnionTemplateSpec.Member newMember = new UnionTemplateSpec.Member();
        unionClass.getMembers().add(newMember);
        newMember.setSchema(memberType);
        newMember.setAlias(member.getAlias());
        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) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) IdentityHashMap(java.util.IdentityHashMap) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec)

Example 3 with CustomInfoSpec

use of com.linkedin.pegasus.generator.spec.CustomInfoSpec in project rest.li by linkedin.

the class TemplateSpecGenerator method processSchema.

private ClassTemplateSpec processSchema(DataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
    final CustomInfoSpec customInfo = getImmediateCustomInfo(schema);
    ClassTemplateSpec result = null;
    TyperefDataSchema originalTyperefSchema = null;
    while (schema.getType() == DataSchema.Type.TYPEREF) {
        final TyperefDataSchema typerefSchema = (TyperefDataSchema) schema;
        if (originalTyperefSchema == null) {
            originalTyperefSchema = typerefSchema;
        }
        final ClassTemplateSpec found = _schemaToClassMap.get(schema);
        schema = typerefSchema.getRef();
        if (schema.getType() == DataSchema.Type.UNION) {
            result = (found != null) ? found : generateUnion((UnionDataSchema) schema, typerefSchema);
            break;
        } else if (found == null) {
            generateTyperef(typerefSchema, originalTyperefSchema);
        }
    }
    if (result == null) {
        assert schema == schema.getDereferencedDataSchema();
        if (schema instanceof ComplexDataSchema) {
            final ClassTemplateSpec found = _schemaToClassMap.get(schema);
            if (found == null) {
                if (schema instanceof NamedDataSchema) {
                    result = generateNamedSchema((NamedDataSchema) schema);
                } else {
                    result = generateUnnamedComplexSchema(schema, enclosingClass, memberName);
                }
            } else {
                result = found;
            }
            if (customInfo != null) {
                result = customInfo.getCustomClass();
            }
        } else if (schema instanceof PrimitiveDataSchema) {
            result = (customInfo != null) ? customInfo.getCustomClass() : getPrimitiveClassForSchema((PrimitiveDataSchema) schema, enclosingClass, memberName);
        }
    }
    if (result == null) {
        throw unrecognizedSchemaType(enclosingClass, memberName, schema);
    }
    result.setOriginalTyperefSchema(originalTyperefSchema);
    return result;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema)

Example 4 with CustomInfoSpec

use of com.linkedin.pegasus.generator.spec.CustomInfoSpec 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 5 with CustomInfoSpec

use of com.linkedin.pegasus.generator.spec.CustomInfoSpec in project rest.li by linkedin.

the class TemplateSpecGenerator method generateTyperef.

private TyperefTemplateSpec generateTyperef(TyperefDataSchema schema, TyperefDataSchema originalTyperefSchema) {
    pushCurrentLocation(_schemaResolver.nameToDataSchemaLocations().get(schema.getFullName()));
    final TyperefTemplateSpec typerefClass = new TyperefTemplateSpec(schema);
    typerefClass.setOriginalTyperefSchema(originalTyperefSchema);
    typerefClass.setNamespace(schema.getNamespace());
    typerefClass.setPackage(schema.getPackage());
    typerefClass.setClassName(schema.getName());
    typerefClass.setModifiers(ModifierSpec.PUBLIC);
    registerClassTemplateSpec(schema, typerefClass);
    final CustomInfoSpec customInfo = getImmediateCustomInfo(schema);
    typerefClass.setCustomInfo(customInfo);
    popCurrentLocation();
    return typerefClass;
}
Also used : CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec)

Aggregations

CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)8 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)7 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)6 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)6 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)6 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)6 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)5 DataSchema (com.linkedin.data.schema.DataSchema)5 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)5 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)5 MapDataSchema (com.linkedin.data.schema.MapDataSchema)5 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)5 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)2 IdentityHashMap (java.util.IdentityHashMap)2 ArrayTemplateSpec (com.linkedin.pegasus.generator.spec.ArrayTemplateSpec)1 MapTemplateSpec (com.linkedin.pegasus.generator.spec.MapTemplateSpec)1 RecordTemplateSpec (com.linkedin.pegasus.generator.spec.RecordTemplateSpec)1 TyperefTemplateSpec (com.linkedin.pegasus.generator.spec.TyperefTemplateSpec)1 UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)1