Skip to content

Commit

Permalink
Fix default operation issue (#948)
Browse files Browse the repository at this point in the history
* Fix default operation issue

* clean

* Fixed
  • Loading branch information
SimonCockx authored Mar 7, 2025
1 parent 915c07a commit 9b11a92
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ class ExpressionGeneratorTest {



@Test
def void testDefaultWithMetaCoercion() {
val expr = '''
foo default bar
'''

val expected = '''
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.metafields.FieldWithMetaString;
{
FieldWithMetaString foo;
FieldWithMetaString bar;
final FieldWithMetaString fieldWithMetaString = MapperS.of(foo).getOrDefault(bar);
return fieldWithMetaString == null ? null : fieldWithMetaString.getValue();
}
'''

assertJavaCode(expected, expr, String, #[], #["foo string (1..1) [metadata scheme]", "bar string (1..1) [metadata scheme]"])
}

@Test
def void testFeatureCallToIncompatibleOverrideUsesCorrectGetter() {
val context = '''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,6 @@ class ExpressionGenerator extends RosettaExpressionSwitch<JavaStatementBuilder,
private def binaryExpr(RosettaBinaryOperation expr, Context context) {
val left = expr.left
val right = expr.right
val leftRtype = typeProvider.getRMetaAnnotatedType(expr.left).RType
val rightRtype = typeProvider.getRMetaAnnotatedType(expr.right).RType
val joined = leftRtype.join(rightRtype).toJavaReferenceType
val resultType = typeProvider.getRMetaAnnotatedType(expr).toJavaReferenceType
val leftType = leftRtype.toJavaReferenceType
val rightType = rightRtype.toJavaReferenceType

switch expr.operator {
case "and",
Expand All @@ -365,17 +359,23 @@ class ExpressionGenerator extends RosettaExpressionSwitch<JavaStatementBuilder,
case "-",
case "*",
case "/": {
val leftRtype = typeProvider.getRMetaAnnotatedType(expr.left).RType
val rightRtype = typeProvider.getRMetaAnnotatedType(expr.right).RType
val leftType = leftRtype.toJavaReferenceType
val rightType = rightRtype.toJavaReferenceType
val joinedWithoutMeta = leftRtype.join(rightRtype).toJavaReferenceType
val resultType = typeProvider.getRMetaAnnotatedType(expr).toJavaReferenceType
val method = switch expr.operator {
case "+": "add"
case "-": "subtract"
case "*": "multiply"
case "/": "divide"
}
if (leftType.extendsNumber && rightType.extendsNumber) {
val leftCode = javaCode(left, MAPPER.wrapExtends(joined), context.scope)
val rightCode = javaCode(right, MAPPER.wrapExtends(joined), context.scope)
val leftCode = javaCode(left, MAPPER.wrapExtends(joinedWithoutMeta), context.scope)
val rightCode = javaCode(right, MAPPER.wrapExtends(joinedWithoutMeta), context.scope)
leftCode
.then(rightCode, [l, r|JavaExpression.from('''«MapperMaths».<«resultType», «joined», «joined»>«method»(«l», «r»)''', MAPPER_S.wrap(resultType))], context.scope)
.then(rightCode, [l, r|JavaExpression.from('''«MapperMaths».<«resultType», «joinedWithoutMeta», «joinedWithoutMeta»>«method»(«l», «r»)''', MAPPER_S.wrap(resultType))], context.scope)
} else {
val leftCode = javaCode(left, MAPPER.wrapExtends(leftType), context.scope)
val rightCode = javaCode(right, MAPPER.wrapExtends(rightType), context.scope)
Expand All @@ -385,19 +385,28 @@ class ExpressionGenerator extends RosettaExpressionSwitch<JavaStatementBuilder,
}
case "contains",
case "disjoint": {
val leftRMetaType = typeProvider.getRMetaAnnotatedType(expr.left)
val rightRMetaType = typeProvider.getRMetaAnnotatedType(expr.right)
val joined = leftRMetaType.joinMetaAnnotatedTypes(rightRMetaType).toJavaReferenceType

val leftCode = javaCode(left, MAPPER.wrapExtends(joined), context.scope)
val rightCode = javaCode(right, MAPPER.wrapExtends(joined), context.scope)
leftCode
.then(rightCode, [l, r|JavaExpression.from('''«runtimeMethod(expr.operator)»(«l», «r»)''', COMPARISON_RESULT)], context.scope)
}
case "default": {
val leftRMetaType = typeProvider.getRMetaAnnotatedType(expr.left)
val rightRMetaType = typeProvider.getRMetaAnnotatedType(expr.right)
val joined = leftRMetaType.joinMetaAnnotatedTypes(rightRMetaType).toJavaReferenceType

val leftCode = javaCode(left, MAPPER.wrapExtends(joined), context.scope)
if (left.isMulti) {
val rightCode = javaCode(right, MAPPER.wrapExtends(joined), context.scope)

leftCode
.then(rightCode, [l, r| new JavaConditionalExpression(JavaExpression.from('''«l».getMulti().isEmpty()''', JavaPrimitiveType.BOOLEAN),r ,l , typeUtil)], context.scope)
} else {
val resultType = typeProvider.getRMetaAnnotatedType(expr).toJavaReferenceType
val rightCode = javaCode(right, joined, context.scope)

leftCode
Expand All @@ -406,7 +415,7 @@ class ExpressionGenerator extends RosettaExpressionSwitch<JavaStatementBuilder,
}
case "join": {
val leftCode = javaCode(left, MAPPER_C.wrapExtends(STRING), context.scope)
val rightCode = expr.right === null ? JavaExpression.from('''«MapperS».of("")''', resultType) : javaCode(right, MAPPER_S.wrap(STRING), context.scope)
val rightCode = expr.right === null ? JavaExpression.from('''«MapperS».of("")''', MAPPER_S.wrap(STRING)) : javaCode(right, MAPPER_S.wrap(STRING), context.scope)
leftCode
.then(rightCode, [l, r|JavaExpression.from('''«l».join(«r»)''', MAPPER_S.wrap(STRING))], context.scope)
}
Expand All @@ -416,6 +425,9 @@ class ExpressionGenerator extends RosettaExpressionSwitch<JavaStatementBuilder,
case "<=",
case ">",
case ">=": {
val leftRtype = typeProvider.getRMetaAnnotatedType(expr.left).RType
val rightRtype = typeProvider.getRMetaAnnotatedType(expr.right).RType
val joinedWithoutMeta = leftRtype.join(rightRtype).toJavaReferenceType
val method = switch expr.operator {
case "=": 'areEqual'
case "<>": 'notEqual'
Expand All @@ -430,8 +442,8 @@ class ExpressionGenerator extends RosettaExpressionSwitch<JavaStatementBuilder,
} else {
CardinalityModifier.ALL
}
val leftCode = javaCode(left, MAPPER.wrapExtends(joined), context.scope)
val rightCode = javaCode(right, MAPPER.wrapExtends(joined), context.scope)
val leftCode = javaCode(left, MAPPER.wrapExtends(joinedWithoutMeta), context.scope)
val rightCode = javaCode(right, MAPPER.wrapExtends(joinedWithoutMeta), context.scope)
leftCode
.then(rightCode, [l, r|JavaExpression.from('''«runtimeMethod(method)»(«l», «r», «toCardinalityOperator(modifier, defaultModifier)»)''', COMPARISON_RESULT)], context.scope)
}
Expand Down

0 comments on commit 9b11a92

Please sign in to comment.