(expr: CELExpr, negative: boolean = false)
| 78 | // Convert common expr to simple expr |
| 79 | export const resolveCELExpr = (expr: CELExpr): SimpleExpr => { |
| 80 | const dfs = (expr: CELExpr, negative: boolean = false): SimpleExpr => { |
| 81 | if (expr.exprKind?.case !== "callExpr") { |
| 82 | // If no callExpr, we treat it as a raw string. |
| 83 | return resolveRawStringExpr(expr); |
| 84 | } |
| 85 | const callExpr = expr.exprKind.value; |
| 86 | |
| 87 | try { |
| 88 | const { args } = callExpr; |
| 89 | const operator = callExpr.function as Operator; |
| 90 | if (isLogicalOperator(operator)) { |
| 91 | const group: ConditionGroupExpr = { |
| 92 | type: ExprType.ConditionGroup, |
| 93 | operator, |
| 94 | args: [], |
| 95 | }; |
| 96 | const [left, right] = args; |
| 97 | const sub = (subTree: CELExpr, expand: boolean) => { |
| 98 | const subExpr = dfs(subTree); |
| 99 | if ( |
| 100 | expand && |
| 101 | isConditionGroupExpr(subExpr) && |
| 102 | subExpr.operator === operator |
| 103 | ) { |
| 104 | group.args.push(...subExpr.args); |
| 105 | } else { |
| 106 | group.args.push(subExpr); |
| 107 | } |
| 108 | }; |
| 109 | sub(left, true); |
| 110 | sub(right, false); |
| 111 | return group; |
| 112 | } |
| 113 | if (isNegativeOperator(operator)) { |
| 114 | return dfs(args[0], true); |
| 115 | } |
| 116 | if (isEqualityOperator(operator)) { |
| 117 | return resolveEqualityExpr(expr); |
| 118 | } |
| 119 | if (isCompareOperator(operator)) { |
| 120 | return resolveCompareExpr(expr); |
| 121 | } |
| 122 | if (isStringOperator(operator)) { |
| 123 | return resolveStringExpr(expr, negative); |
| 124 | } |
| 125 | if (isCollectionOperator(operator)) { |
| 126 | return resolveCollectionExpr(expr, negative); |
| 127 | } |
| 128 | throw new Error(`unsupported expr "${JSON.stringify(expr)}"`); |
| 129 | } catch { |
| 130 | // Any error occurs, we treat it as a raw string. |
| 131 | return resolveRawStringExpr(expr); |
| 132 | } |
| 133 | }; |
| 134 | return dfs(expr); |
| 135 | }; |
| 136 |
no test coverage detected