Skip to content

Commit

Permalink
set.eq => set_eq
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Nov 2, 2020
1 parent 32feed6 commit 3d46391
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 43 deletions.
1 change: 0 additions & 1 deletion apisix/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ return {
config = config,
sleep = utils.sleep,
json = require("apisix.core.json"),
set = require("apisix.core.set"),
table = require("apisix.core.table"),
request = require("apisix.core.request"),
response = require("apisix.core.response"),
Expand Down
40 changes: 0 additions & 40 deletions apisix/core/set.lua

This file was deleted.

16 changes: 16 additions & 0 deletions apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,20 @@ end
_M.patch = patch


-- Compare two tables as if they are sets (only compare the key part)
function _M.set_eq(a, b)
if nkeys(a) ~= nkeys(b) then
return false
end

for k in pairs(a) do
if b[k] == nil then
return false
end
end

return true
end


return _M
4 changes: 2 additions & 2 deletions apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ local function load(plugin_names)
end

-- the same configure may be synchronized more than one
if core.set.eq(local_plugins_hash, processed) then
if core.table.set_eq(local_plugins_hash, processed) then
core.log.info("plugins not changed")
return true
end
Expand Down Expand Up @@ -152,7 +152,7 @@ local function load_stream(plugin_names)
end

-- the same configure may be synchronized more than one
if core.set.eq(stream_local_plugins_hash, processed) then
if core.table.set_eq(stream_local_plugins_hash, processed) then
core.log.info("plugins not changed")
return true
end
Expand Down
33 changes: 33 additions & 0 deletions t/core/table.t
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,36 @@ value
nil
--- no_error_log
[error]



=== TEST 4: set_eq
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local cases = {
{expect = true, a = {}, b = {}},
{expect = true, a = {a = 1}, b = {a = 1}},
{expect = true, a = {a = 1}, b = {a = 2}},
{expect = false, a = {b = 1}, b = {a = 1}},
{expect = false, a = {a = 1, b = 1}, b = {a = 1}},
{expect = false, a = {a = 1}, b = {a = 1, b = 2}},
}
for _, t in ipairs(cases) do
local actual = core.table.set_eq(t.a, t.b)
local expect = t.expect
if actual ~= expect then
ngx.say("expect ", expect, ", actual ", actual)
return
end
end
ngx.say("ok")
}
}
--- response_body
ok
--- request
GET /t
--- no_error_log
[error]

0 comments on commit 3d46391

Please sign in to comment.