@@ -180,16 +180,26 @@ class PythonExpressionGenerator {
180180 }
181181
182182 private def getGuardExpression (SwitchCaseGuard caseGuard , boolean isLambda ){
183- val enumGuard= caseGuard. getEnumGuard
184- if (enumGuard!== null ){
185- return ' ' ' switchAttribute == rune_resolve_attr(«generateEnumString(enumGuard)»,"«caseGuard.getEnumGuard.getName()»")' ' '
183+ if (caseGuard == = null ) {
184+ throw new UnsupportedOperationException (" Null SwitchCaseGuard" )
186185 }
187- if (caseGuard. getChoiceOptionGuard!== null ){
188- return ' ' ' rune_resolve_attr(switchAttribute,"«caseGuard.getChoiceOptionGuard.getName()»")' ' '
186+ val literalGuard = caseGuard. getLiteralGuard();
187+ if (literalGuard !== null ) {
188+ return ' ' ' switchAttribute == «generateExpression(literalGuard, 0, isLambda)»' ' '
189189 }
190- if (caseGuard. getSymbolGuard!== null ){
191- return ' ' ' rune_resolve_attr(switchAttribute,"«caseGuard.getSymbolGuard.getName()»")' ' '
190+ val enumGuard = caseGuard. getEnumGuard
191+ if (enumGuard !== null ){
192+ return ' ' ' switchAttribute == rune_resolve_attr(«generateEnumString(enumGuard)»,"«enumGuard.getName()»")' ' '
192193 }
194+ val optionGuard = caseGuard. getChoiceOptionGuard
195+ if (optionGuard !== null ){
196+ return ' ' ' rune_resolve_attr(switchAttribute,"«optionGuard.getName()»")' ' '
197+ }
198+ val dataGuard = caseGuard. getDataGuard
199+ if (dataGuard !== null ){
200+ return ' ' ' rune_resolve_attr(switchAttribute,"«dataGuard.getName()»")' ' '
201+ }
202+ throw new UnsupportedOperationException (" Unsupported SwitchCaseGuard type" )
193203 }
194204
195205 private def String generateSwitchOperation (SwitchOperation expr , int ifLevel , boolean isLambda ) {
@@ -204,8 +214,8 @@ class PythonExpressionGenerator {
204214
205215 for (pair : expr. cases. indexed) {
206216 val currentCase = pair. value
207- val funcName= (currentCase. isDefault()) ? " _then_default" : " _then_" + (pair. key+ 1 )
208- val thenExprDef= (currentCase. isDefault()) ? generateExpression(expr. getDefault(), 0 , isLambda) : generateExpression(currentCase. getExpression(), ifLevel + 1 , isLambda)
217+ val funcName = (currentCase. isDefault()) ? " _then_default" : " _then_" + (pair. key+ 1 )
218+ val thenExprDef = (currentCase. isDefault()) ? generateExpression(expr. getDefault(), 0 , isLambda) : generateExpression(currentCase. getExpression(), ifLevel + 1 , isLambda)
209219
210220 _thenFuncsBuilder. append(indent)
211221 _thenFuncsBuilder. append(" def " + funcName + " ():" )
@@ -214,7 +224,7 @@ class PythonExpressionGenerator {
214224 _thenFuncsBuilder. append(" return " + thenExprDef)
215225 _thenFuncsBuilder. newLine
216226
217- if (currentCase. isDefault()){
227+ if (currentCase. isDefault()) {
218228 // Default else
219229 _switchLogicBuilder. append(indent)
220230 _switchLogicBuilder. append(" else:" )
@@ -223,21 +233,12 @@ class PythonExpressionGenerator {
223233 _switchLogicBuilder. append(" return " )
224234 _switchLogicBuilder. append(funcName)
225235 _switchLogicBuilder. append(" ()" )
226- }
227- else {
228- val guard = currentCase. getGuard()
229-
236+ } else {
237+ val guard = currentCase. getGuard()
230238 val prefix = (pair. key == 0 ) ? " if " : " elif "
231239 _switchLogicBuilder. append(indent)
232240 _switchLogicBuilder. append(prefix)
233- if (guard. getLiteralGuard() !== null ) {
234- val guardExpr = generateExpression(guard. getLiteralGuard(), 0 , isLambda)
235- _switchLogicBuilder. append(" switchAttribute == " )
236- _switchLogicBuilder. append(guardExpr)
237- } else {
238- val guardExpr = getGuardExpression(guard, isLambda)
239- _switchLogicBuilder. append(guardExpr)
240- }
241+ _switchLogicBuilder. append(getGuardExpression(guard, isLambda))
241242 _switchLogicBuilder. append(" :" )
242243 _switchLogicBuilder. newLine()
243244 _switchLogicBuilder. append(indent)
0 commit comments