( expr: SimpleExpr )
| 33 | |
| 34 | // Build CEL expr according to simple expr |
| 35 | export const buildCELExpr = async ( |
| 36 | expr: SimpleExpr |
| 37 | ): Promise<CELExpr | undefined> => { |
| 38 | const convert = async (expr: SimpleExpr): Promise<CELExpr | undefined> => { |
| 39 | if (isConditionGroupExpr(expr)) return convertGroup(expr); |
| 40 | if (isConditionExpr(expr)) return convertCondition(expr); |
| 41 | if (isRawStringExpr(expr)) { |
| 42 | if (!expr.content) { |
| 43 | return undefined; |
| 44 | } |
| 45 | const request = create(BatchParseRequestSchema, { |
| 46 | expressions: [expr.content], |
| 47 | }); |
| 48 | const response = await celServiceClientConnect.batchParse(request, { |
| 49 | contextValues: createContextValues().set(silentContextKey, true), |
| 50 | }); |
| 51 | return head(response.expressions); |
| 52 | } |
| 53 | throw new Error(`unexpected type "${String(expr)}"`); |
| 54 | }; |
| 55 | const convertCondition = (condition: ConditionExpr): CELExpr => { |
| 56 | if (isEqualityExpr(condition)) { |
| 57 | const { operator, args } = condition; |
| 58 | const [factor, value] = args; |
| 59 | return wrapCallExpr(operator, [ |
| 60 | wrapIdentExpr(factor), |
| 61 | wrapConstExpr(value), |
| 62 | ]); |
| 63 | } |
| 64 | if (isCompareExpr(condition)) { |
| 65 | const { operator, args } = condition; |
| 66 | const [factor, value] = args; |
| 67 | return wrapCallExpr(operator, [ |
| 68 | wrapIdentExpr(factor), |
| 69 | wrapConstExpr(value), |
| 70 | ]); |
| 71 | } |
| 72 | if (isStringExpr(condition)) { |
| 73 | const { operator, args } = condition; |
| 74 | const [factor, value] = args; |
| 75 | if (operator === "@not_contains") { |
| 76 | return wrapCallExpr("!_", [ |
| 77 | wrapCallExpr( |
| 78 | "contains", |
| 79 | [wrapConstExpr(value)], |
| 80 | wrapIdentExpr(factor) |
| 81 | ), |
| 82 | ]); |
| 83 | } |
| 84 | return wrapCallExpr( |
| 85 | operator, |
| 86 | [wrapConstExpr(value)], |
| 87 | wrapIdentExpr(factor) |
| 88 | ); |
| 89 | } |
| 90 | if (isCollectionExpr(condition)) { |
| 91 | const { operator, args } = condition; |
| 92 | const [factor, values] = args; |
no test coverage detected