Search in sources :

Example 1 with RecordTemplateSpec

use of com.linkedin.pegasus.generator.spec.RecordTemplateSpec 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)

Example 2 with RecordTemplateSpec

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

the class JavaDataTemplateGenerator method defineClass.

private JDefinedClass defineClass(ClassTemplateSpec classTemplateSpec) throws JClassAlreadyExistsException {
    JDefinedClass result = _definedClasses.get(classTemplateSpec);
    if (result == null) {
        final int jmodValue = getJModValue(classTemplateSpec.getModifiers());
        final JClassContainer container;
        if (classTemplateSpec.getEnclosingClass() == null) {
            container = getPackage(classTemplateSpec.getPackage());
        } else {
            container = defineClass(classTemplateSpec.getEnclosingClass());
        }
        if (classTemplateSpec instanceof ArrayTemplateSpec || classTemplateSpec instanceof FixedTemplateSpec || classTemplateSpec instanceof MapTemplateSpec || classTemplateSpec instanceof RecordTemplateSpec || classTemplateSpec instanceof TyperefTemplateSpec || classTemplateSpec instanceof UnionTemplateSpec) {
            result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()));
        } else if (classTemplateSpec instanceof EnumTemplateSpec) {
            result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()), ClassType.ENUM);
        } else {
            throw new RuntimeException();
        }
        _definedClasses.put(classTemplateSpec, result);
    }
    return result;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) FixedTemplateSpec(com.linkedin.pegasus.generator.spec.FixedTemplateSpec) MapTemplateSpec(com.linkedin.pegasus.generator.spec.MapTemplateSpec) EnumTemplateSpec(com.linkedin.pegasus.generator.spec.EnumTemplateSpec) TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) JClassContainer(com.sun.codemodel.JClassContainer) ArrayTemplateSpec(com.linkedin.pegasus.generator.spec.ArrayTemplateSpec)

Example 3 with RecordTemplateSpec

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

the class JavaDataTemplateGenerator method generateRecord.

protected void generateRecord(JDefinedClass templateClass, RecordTemplateSpec recordSpec) throws JClassAlreadyExistsException {
    templateClass.javadoc().append(recordSpec.getSchema().getDoc());
    setDeprecatedAnnotationAndJavadoc(recordSpec.getSchema(), templateClass);
    extendRecordBaseClass(templateClass);
    if (_pathSpecMethods) {
        generatePathSpecMethodsForRecord(recordSpec.getFields(), templateClass);
    }
    final JFieldVar schemaFieldVar = generateSchemaField(templateClass, recordSpec.getSchema());
    generateConstructorWithNoArg(templateClass, schemaFieldVar, _dataMapClass);
    generateConstructorWithArg(templateClass, schemaFieldVar, _dataMapClass);
    for (RecordTemplateSpec.Field field : recordSpec.getFields()) {
        generateRecordFieldAccessors(templateClass, field, generate(field.getType()), schemaFieldVar);
    }
    recordSpec.getFields().stream().map(RecordTemplateSpec.Field::getCustomInfo).distinct().forEach(customInfo -> generateCustomClassInitialization(templateClass, customInfo));
    if (_copierMethods) {
        generateCopierMethods(templateClass);
    }
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec)

Example 4 with RecordTemplateSpec

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

the class TestTemplateSpecGenerator method testCustomInfoForRecordFields.

@Test(dataProvider = "customTypeDataForRecord")
public void testCustomInfoForRecordFields(final List<DataSchema> customTypedSchemas) {
    final List<RecordDataSchema.Field> fields = customTypedSchemas.stream().map(RecordDataSchema.Field::new).peek(field -> field.setName("field_" + _uniqueNumberGenerator.getAndIncrement(), null)).collect(Collectors.toList());
    final RecordDataSchema record = new RecordDataSchema(new Name(INPUT_SCHEMA_NAME), RecordDataSchema.RecordType.RECORD);
    record.setFields(fields, null);
    final TemplateSpecGenerator generator = new TemplateSpecGenerator(_resolver);
    final RecordTemplateSpec spec = (RecordTemplateSpec) generator.generate(record, _location);
    for (int i = 0; i < customTypedSchemas.size(); ++i) {
        Assert.assertNotNull(spec.getFields().get(i).getCustomInfo());
        Assert.assertEquals(spec.getFields().get(i).getCustomInfo().getCustomClass().getClassName(), CustomTypeUtil.getJavaCustomTypeClassNameFromSchema((TyperefDataSchema) customTypedSchemas.get(i)));
    }
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) Mock(org.mockito.Mock) DataSchema(com.linkedin.data.schema.DataSchema) BeforeMethod(org.testng.annotations.BeforeMethod) Test(org.testng.annotations.Test) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) CustomTypeUtil(com.linkedin.util.CustomTypeUtil) Collectors(java.util.stream.Collectors) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Mockito(org.mockito.Mockito) MockitoAnnotations(org.mockito.MockitoAnnotations) DataMap(com.linkedin.data.DataMap) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Assert(org.testng.Assert) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) DataSchemaUtil(com.linkedin.data.schema.DataSchemaUtil) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) Collections(java.util.Collections) Name(com.linkedin.data.schema.Name) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) Name(com.linkedin.data.schema.Name) Test(org.testng.annotations.Test)

Aggregations

RecordTemplateSpec (com.linkedin.pegasus.generator.spec.RecordTemplateSpec)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)2 DataMap (com.linkedin.data.DataMap)1 DataSchema (com.linkedin.data.schema.DataSchema)1 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)1 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 DataSchemaUtil (com.linkedin.data.schema.DataSchemaUtil)1 Name (com.linkedin.data.schema.Name)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)1 ArrayTemplateSpec (com.linkedin.pegasus.generator.spec.ArrayTemplateSpec)1 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)1 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)1 EnumTemplateSpec (com.linkedin.pegasus.generator.spec.EnumTemplateSpec)1 FixedTemplateSpec (com.linkedin.pegasus.generator.spec.FixedTemplateSpec)1 MapTemplateSpec (com.linkedin.pegasus.generator.spec.MapTemplateSpec)1 TyperefTemplateSpec (com.linkedin.pegasus.generator.spec.TyperefTemplateSpec)1 CustomTypeUtil (com.linkedin.util.CustomTypeUtil)1