@@ -46,6 +46,7 @@ import com.regnosys.rosetta.rosetta.expression.MinOperation
4646import com.regnosys.rosetta.rosetta.expression.MaxOperation
4747import com.regnosys.rosetta.rosetta.expression.SwitchOperation
4848import com.regnosys.rosetta.rosetta.expression.SwitchCaseGuard
49+ import com.regnosys.rosetta.rosetta.expression.SwitchCaseOrDefault
4950import com.regnosys.rosetta.rosetta.simple.Attribute
5051import com.regnosys.rosetta.rosetta.simple.Condition
5152import com.regnosys.rosetta.rosetta.simple.Data
@@ -61,7 +62,7 @@ class PythonExpressionGenerator {
6162
6263 public var List<String > importsFound
6364 public var ifCondBlocks = new ArrayList<String > ()
64- public var switchCond = false
65+ public var isSwitchCond = false
6566
6667 def String generateConditions (Data cls ) {
6768 var nConditions = 0 ;
@@ -198,12 +199,12 @@ class PythonExpressionGenerator {
198199
199200 private def generateIfThenElseOrSwitch (Condition c ) {
200201 ifCondBlocks. clear()
201- switchCond= false
202+ isSwitchCond= false
203+
202204 var expr = generateExpression(c. expression, 0 , false )
203- if (switchCond) return expr
204205
205206 var blocks = (ifCondBlocks. isEmpty()) ? " " : ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
206- return ' ' ' «blocks» return «expr»
207+ return isSwitchCond ? expr : ' ' ' «blocks» return «expr»
207208 ' ' '
208209 }
209210
@@ -286,20 +287,16 @@ class PythonExpressionGenerator {
286287 private def String generateSwitchOperation (SwitchOperation expr , int ifLevel , boolean isLambda ) {
287288 val attr = generateExpression(expr. argument, 0 , isLambda)
288289
289- var switchCases= expr. cases
290-
291290 var _thenFuncsBuilder = new StringConcatenation ()
292291 var _switchLogicBuilder= new StringConcatenation ()
293292
294293 val indent = " "
295- switchCond = true
294+ isSwitchCond = true
296295
297- for (i : 0 .. < switchCases. size) {
298- val funcName= if (switchCases. get(i). isDefault()) " _then_default" else " _then_" + (i+ 1 )
299- val thenExprDef= if (switchCases. get(i). isDefault())
300- generateExpression(expr. getDefault(), 0 , isLambda)
301- else
302- generateExpression(switchCases. get(i). getExpression(), ifLevel + 1 , isLambda)
296+ for (pair : expr. cases. indexed) {
297+ val currentCase = pair. value as SwitchCaseOrDefault
298+ val funcName= (currentCase. isDefault()) ? " _then_default" : " _then_" + (pair. key+ 1 )
299+ val thenExprDef= (currentCase. isDefault()) ? generateExpression(expr. getDefault(), 0 , isLambda) : generateExpression(currentCase. getExpression(), ifLevel + 1 , isLambda)
303300
304301 _thenFuncsBuilder. append(indent)
305302 _thenFuncsBuilder. append(" def " + funcName + " ():" )
@@ -308,7 +305,7 @@ class PythonExpressionGenerator {
308305 _thenFuncsBuilder. append(" return " + thenExprDef)
309306 _thenFuncsBuilder. newLine
310307
311- if (switchCases . get(i) . isDefault()){
308+ if (currentCase . isDefault()){
312309 // Default else
313310 _switchLogicBuilder. append(indent)
314311 _switchLogicBuilder. append(" else:" )
@@ -319,9 +316,9 @@ class PythonExpressionGenerator {
319316 _switchLogicBuilder. append(" ()" )
320317 }
321318 else {
322- val guard = switchCases . get(i) . getGuard()
319+ val guard = currentCase . getGuard()
323320
324- val prefix = (i == 0 ) ? " if " : " elif "
321+ val prefix = (pair . key == 0 ) ? " if " : " elif "
325322 _switchLogicBuilder. append(indent)
326323 _switchLogicBuilder. append(prefix)
327324 if (guard. getLiteralGuard() !== null ) {
0 commit comments