Skip to content

Commit

Permalink
policy: avoid exposing PHASES
Browse files Browse the repository at this point in the history
The module is designed so its users call phases() instead.
  • Loading branch information
davidor committed Nov 14, 2017
1 parent 34d5a94 commit e1a63cb
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apicast/src/executor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local _M = { }
local mt = { __index = _M }

-- forward all policy methods to the policy chain
for _,phase in policy:phases() do
for _,phase in policy.phases() do
_M[phase] = function(self, ...)
return self.policy_chain[phase](self.policy_chain, self:context(phase), ...)
end
Expand Down
20 changes: 10 additions & 10 deletions apicast/src/policy.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local _M = {
PHASES = {
'init', 'init_worker',
'rewrite', 'access', 'balancer',
'header_filter', 'body_filter',
'post_action', 'log'
}
local _M = { }

local PHASES = {
'init', 'init_worker',
'rewrite', 'access', 'balancer',
'header_filter', 'body_filter',
'post_action', 'log'
}

local setmetatable = setmetatable
Expand All @@ -28,15 +28,15 @@ function _M.new(name, version)
return setmetatable({}, mt)
end

for _, phase in _M:phases() do
for _, phase in _M.phases() do
policy[phase] = noop
end

return policy
end

function _M:phases()
return ipairs(self.PHASES)
function _M.phases()
return ipairs(PHASES)
end

return _M
2 changes: 1 addition & 1 deletion apicast/src/policy/local_chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local function build_chain(context)
end

-- forward all policy methods to the policy chain
for _, phase in policy:phases() do
for _, phase in policy.phases() do
_M[phase] = function(_, context, ...)
local policy_chain = find_policy_chain(context)

Expand Down
2 changes: 1 addition & 1 deletion apicast/src/policy/phase_logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
local policy = require('policy')
local _M = policy.new('Phase logger')

for _, phase in policy:phases() do
for _, phase in policy.phases() do
_M[phase] = function() ngx.log(ngx.DEBUG, 'running phase: ', phase) end
end

Expand Down
2 changes: 1 addition & 1 deletion apicast/src/policy_chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ local function call_chain(phase_name)
end
end

for _,phase in policy:phases() do
for _,phase in policy.phases() do
_M[phase] = call_chain(phase)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/policy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('policy', function()
it('defines a method for each of the nginx phases; they do nothing by default', function()
local my_policy = policy.new('custom_authorizer')

for _, phase in policy:phases() do
for _, phase in policy.phases() do
-- To be precise, we should check that the function is defined, returns
-- nil, and it does not have any side-effects. Checking the latter is
-- complicated so we'll leave it for now.
Expand All @@ -51,7 +51,7 @@ describe('policy', function()
describe('.phases', function()
it('returns the nginx phases where policies can run, sorted by order of execution', function()
local res = {}
for _, phase in policy:phases() do
for _, phase in policy.phases() do
table.insert(res, phase)
end

Expand Down

0 comments on commit e1a63cb

Please sign in to comment.