|
6 | 6 | -- to get the left operand instead of the operand itself. |
7 | 7 |
|
8 | 8 | local setmetatable = setmetatable |
9 | | -local Operation = require('apicast.conditions.operation') |
| 9 | +local match = ngx.re.match |
10 | 10 | local TemplateString = require('apicast.template_string') |
11 | 11 |
|
12 | 12 | local _M = {} |
13 | 13 |
|
14 | 14 | local mt = { __index = _M } |
15 | 15 |
|
| 16 | +local evaluate_func = { |
| 17 | + ['=='] = function(left, right) return tostring(left) == tostring(right) end, |
| 18 | + ['!='] = function(left, right) return tostring(left) ~= tostring(right) end, |
| 19 | + |
| 20 | + -- Implemented on top of ngx.re.match. Returns true when there is a match and |
| 21 | + -- false otherwise. |
| 22 | + ['matches'] = function(left, right) |
| 23 | + return (match(tostring(left), tostring(right)) and true) or false |
| 24 | + end |
| 25 | +} |
| 26 | + |
16 | 27 | local function new(evaluate_left_side_func, op, value, value_type) |
17 | 28 | local self = setmetatable({}, mt) |
18 | 29 |
|
19 | 30 | self.evaluate_left_side_func = evaluate_left_side_func |
20 | | - self.op = op |
21 | | - self.value = value |
22 | | - self.value_type = value_type |
| 31 | + self.evaluate_func = evaluate_func[op] |
| 32 | + assert(self.evaluate_func, 'Unsupported operation: ' .. (op or 'nil')) |
| 33 | + self.right_template = TemplateString.new(value, value_type or 'plain') |
23 | 34 |
|
24 | 35 | return self |
25 | 36 | end |
@@ -55,21 +66,18 @@ function _M.new_op_with_jwt_claim(jwt_claim_name, op, value, value_type) |
55 | 66 | end |
56 | 67 |
|
57 | 68 | function _M.new_op_with_liquid_templating(liquid_expression, op, value, value_type) |
| 69 | + local template = TemplateString.new(liquid_expression or "", "liquid") |
58 | 70 | local eval_left_func = function(context) |
59 | | - return TemplateString.new(liquid_expression or "" , "liquid"):render(context) |
| 71 | + return template:render(context) |
60 | 72 | end |
61 | 73 |
|
62 | 74 | return new(eval_left_func, op, value, value_type) |
63 | 75 | end |
64 | 76 |
|
65 | 77 | function _M:evaluate(context) |
66 | | - local left_operand_val = self.evaluate_left_side_func(context) |
67 | | - |
68 | | - local op = Operation.new( |
69 | | - left_operand_val, 'plain', self.op, self.value, self.value_type |
70 | | - ) |
71 | | - |
72 | | - return op:evaluate(context) |
| 78 | + local left = self.evaluate_left_side_func(context) |
| 79 | + local right = self.right_template:render(context) |
| 80 | + return self.evaluate_func(left, right) |
73 | 81 | end |
74 | 82 |
|
75 | 83 | return _M |
0 commit comments