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));
}
}
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;
}
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;
}
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);
}
}
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)));
}
}
Aggregations