Search in sources :

Example 1 with ClassTemplateSpec

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

the class TemplateSpecGenerator method generateNamedSchema.

private ClassTemplateSpec generateNamedSchema(NamedDataSchema schema) {
    pushCurrentLocation(_schemaResolver.nameToDataSchemaLocations().get(schema.getFullName()));
    // make sure no duplicate template spec classname which should be binding name of the schema
    final String className = schema.getBindingName();
    checkForClassNameConflict(className, schema);
    final ClassTemplateSpec templateClass;
    switch(schema.getType()) {
        case RECORD:
            templateClass = generateRecord((RecordDataSchema) schema);
            break;
        case ENUM:
            templateClass = generateEnum((EnumDataSchema) schema);
            break;
        case FIXED:
            templateClass = generateFixed((FixedDataSchema) schema);
            break;
        default:
            throw unrecognizedSchemaType(null, null, schema);
    }
    popCurrentLocation();
    return templateClass;
}
Also used : ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema)

Example 2 with ClassTemplateSpec

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

use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec 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 (found == null) {
            if (schema.getType() == DataSchema.Type.UNION) {
                result = generateUnion((UnionDataSchema) schema, typerefSchema);
                break;
            } else {
                generateTyperef(typerefSchema, originalTyperefSchema);
            }
        } else if (schema.getType() == DataSchema.Type.UNION) {
            result = found;
            break;
        }
    }
    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) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) 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 ClassTemplateSpec

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

the class TemplateSpecGenerator method getCustomClasses.

public static CustomClasses getCustomClasses(DataSchema schema, String customTypeLanguage) {
    CustomClasses customClasses = null;
    final Map<String, Object> properties = schema.getProperties();
    if (customTypeLanguage != null) {
        final Object java = properties.get(customTypeLanguage);
        if (java != null) {
            if (java.getClass() != DataMap.class) {
                throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property that is not a DataMap");
            }
            final DataMap map = (DataMap) java;
            final Object custom = map.get(CustomTypeUtil.CLASS_PROPERTY);
            if (custom != null) {
                if (custom.getClass() != String.class) {
                    throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property with \"class\" that is not a string");
                }
                // a custom class specification has been found
                customClasses = new CustomClasses();
                customClasses.customClass = new ClassTemplateSpec();
                customClasses.customClass.setFullName((String) custom);
                if (!allowCustomClass(schema)) {
                    throw new IllegalArgumentException(schema + " cannot have custom class binding");
                }
            }
            // check for coercer class
            final Object coercerClass = map.get(CustomTypeUtil.COERCER_CLASS_PROPERTY);
            if (coercerClass != null) {
                if (coercerClass.getClass() != String.class) {
                    throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property with \"coercerClass\" that is not a string");
                }
                if (customClasses == null) {
                    throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property with \"coercerClass\" but does not have \"class\" property");
                }
                // a custom class specification has been found
                customClasses.customCoercerClass = new ClassTemplateSpec();
                customClasses.customCoercerClass.setFullName((String) coercerClass);
            }
        }
    }
    return customClasses;
}
Also used : ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) DataMap(com.linkedin.data.DataMap)

Example 5 with ClassTemplateSpec

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

the class TemplateSpecGenerator method generateRecord.

private RecordTemplateSpec generateRecord(RecordDataSchema schema) {
    final RecordTemplateSpec recordClass = new RecordTemplateSpec(schema);
    recordClass.setNamespace(schema.getNamespace());
    recordClass.setPackage(schema.getPackage());
    recordClass.setClassName(schema.getName());
    recordClass.setModifiers(ModifierSpec.PUBLIC);
    registerClassTemplateSpec(schema, recordClass);
    // processSchema included schemas first, so that unnamed classes will belong to the defining class
    // instead of the current class
    final List<NamedDataSchema> includes = schema.getInclude();
    for (NamedDataSchema includedSchema : includes) {
        processSchema(includedSchema, null, null);
    }
    final Map<CustomInfoSpec, Object> customInfoMap = new IdentityHashMap<CustomInfoSpec, Object>(schema.getFields().size() * 2);
    for (RecordDataSchema.Field field : schema.getFields()) {
        final ClassTemplateSpec fieldClass = processSchema(field.getType(), recordClass, field.getName());
        final RecordTemplateSpec.Field newField = new RecordTemplateSpec.Field();
        newField.setSchemaField(field);
        newField.setType(fieldClass);
        newField.setDataClass(determineDataClass(field.getType(), recordClass, field.getName()));
        final CustomInfoSpec customInfo = getImmediateCustomInfo(field.getType());
        if (customInfo != null) {
            if (!customInfoMap.containsKey(customInfo)) {
                customInfoMap.put(customInfo, null);
            }
            newField.setCustomInfo(customInfo);
        }
        recordClass.addField(newField);
    }
    return recordClass;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) IdentityHashMap(java.util.IdentityHashMap) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec)

Aggregations

ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)17 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 DataSchema (com.linkedin.data.schema.DataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)4 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)3 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)3 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)3 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)3 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)3 MapDataSchema (com.linkedin.data.schema.MapDataSchema)2 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)2 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)2 FinderBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.FinderBuilderSpec)2 DataMap (com.linkedin.data.DataMap)1 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)1 RecordTemplateSpec (com.linkedin.pegasus.generator.spec.RecordTemplateSpec)1 FinderSchema (com.linkedin.restli.restspec.FinderSchema)1 ActionBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.ActionBuilderSpec)1