You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2020/10/31 01:13:28 UTC
[apisix] branch master updated: chore: move try_read_attr function
into table.lua (#2257) (#2566)
This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 68cf301 chore: move try_read_attr function into table.lua (#2257) (#2566)
68cf301 is described below
commit 68cf301ed447a8d1ab112f1adb7a7ba1304da3db
Author: Peter Zhu <st...@gmail.com>
AuthorDate: Sat Oct 31 09:13:20 2020 +0800
chore: move try_read_attr function into table.lua (#2257) (#2566)
Co-authored-by: YuanSheng Wang <me...@gmail.com>
---
apisix/core/table.lua | 16 ++++++++++++++++
apisix/plugins/hmac-auth.lua | 21 +++------------------
apisix/plugins/log-rotate.lua | 21 +++------------------
apisix/plugins/skywalking.lua | 21 +++------------------
t/core/table.t | 26 ++++++++++++++++++++++++++
5 files changed, 51 insertions(+), 54 deletions(-)
diff --git a/apisix/core/table.lua b/apisix/core/table.lua
index 6b77b42..10bfaa7 100644
--- a/apisix/core/table.lua
+++ b/apisix/core/table.lua
@@ -60,6 +60,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
+
+
function _M.array_find(array, val)
for i, v in ipairs(array) do
if v == val then
diff --git a/apisix/plugins/hmac-auth.lua b/apisix/plugins/hmac-auth.lua
index 9f6e6d1..b85390e 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,8 +312,9 @@ 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
- local attr = local_conf.plugin_attr["hmac-auth"]
+ local attr = core.table.try_read_attr(local_conf, "plugin_attr",
+ "hmac-auth")
+ if attr then
access_key = attr.access_key or access_key
signature_key = attr.signature_key or signature_key
algorithm_key = attr.algorithm_key or algorithm_key
diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua
index 22d38b1..6287b55 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,26 +150,13 @@ 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
- local attr = local_conf.plugin_attr["log-rotate"]
+ local attr = core.table.try_read_attr(local_conf, "plugin_attr",
+ "log-rotate")
+ if attr then
interval = attr.interval or interval
max_kept = attr.max_kept or max_kept
end
diff --git a/apisix/plugins/skywalking.lua b/apisix/plugins/skywalking.lua
index 26aeb60..c62ba8a 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 59c8875..69bf70d 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]