-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #814 from 3scale/json-conditions
JSON conditions for conditional policy
- v3.15.1
- v3.15.0
- v3.14.0
- v3.13.2
- v3.13.1
- v3.13.0
- v3.12.2
- v3.12.0
- v3.11.0
- v3.10.0
- v3.10.0-beta2
- v3.10.0-beta1
- v3.10.0-alpha2
- v3.10.0-alpha1
- v3.9.1
- v3.9.0
- v3.9.0-beta1
- v3.9.0-alpha1
- v3.8.0
- v3.8.0-cr1
- v3.8.0-beta1
- v3.8.0-alpha2
- v3.8.0-alpha1
- v3.7.0
- v3.7.0-cr2
- v3.7.0-cr1
- v3.7.0-beta2
- v3.7.0-beta1
- v3.6.0
- v3.6.0-rc2
- v3.6.0-rc1
- v3.6.0-beta1
- v3.5.1
- v3.5.0
- v3.5.0-rc1
- v3.5.0-beta1
- v3.4.0
- v3.4.0-rc2
- v3.4.0-rc1
- v3.4.0-beta1
- v3.3.0
- v3.3.0-cr2
- v3.3.0-cr1
- v3.3.0-beta2
- v3.3.0-beta1
- 3scale-test
- 3scale-2.15.1-GA
- 3scale-2.14.1-GA
- 3scale-2.13.2-GA
- 3scale-2.13.1-GA
- 3scale-2.13.0-GA
- 3scale-2.12.2-GA
- 3scale-2.12.1-GA
- 3scale-2.12.0-GA
- 3scale-2.11.2-GA
- 3scale-2.11.1-GA
- 3scale-2.11.0-GA
- 3scale-2.10.0-GA
- 3scale-2.10.0-ER2
- 3scale-2.10.0-ER1
- 3scale-2.10.0-CR1
- 3scale-2.9.1-GA
- 3scale-2.9.0-GA
- 3scale-2.9.0-CR1
- 3scale-2.8.1-GA
- 3scale-2.8.0-GA
- 3scale-2.8.0-DR1
- 3scale-2.7.0
- 3scale-2.7.0-ER2
- 3scale-2.7.0-ER1
- 3scale-2.7.0-CR2
- 3scale-2.7.0-CR1
- 3scale-2.6.0
- 3scale-2.6.0-ER2
- 3scale-2.6.0-ER1
- 3scale-2.6.0-CR1
- 3scale-2.5.0
- 3scale-2.4.0-ER1
- 3scale-2.4.0-DR2
- 3scale-2.4.0-DR1
- 3scale-2.3.0-ER1
- 2.4.0-DR1
Showing
11 changed files
with
446 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
local setmetatable = setmetatable | ||
local ipairs = ipairs | ||
local assert = assert | ||
|
||
local _M = {} | ||
|
||
local mt = { __index = _M } | ||
|
||
local default_combine_op = 'and' | ||
|
||
local function all_true(operations, context) | ||
for _, operation in ipairs(operations) do | ||
if not operation:evaluate(context) then | ||
return false | ||
end | ||
end | ||
|
||
return true | ||
end | ||
|
||
local function at_least_one_true(operations, context) | ||
for _, operation in ipairs(operations) do | ||
if operation:evaluate(context) then | ||
return true | ||
end | ||
end | ||
|
||
return false | ||
end | ||
|
||
local evaluate_func = { | ||
['and'] = all_true, | ||
['or'] = at_least_one_true, | ||
['true'] = function() return true end | ||
} | ||
|
||
function _M.new(operations, combine_op) | ||
local self = setmetatable({}, mt) | ||
|
||
if not operations then | ||
-- If there's nothing to evaluate, return true. | ||
self.evaluate_func = evaluate_func['true'] | ||
else | ||
self.evaluate_func = evaluate_func[combine_op or default_combine_op] | ||
end | ||
|
||
assert(self.evaluate_func, 'Unsupported combine op') | ||
|
||
self.operations = operations | ||
|
||
return self | ||
end | ||
|
||
function _M:evaluate(context) | ||
return self.evaluate_func(self.operations, context) | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
local setmetatable = setmetatable | ||
local assert = assert | ||
local tostring = tostring | ||
|
||
local TemplateString = require 'apicast.template_string' | ||
|
||
local default_value_type = 'plain' | ||
|
||
local _M = {} | ||
|
||
local mt = { __index = _M } | ||
|
||
local function create_template(value, type) | ||
return TemplateString.new(value, type or default_value_type) | ||
end | ||
|
||
local evaluate_func = { | ||
['=='] = function(left, right) return tostring(left) == tostring(right) end, | ||
['!='] = function(left, right) return tostring(left) ~= tostring(right) end | ||
} | ||
|
||
--- Initialize an operation | ||
-- @tparam string left Left operand | ||
-- @tparam[opt] string left_type How to evaluate the left operand ('plain' or | ||
-- 'liquid'). Default: 'plain'. | ||
-- @tparam string op Operation | ||
-- @tparam[opt] string right Right operand | ||
-- @tparam string right_type How to evaluate the right operand ('plain' or | ||
-- 'liquid'). Default: 'plain'. | ||
function _M.new(left, left_type, op, right, right_type) | ||
local self = setmetatable({}, mt) | ||
|
||
self.evaluate_func = evaluate_func[op] | ||
assert(self.evaluate_func, 'Unsupported operation') | ||
|
||
self.templated_left = create_template(left, left_type) | ||
self.templated_right = create_template(right, right_type) | ||
|
||
return self | ||
end | ||
|
||
function _M:evaluate(context) | ||
local left_value = self.templated_left:render(context) | ||
local right_value = self.templated_right:render(context) | ||
|
||
return self.evaluate_func(left_value, right_value) | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
local Condition = require('apicast.policy.conditional.condition') | ||
local Operation = require('apicast.policy.conditional.operation') | ||
|
||
describe('Engine', function() | ||
describe('.new', function() | ||
it('raises error with an unsupported operation', function() | ||
local res, err = pcall(Condition.new, {}, '<>') | ||
|
||
assert.is_falsy(res) | ||
assert.is_truthy(err) | ||
end) | ||
end) | ||
|
||
describe('.evaluate', function() | ||
it('combines operations with "and"', function() | ||
local true_condition = Condition.new( | ||
{ | ||
Operation.new('1', 'plain', '==', '1', 'plain'), | ||
Operation.new('2', 'plain', '==', '2', 'plain'), | ||
Operation.new('3', 'plain', '==', '3', 'plain') | ||
}, | ||
'and' | ||
) | ||
|
||
assert.is_true(true_condition:evaluate()) | ||
|
||
local false_condition = Condition.new( | ||
{ | ||
Operation.new('1', 'plain', '==', '1', 'plain'), | ||
Operation.new('2', 'plain', '==', '20', 'plain'), | ||
Operation.new('3', 'plain', '==', '3', 'plain') | ||
}, | ||
'and' | ||
) | ||
|
||
assert.is_false(false_condition:evaluate()) | ||
end) | ||
|
||
it('combines operations with "or"', function() | ||
local true_condition = Condition.new( | ||
{ | ||
Operation.new('1', 'plain', '==', '10', 'plain'), | ||
Operation.new('2', 'plain', '==', '20', 'plain'), | ||
Operation.new('3', 'plain', '==', '3', 'plain') | ||
}, | ||
'or' | ||
) | ||
|
||
assert.is_true(true_condition:evaluate()) | ||
|
||
local false_condition = Condition.new( | ||
{ | ||
Operation.new('1', 'plain', '==', '10', 'plain'), | ||
Operation.new('2', 'plain', '==', '20', 'plain'), | ||
Operation.new('3', 'plain', '==', '30', 'plain') | ||
}, | ||
'or' | ||
) | ||
|
||
assert.is_false(false_condition:evaluate()) | ||
end) | ||
|
||
it('combines operations with "and" by default', function() | ||
local true_condition = Condition.new( | ||
{ | ||
Operation.new('1', 'plain', '==', '1', 'plain'), | ||
Operation.new('2', 'plain', '==', '2', 'plain'), | ||
Operation.new('3', 'plain', '==', '3', 'plain') | ||
} | ||
) | ||
|
||
assert.is_true(true_condition:evaluate()) | ||
|
||
local false_condition = Condition.new( | ||
{ | ||
Operation.new('1', 'plain', '==', '1', 'plain'), | ||
Operation.new('2', 'plain', '==', '20', 'plain'), | ||
Operation.new('3', 'plain', '==', '3', 'plain') | ||
} | ||
) | ||
|
||
assert.is_false(false_condition:evaluate()) | ||
end) | ||
|
||
it('returns true when there are no operations', function() | ||
assert.is_true(Condition.new():evaluate()) | ||
end) | ||
end) | ||
end) |
Oops, something went wrong.