Search in sources :

Example 1 with UnionTemplateSpec

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

the class JavaDataTemplateGenerator method generatePathSpecMethodsForUnion.

private void generatePathSpecMethodsForUnion(UnionTemplateSpec unionSpec, JDefinedClass unionClass) throws JClassAlreadyExistsException {
    final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(unionClass);
    for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
        JClass fieldsRefType = _pathSpecClass;
        if (hasNestedFields(member.getSchema())) {
            final JClass unionMemberClass = generate(member.getClassTemplateSpec());
            fieldsRefType = getCodeModel().ref(unionMemberClass.fullName() + ".Fields");
        }
        String memberKey = member.getUnionMemberKey();
        String methodName = CodeUtil.getUnionMemberName(member);
        final JMethod accessorMethod = fieldsNestedClass.method(JMod.PUBLIC, fieldsRefType, methodName);
        accessorMethod.body()._return(JExpr._new(fieldsRefType).arg(JExpr.invoke("getPathComponents")).arg(memberKey));
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) ByteString(com.linkedin.data.ByteString) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) JMethod(com.sun.codemodel.JMethod)

Example 2 with UnionTemplateSpec

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

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

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

the class JavaDataTemplateGenerator method generateUnion.

protected void generateUnion(JDefinedClass unionClass, UnionTemplateSpec unionSpec) throws JClassAlreadyExistsException {
    extendUnionBaseClass(unionClass);
    final JVar schemaField = generateSchemaField(unionClass, unionSpec.getSchema(), unionSpec.getSourceFileFormat());
    // Generate instance vars for members.
    Map<String, JVar> memberVarMap = new HashMap<>();
    for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
        if (member.getClassTemplateSpec() != null) {
            final String memberName = CodeUtil.uncapitalize(CodeUtil.getUnionMemberName(member));
            final JVar memberVar = unionClass.field(JMod.PRIVATE, generate(member.getClassTemplateSpec()), "_" + memberName + "Member", JExpr._null());
            memberVarMap.put(member.getUnionMemberKey(), memberVar);
        }
    }
    final JClass changeListenerClass;
    final JVar changeListenerVar;
    // Generate change listener if there are any members.
    if (!memberVarMap.isEmpty()) {
        changeListenerClass = generateChangeListener(unionClass, memberVarMap);
        changeListenerVar = unionClass.field(JMod.PRIVATE, changeListenerClass, "__changeListener", JExpr._new(changeListenerClass).arg(JExpr._this()));
    } else {
        changeListenerClass = null;
        changeListenerVar = null;
    }
    // Default union datamap size to 1 (last arg) as union can have at-most one element.
    // We don't need cache for unions, so pass in -1 for cache size to ignore size param.
    generateDataMapConstructor(unionClass, schemaField, 1, -1, changeListenerVar);
    generateConstructorWithObjectArg(unionClass, schemaField, changeListenerVar);
    for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
        if (member.getClassTemplateSpec() != null) {
            generateUnionMemberAccessors(unionClass, member, generate(member.getClassTemplateSpec()), generate(member.getDataClass()), schemaField, memberVarMap.get(member.getUnionMemberKey()));
        }
    }
    unionSpec.getMembers().stream().map(UnionTemplateSpec.Member::getCustomInfo).distinct().forEach(customInfo -> generateCustomClassInitialization(unionClass, customInfo));
    if (_pathSpecMethods) {
        generatePathSpecMethodsForUnion(unionSpec, unionClass);
    }
    if (_fieldMaskMethods) {
        generateMaskBuilderForUnion(unionSpec, unionClass);
    }
    if (_copierMethods) {
        generateCopierMethods(unionClass, memberVarMap, changeListenerClass);
    }
    if (unionSpec.getTyperefClass() != null) {
        final TyperefTemplateSpec typerefClassSpec = unionSpec.getTyperefClass();
        final JDefinedClass typerefInfoClass = unionClass._class(getJModValue(typerefClassSpec.getModifiers()), escapeReserved(typerefClassSpec.getClassName()));
        generateTyperef(typerefInfoClass, typerefClassSpec);
        final JFieldVar typerefInfoField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, TyperefInfo.class, DataTemplateUtil.TYPEREFINFO_FIELD_NAME);
        typerefInfoField.init(JExpr._new(typerefInfoClass));
        unionClass._implements(HasTyperefInfo.class);
        final JMethod typerefInfoMethod = unionClass.method(JMod.PUBLIC, TyperefInfo.class, "typerefInfo");
        typerefInfoMethod.body()._return(typerefInfoField);
    }
}
Also used : HashMap(java.util.HashMap) JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass) ByteString(com.linkedin.data.ByteString) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 5 with UnionTemplateSpec

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

the class TestTemplateSpecGenerator method testCustomInfoForUnionMembers.

@Test(dataProvider = "customTypeDataForUnion")
public void testCustomInfoForUnionMembers(final List<DataSchema> customTypedSchemas) {
    final UnionDataSchema union = new UnionDataSchema();
    List<UnionDataSchema.Member> members = customTypedSchemas.stream().map(UnionDataSchema.Member::new).collect(Collectors.toCollection(ArrayList::new));
    union.setMembers(members, null);
    final TyperefDataSchema typeref = new TyperefDataSchema(new Name(INPUT_SCHEMA_NAME));
    typeref.setReferencedType(union);
    final TemplateSpecGenerator generator = new TemplateSpecGenerator(_resolver);
    final UnionTemplateSpec spec = (UnionTemplateSpec) generator.generate(typeref, _location);
    for (int i = 0; i < customTypedSchemas.size(); ++i) {
        Assert.assertNotNull(spec.getMembers().get(i).getCustomInfo());
        Assert.assertEquals(spec.getMembers().get(i).getCustomInfo().getCustomClass().getClassName(), CustomTypeUtil.getJavaCustomTypeClassNameFromSchema((TyperefDataSchema) customTypedSchemas.get(i)));
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) Name(com.linkedin.data.schema.Name) Test(org.testng.annotations.Test)

Aggregations

UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)8 JDefinedClass (com.sun.codemodel.JDefinedClass)4 ByteString (com.linkedin.data.ByteString)3 TyperefTemplateSpec (com.linkedin.pegasus.generator.spec.TyperefTemplateSpec)3 JClass (com.sun.codemodel.JClass)3 JMethod (com.sun.codemodel.JMethod)3 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)2 JFieldVar (com.sun.codemodel.JFieldVar)2 JVar (com.sun.codemodel.JVar)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)1 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 Name (com.linkedin.data.schema.Name)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1