Skip to content

Commit

Permalink
Story/1045/cdm serialization (#889)
Browse files Browse the repository at this point in the history
* add new annotations

* add RuneDataType

* Handle run annotations and rune meta

* add run annotations to builder methods

* remove duplicate meta fields key

* remove properly!

* add RuneAttribute annotation to multi cardinality setters

* add RuneDataType to MetaFields

* Add @RuneAttribute to all meta fields

* fix typo in scheme annotations

* unwrap meta @DaTa wrappers for object types using @RuneMetaType
annotation

* Changes to handle scoped keys and references for Rune attributes

* Fix annotations for MetaAndTemplateFields

* add comments to meta fields classes

* rename scoped key accessor methods

* fix whitespace

* fix whitespace
  • Loading branch information
davidalk authored Jan 6, 2025
1 parent 3a9be82 commit 3c5751f
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.regnosys.rosetta.generator.java.types.JavaPojoProperty
import java.util.Collection
import com.rosetta.util.types.JavaClass
import com.regnosys.rosetta.generator.java.types.RJavaEnum
import com.regnosys.rosetta.types.RDataType

class ModelObjectBoilerPlate {

Expand All @@ -43,6 +44,19 @@ class ModelObjectBoilerPlate {
return prop.name
}

def String javaRuneAnnotation(JavaPojoProperty prop) {
if (prop.type == REFERENCE) {
return '@ref:scoped'
} else
return prop.runeName
}

def boolean addRuneMetaAnnotation(JavaPojoProperty prop) {
return prop.type == REFERENCE ||
prop.type==META_FIELDS ||
(javaRuneAnnotation(prop) == "@data" && prop.type.isValueRosettaModelObject)
}

def StringConcatenationClient implementsClause(JavaPojoInterface javaType) {
'''«FOR i : javaType.interfaces SEPARATOR ', '»«i»«ENDFOR»'''
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package com.regnosys.rosetta.generator.java.object

import com.rosetta.model.lib.meta.Key
import com.rosetta.model.lib.process.BuilderMerger
import java.util.ArrayList
import java.util.function.Consumer
import java.util.stream.Collectors
import org.eclipse.xtend2.lib.StringConcatenationClient

import com.regnosys.rosetta.generator.java.JavaScope
import com.regnosys.rosetta.generator.java.types.JavaTypeTranslator
import javax.inject.Inject
import com.rosetta.model.lib.annotations.RosettaAttribute
import com.rosetta.model.lib.RosettaModelObjectBuilder
import com.regnosys.rosetta.generator.java.types.JavaTypeUtil
import com.regnosys.rosetta.generator.java.types.JavaPojoProperty
import com.rosetta.util.types.JavaType
import com.rosetta.util.types.JavaClass
import com.regnosys.rosetta.generator.java.types.JavaPojoInterface
import com.regnosys.rosetta.generator.java.statement.builder.JavaExpression
import com.regnosys.rosetta.generator.java.expression.TypeCoercionService
import com.regnosys.rosetta.generator.java.statement.JavaEnhancedForLoop
import com.regnosys.rosetta.generator.java.statement.JavaIfThenStatement
import com.regnosys.rosetta.generator.java.statement.builder.JavaExpression
import com.regnosys.rosetta.generator.java.statement.builder.JavaIfThenElseBuilder
import com.regnosys.rosetta.generator.java.statement.builder.JavaThis
import com.regnosys.rosetta.generator.java.statement.builder.JavaVariable
import com.regnosys.rosetta.generator.java.types.JavaPojoInterface
import com.regnosys.rosetta.generator.java.types.JavaPojoProperty
import com.regnosys.rosetta.generator.java.types.JavaTypeTranslator
import com.regnosys.rosetta.generator.java.types.JavaTypeUtil
import com.regnosys.rosetta.generator.java.types.RJavaWithMetaValue
import com.regnosys.rosetta.generator.java.statement.JavaIfThenStatement
import com.rosetta.model.lib.RosettaModelObjectBuilder
import com.rosetta.model.lib.annotations.RosettaAttribute
import com.rosetta.model.lib.annotations.RuneAttribute
import com.rosetta.model.lib.annotations.RuneMetaType
import com.rosetta.model.lib.meta.Key
import com.rosetta.model.lib.process.BuilderMerger
import com.rosetta.util.types.JavaClass
import com.rosetta.util.types.JavaPrimitiveType
import com.regnosys.rosetta.generator.java.statement.builder.JavaThis
import com.regnosys.rosetta.generator.java.statement.JavaEnhancedForLoop
import com.regnosys.rosetta.generator.java.statement.builder.JavaIfThenElseBuilder
import com.rosetta.util.types.JavaType
import java.util.ArrayList
import java.util.Collections
import com.regnosys.rosetta.generator.java.statement.builder.JavaConditionalExpression
import java.util.function.Consumer
import java.util.stream.Collectors
import javax.inject.Inject
import org.eclipse.xtend2.lib.StringConcatenationClient

class ModelObjectBuilderGenerator {

Expand Down Expand Up @@ -124,6 +124,8 @@ class ModelObjectBuilderGenerator {

@Override
@«RosettaAttribute»("«prop.javaAnnotation»")
@«RuneAttribute»("«prop.javaRuneAnnotation»")
«IF prop.addRuneMetaAnnotation»@«RuneMetaType»«ENDIF»
public «prop.toBuilderTypeExt» «prop.getterName»() «field.completeAsReturn.toBlock»
«IF prop.type.isRosettaModelObject»
«IF !prop.type.isList»
Expand Down Expand Up @@ -258,7 +260,11 @@ class ModelObjectBuilderGenerator {
«val addMethodScope = scope.methodScope(addMethodName)»
«val addMethodArg = new JavaVariable(addMethodScope.createUniqueIdentifier(currentProp.name.toFirstLower), itemType)»
@Override
«IF isMainProp»@«RosettaAttribute»("«currentProp.javaAnnotation»"ENDIF»
«IF isMainProp»
@«RosettaAttribute»("«currentProp.javaAnnotation»")
@«RuneAttribute»("«currentProp.javaRuneAnnotation»")
«IF currentProp.addRuneMetaAnnotation»@«RuneMetaType»«ENDIF»
«ENDIF»
public «builderType» «addMethodName»(«itemType» «addMethodArg») «
(if (isMainProp) {
new JavaIfThenStatement(
Expand Down Expand Up @@ -381,6 +387,10 @@ class ModelObjectBuilderGenerator {
«val setMultiMethodScope = scope.methodScope(setMethodName)»
«val setMultiMethodArg = new JavaVariable(setMultiMethodScope.createUniqueIdentifier(currentProp.name.toFirstLower + "s"), propType)»
@Override
«IF isMainProp»
@«RuneAttribute»("«currentProp.javaRuneAnnotation»")
«IF currentProp.addRuneMetaAnnotation»@«RuneMetaType»«ENDIF»
«ENDIF»
public «builderType» «setMethodName»(«propType» «setMultiMethodArg») «
(if (isMainProp) {
new JavaIfThenElseBuilder(
Expand Down Expand Up @@ -459,7 +469,11 @@ class ModelObjectBuilderGenerator {
«val setMethodScope = scope.methodScope(setMethodName)»
«val setMethodArg = new JavaVariable(setMethodScope.createUniqueIdentifier(currentProp.name.toFirstLower), propType)»
@Override
«IF isMainProp»@«RosettaAttribute»("«currentProp.javaAnnotation»"ENDIF»
«IF isMainProp»
@«RosettaAttribute»("«currentProp.javaAnnotation»")
@«RuneAttribute»("«currentProp.javaRuneAnnotation»")
«IF currentProp.addRuneMetaAnnotation»@«RuneMetaType»«ENDIF»
«ENDIF»
public «builderType» «setMethodName»(«propType» «setMethodArg») «
(if (isMainProp) {
JavaExpression.from('''this.«field» = «setMethodArg» == null ? null : «setMethodArg.toBuilder»''', JavaPrimitiveType.VOID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import com.regnosys.rosetta.generator.java.types.JavaPojoProperty
import com.regnosys.rosetta.generator.java.statement.builder.JavaExpression
import com.regnosys.rosetta.generator.java.expression.TypeCoercionService
import com.regnosys.rosetta.generator.java.statement.builder.JavaVariable
import com.rosetta.model.lib.annotations.RuneDataType
import com.regnosys.rosetta.config.RosettaConfiguration
import com.rosetta.model.lib.annotations.RuneAttribute
import com.rosetta.model.lib.annotations.RuneMetaType

class ModelObjectGenerator {

Expand All @@ -36,6 +40,7 @@ class ModelObjectGenerator {
@Inject extension JavaTypeTranslator
@Inject extension JavaTypeUtil
@Inject extension TypeCoercionService
@Inject RosettaConfiguration rosettaConfiguration

def generate(RootPackage root, IFileSystemAccess2 fsa, RDataType t, String version) {
fsa.generateFile(root.child(t.name + '.java').withForwardSlashes,
Expand All @@ -58,6 +63,7 @@ class ModelObjectGenerator {
'''
«javaType.javadoc»
@«RosettaDataType»(value="«javaType.rosettaName»", builder=«javaType.toBuilderImplType».class, version="«javaType.version»")
@«RuneDataType»(value="«javaType.rosettaName»", model="«rosettaConfiguration.model.name»", builder=«javaType.toBuilderImplType».class, version="«javaType.version»")
public interface «javaType» extends «implementsClause(javaType)» {
«metaType» «metaDataIdentifier» = new «metaType»();
Expand Down Expand Up @@ -165,6 +171,7 @@ class ModelObjectGenerator {
}

@Override
@«RuneAttribute»("@type")
default Class<? extends «javaType»> getType() {
return «javaType».class;
}
Expand Down Expand Up @@ -227,6 +234,8 @@ class ModelObjectGenerator {
«val field = new JavaVariable(scope.getIdentifierOrThrow(prop), prop.type)»
@Override
@«RosettaAttribute»("«prop.javaAnnotation»")
@«RuneAttribute»("«prop.javaRuneAnnotation»")
«IF prop.addRuneMetaAnnotation»@«RuneMetaType»«ENDIF»
public «prop.type» «prop.getterName»() «field.completeAsReturn.toBlock»

«IF !extended»«derivedIncompatibleGettersForProperty(field, prop, scope)»«ENDIF»
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@

public class JavaPojoProperty {
private final String name;
private final String runeName;
private final String compatibilityName;
private final JavaType type;
private final String javadoc;
private final JavaPojoProperty parentProperty;
private final AttributeMeta meta; // used in `process` method
private final boolean hasLocation; // used in builder `getOrCreate`

public JavaPojoProperty(String name, String compatibilityName, JavaType type, String javadoc, AttributeMeta meta, boolean hasLocation) {
this(name, compatibilityName, type, javadoc, meta, hasLocation, null);
public JavaPojoProperty(String name, String runeName, String compatibilityName, JavaType type, String javadoc, AttributeMeta meta, boolean hasLocation) {
this(name, runeName, compatibilityName, type, javadoc, meta, hasLocation, null);
}
private JavaPojoProperty(String name, String compatibilityName, JavaType type, String javadoc, AttributeMeta meta, boolean hasLocation, JavaPojoProperty parentProperty) {
private JavaPojoProperty(String name, String runeName, String compatibilityName, JavaType type, String javadoc, AttributeMeta meta, boolean hasLocation, JavaPojoProperty parentProperty) {
this.name = name;
this.runeName = runeName;
this.compatibilityName = compatibilityName;
this.type = type;
this.javadoc = javadoc;
Expand All @@ -47,7 +49,7 @@ private JavaPojoProperty(String name, String compatibilityName, JavaType type, S
this.parentProperty = parentProperty;
}
public JavaPojoProperty specialize(String compatibilityName, JavaType newType, String newJavadoc, AttributeMeta newMeta, boolean newHasLocation) {
return new JavaPojoProperty(name, compatibilityName, newType, newJavadoc, newMeta, newHasLocation, this);
return new JavaPojoProperty(name, runeName, compatibilityName, newType, newJavadoc, newMeta, newHasLocation, this);
}

public boolean isCompatibleWithParent() {
Expand All @@ -57,6 +59,9 @@ public boolean isCompatibleWithParent() {
public String getName() {
return name;
}
public String getRuneName() {
return runeName;
}
public String getGetterName() {
return "get" + StringUtils.capitalize(compatibilityName);
}
Expand Down Expand Up @@ -97,7 +102,7 @@ public String toString() {
}
@Override
public int hashCode() {
return Objects.hash(compatibilityName, hasLocation, javadoc, meta, name, parentProperty, type);
return Objects.hash(compatibilityName, hasLocation, javadoc, meta, name, runeName, parentProperty, type);
}
@Override
public boolean equals(Object obj) {
Expand All @@ -109,7 +114,7 @@ public boolean equals(Object obj) {
return false;
JavaPojoProperty other = (JavaPojoProperty) obj;
return Objects.equals(compatibilityName, other.compatibilityName) && hasLocation == other.hasLocation
&& Objects.equals(javadoc, other.javadoc) && meta == other.meta && Objects.equals(name, other.name)
&& Objects.equals(javadoc, other.javadoc) && meta == other.meta && Objects.equals(name, other.name) && Objects.equals(runeName, other.runeName)
&& Objects.equals(parentProperty, other.parentProperty) && Objects.equals(type, other.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public List<JavaClass<?>> getInterfaces() {
@Override
public Collection<JavaPojoProperty> getOwnProperties() {
return List.of(
new JavaPojoProperty("value", "value", valueType, null, null, false),
new JavaPojoProperty("meta", "meta", typeUtil.META_FIELDS, null, null, false)
new JavaPojoProperty("value", "@data", "value", valueType, null, null, false),
new JavaPojoProperty("meta", "meta", "meta", typeUtil.META_FIELDS, null, null, false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ private void initializeProperties() {
type.getOwnAttributes().forEach(attr -> {
String name = attr.getName();
JavaType type = typeTranslator.toMetaJavaType(attr);
addPropertyIfNecessary(name, type, ModelGeneratorUtil.javadoc(attr.getDefinition(), attr.getDocReferences(), null), attr.getRMetaAnnotatedType().hasMetaAttribute("id") ? AttributeMeta.GLOBAL_KEY_FIELD : null, attr.getRMetaAnnotatedType().hasMetaAttribute("location"));
addPropertyIfNecessary(name, name, type, ModelGeneratorUtil.javadoc(attr.getDefinition(), attr.getDocReferences(), null), attr.getRMetaAnnotatedType().hasMetaAttribute("id") ? AttributeMeta.GLOBAL_KEY_FIELD : null, attr.getRMetaAnnotatedType().hasMetaAttribute("location"));
});
if (type.hasMetaAttribute("key")) {
JavaType metaFieldsType = type.hasMetaAttribute("template") ? typeUtil.META_AND_TEMPLATE_FIELDS : typeUtil.META_FIELDS;
addPropertyIfNecessary("meta", metaFieldsType, null, null, false);
addPropertyIfNecessary("meta", "meta", metaFieldsType, null, null, false);
}
}
}
private void addPropertyIfNecessary(String name, JavaType type, String javadoc, AttributeMeta meta, boolean hasLocation) {
private void addPropertyIfNecessary(String name, String runeName, JavaType type, String javadoc, AttributeMeta meta, boolean hasLocation) {
JavaPojoProperty parentProperty = allProperties.get(name);
if (parentProperty == null) {
JavaPojoProperty newProperty = new JavaPojoProperty(
name,
runeName,
name,
type,
javadoc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public List<JavaClass<?>> getInterfaces() {
@Override
public Collection<JavaPojoProperty> getOwnProperties() {
return List.of(
new JavaPojoProperty("value", "value", valueType, null, null, false),
new JavaPojoProperty("globalReference", "globalReference", javaTypeUtil.STRING, null, AttributeMeta.META, false),
new JavaPojoProperty("externalReference", "externalReference", javaTypeUtil.STRING, null, AttributeMeta.META, false),
new JavaPojoProperty("reference", "reference", javaTypeUtil.REFERENCE, null, null, false)
new JavaPojoProperty("value", "@data", "value", valueType, null, null, false),
new JavaPojoProperty("globalReference", "@ref", "globalReference", javaTypeUtil.STRING, null, AttributeMeta.META, false),
new JavaPojoProperty("externalReference", "@ref:external", "externalReference", javaTypeUtil.STRING, null, AttributeMeta.META, false),
new JavaPojoProperty("reference", null, "reference", javaTypeUtil.REFERENCE, null, null, false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Deprecated
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface RosettaAttribute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.rosetta.model.lib.RosettaModelObjectBuilder;

@Deprecated
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface RosettaDataType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.rosetta.model.lib.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface RuneAttribute {

String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.rosetta.model.lib.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import com.rosetta.model.lib.RosettaModelObjectBuilder;

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface RuneDataType {
String value() default "";

String model() default "";

Class<? extends RosettaModelObjectBuilder> builder();

String version() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.rosetta.model.lib.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface RuneMetaType {

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.rosetta.model.lib.RosettaModelObjectBuilder;
import com.rosetta.model.lib.annotations.RosettaAttribute;
import com.rosetta.model.lib.annotations.RosettaDataType;
import com.rosetta.model.lib.annotations.RuneAttribute;
import com.rosetta.model.lib.annotations.RuneDataType;
import com.rosetta.model.lib.path.RosettaPath;
import com.rosetta.model.lib.process.BuilderMerger;
import com.rosetta.model.lib.process.BuilderProcessor;
Expand All @@ -40,6 +42,7 @@
* for inside that TradeableProduct
*/
@RosettaDataType(value = "Reference", builder = Reference.ReferenceBuilderImpl.class)
@RuneDataType(value = "Reference", builder = Reference.ReferenceBuilderImpl.class, version="0.0.0")
public interface Reference extends RosettaModelObject {
Reference build();

Expand Down Expand Up @@ -103,6 +106,7 @@ public String getPointsTo() {
}

@RosettaAttribute("value")
@RuneAttribute("@ref:scoped")
public String getReference() {
return reference;
}
Expand Down Expand Up @@ -175,6 +179,7 @@ public String getPointsTo() {
}

@RosettaAttribute("value")
@RuneAttribute("@ref:scoped")
public String getReference() {
return reference;
}
Expand All @@ -185,6 +190,7 @@ public Reference build() {
}

@RosettaAttribute("value")
@RuneAttribute("@ref:scoped")
public ReferenceBuilder setReference(String reference) {
this.reference = reference;
return this;
Expand Down
Loading

0 comments on commit 3c5751f

Please sign in to comment.