Skip to content

Commit

Permalink
chore: move try_read_attr function into table.lua (apache#2257)
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz committed Oct 29, 2020
1 parent 960077f commit 21ea491
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 52 deletions.
16 changes: 16 additions & 0 deletions apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ function _M.set(tab, ...)
end


function _M.try_read_attr(tab, ...)
local count = select('#', ...)

for i = 1, count do
local attr = select(i, ...)
if type(tab) ~= "table" then
return nil
end

tab = tab[attr]
end

return tab
end


-- only work under lua51 or luajit
function _M.setmt__gc(t, mt)
local prox = newproxy(true)
Expand Down
18 changes: 1 addition & 17 deletions apisix/plugins/hmac-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
--
local ngx = ngx
local type = type
local select = select
local abs = math.abs
local ngx_time = ngx.time
local ngx_re = require("ngx.re")
Expand Down Expand Up @@ -99,21 +98,6 @@ local hmac_funcs = {
}


local function try_attr(t, ...)
local tbl = t
local count = select('#', ...)
for i = 1, count do
local attr = select(i, ...)
tbl = tbl[attr]
if type(tbl) ~= "table" then
return false
end
end

return true
end


local function array_to_map(arr)
local map = core.table.new(0, #arr)
for _, v in ipairs(arr) do
Expand Down Expand Up @@ -328,7 +312,7 @@ local function get_params(ctx)
local date_key = DATE_KEY
local signed_headers_key = SIGNED_HEADERS_KEY

if try_attr(local_conf, "plugin_attr", "hmac-auth") then
if core.table.try_read_attr(local_conf, "plugin_attr", "hmac-auth") then
local attr = local_conf.plugin_attr["hmac-auth"]
access_key = attr.access_key or access_key
signature_key = attr.signature_key or signature_key
Expand Down
18 changes: 1 addition & 17 deletions apisix/plugins/log-rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ local lfs = require("lfs")
local io = io
local os = os
local table = table
local select = select
local type = type
local string = string
local local_conf

Expand Down Expand Up @@ -152,25 +150,11 @@ local function scan_log_folder()
end


local function try_attr(t, ...)
local count = select('#', ...)
for i = 1, count do
local attr = select(i, ...)
t = t[attr]
if type(t) ~= "table" then
return false
end
end

return true
end


local function rotate()
local local_conf = core.config.local_conf()
local interval = INTERVAL
local max_kept = MAX_KEPT
if try_attr(local_conf, "plugin_attr", "log-rotate") then
if core.table.try_read_attr(local_conf, "plugin_attr", "log-rotate") then
local attr = local_conf.plugin_attr["log-rotate"]
interval = attr.interval or interval
max_kept = attr.max_kept or max_kept
Expand Down
21 changes: 3 additions & 18 deletions apisix/plugins/skywalking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ local core = require("apisix.core")
local process = require("ngx.process")
local ngx = ngx
local math = math
local select = select
local type = type
local require = require

local plugin_name = "skywalking"
Expand Down Expand Up @@ -103,28 +101,15 @@ function _M.log(conf, ctx)
end


local function try_read_attr(t, ...)
local count = select('#', ...)
for i = 1, count do
local attr = select(i, ...)
if type(t) ~= "table" then
return nil
end
t = t[attr]
end

return t
end


function _M.init()
if process.type() ~= "worker" and process.type() ~= "single" then
return
end

local local_conf = core.config.local_conf()
local local_plugin_info = try_read_attr(local_conf, "plugin_attr",
plugin_name) or {}
local local_plugin_info = core.table.try_read_attr(local_conf,
"plugin_attr",
plugin_name) or {}
local_plugin_info = core.table.clone(local_plugin_info)
local ok, err = core.schema.check(metadata_schema, local_plugin_info)
if not ok then
Expand Down
26 changes: 26 additions & 0 deletions t/core/table.t
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,29 @@ GET /t
ok
--- no_error_log
[error]



=== TEST 3: try_read_attr
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local try_read_attr = core.table.try_read_attr

local t = {level1 = {level2 = "value"}}

local v = try_read_attr(t, "level1", "level2")
ngx.say(v)

local v2 = try_read_attr(t, "level1", "level3")
ngx.say(v2)
}
}
--- request
GET /t
--- response_body
value
nil
--- no_error_log
[error]

0 comments on commit 21ea491

Please sign in to comment.