diff --git a/apisix/core/table.lua b/apisix/core/table.lua index 4ad92caa104d2..942a7f3b438ab 100644 --- a/apisix/core/table.lua +++ b/apisix/core/table.lua @@ -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) diff --git a/apisix/plugins/hmac-auth.lua b/apisix/plugins/hmac-auth.lua index 9f6e6d15be904..5ad91f887a7ca 100644 --- a/apisix/plugins/hmac-auth.lua +++ b/apisix/plugins/hmac-auth.lua @@ -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") @@ -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 @@ -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 diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index 22d38b1ae7d88..883a72b0bc30f 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -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 @@ -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 diff --git a/apisix/plugins/skywalking.lua b/apisix/plugins/skywalking.lua index 26aeb60bff96a..c62ba8ad5fe4a 100644 --- a/apisix/plugins/skywalking.lua +++ b/apisix/plugins/skywalking.lua @@ -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" @@ -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 diff --git a/t/core/table.t b/t/core/table.t index 59c887538e7d1..69bf70d3d54c8 100644 --- a/t/core/table.t +++ b/t/core/table.t @@ -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]