Skip to content

Commit

Permalink
[loader] try to load code from deprecated paths
Browse files Browse the repository at this point in the history
* custom loader can try to load files from new paths
  even when the caller code refers to the old ones
* print deprecation warnings with suggestions how to fix them
  • Loading branch information
mikz committed Nov 21, 2017
1 parent 4df243b commit b1d677a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gateway/src/apicast/executor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
-- when calling the policy chain methods. This 'context' contains information
-- shared among policies.

require('apicast.loader') -- to load code from deprecated paths

local policy_chain = require('apicast.policy_chain')
local policy = require('apicast.policy')
local linked_list = require('apicast.linked_list')
Expand Down
58 changes: 58 additions & 0 deletions gateway/src/apicast/loader.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--- APIcast source loader
-- Loading this module will add a new source code loader to package.searchers.
-- The searcher is going to print deprecation warnings when apicast source is loaded
-- through old or non prefixed paths.
-- We can rename files and set up an alias here so we don't break customer's code and
-- print a deprecation warning.

local loadfile = loadfile

local map = {
['apicast'] = 'apicast.policy.apicast'
}

local function loader(name, path)
local file, err = package.searchpath(name, path)

if file then
file, err = loadfile(file)
end

return file, err
end

local function prefix_loader(name, path)
local prefixed = 'apicast.' .. name
local found, err = loader(prefixed, path)

if found then
ngx.log(ngx.STDERR, 'DEPRECATION: when loading apicast code use correct prefix: require("', prefixed, '")')
end

return found or err
end

local function rename_loader(name, path)
local new = map[name]
local found, err = loader(new, path)

if found then
ngx.log(ngx.WARN, 'DEPRECATION: file renamed, change: require("', name, '")' ,' to: require("', new, '")')
end

return found or err
end

local function apicast_namespace(name)
local path = package.path

if not package.searchpath(name, path) then
if map[name] then
return rename_loader(name, path)
else
return prefix_loader(name, path)
end
end
end

table.insert(package.searchers, apicast_namespace)

0 comments on commit b1d677a

Please sign in to comment.