Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support both standard PATCH and sub path PATCH for admin api #1930

Merged
merged 8 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ local core = require("apisix.core")
local schema_plugin = require("apisix.admin.plugins").check_schema
local type = type
local tostring = tostring
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -138,7 +137,7 @@ function _M.patch(id, conf, sub_path)
local node_value = res_old.body.node.value

if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(node_value, sub_path, conf);
local code, err, node_val = core.table.patch(node_value, sub_path, conf);
node_value = node_val
if code then
return code, err
Expand Down
3 changes: 1 addition & 2 deletions apisix/admin/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local upstreams = require("apisix.admin.upstreams")
local tostring = tostring
local type = type
local loadstring = loadstring
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -232,7 +231,7 @@ function _M.patch(id, conf, sub_path, args)
local node_value = res_old.body.node.value

if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(node_value, sub_path, conf);
local code, err, node_val = core.table.patch(node_value, sub_path, conf);
node_value = node_val
if code then
return code, err
Expand Down
4 changes: 2 additions & 2 deletions apisix/admin/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local upstreams = require("apisix.admin.upstreams")
local tostring = tostring
local ipairs = ipairs
local type = type
local table_util = require("apisix.utils.table-util")

nic-chen marked this conversation as resolved.
Show resolved Hide resolved


local _M = {
Expand Down Expand Up @@ -209,7 +209,7 @@ function _M.patch(id, conf, sub_path)
local node_value = res_old.body.node.value

if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(node_value, sub_path, conf);
local code, err, node_val = core.table.patch(node_value, sub_path, conf);
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
node_value = node_val
if code then
return code, err
Expand Down
3 changes: 1 addition & 2 deletions apisix/admin/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local get_services = require("apisix.http.service").services
local tostring = tostring
local ipairs = ipairs
local type = type
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -243,7 +242,7 @@ function _M.patch(id, conf, sub_path)
local new_value = res_old.body.node.value

if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(new_value, sub_path, conf);
local code, err, node_val = core.table.patch(new_value, sub_path, conf);
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
new_value = node_val
if code then
return code, err
Expand Down
40 changes: 40 additions & 0 deletions apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local new_tab = require("table.new")
local nkeys = require("table.nkeys")
local pairs = pairs
local type = type
local ngx_re = require("ngx.re")


local _M = {
Expand Down Expand Up @@ -105,4 +106,43 @@ local function merge(origin, extend)
end
_M.merge = merge


local function split_uri(uri)
return ngx_re.split(uri, "/")
end


local function patch(node_value, sub_path, conf)
local sub_value = node_value
local sub_paths = split_uri(sub_path)
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
for i = 1, #sub_paths - 1 do
local sub_name = sub_paths[i]
if sub_value[sub_name] == nil then
sub_value[sub_name] = {}
end

sub_value = sub_value[sub_name]

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /"
.. _M.concat(sub_paths, 1, i)
end
end

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /" .. sub_path
end

local sub_name = sub_paths[#sub_paths]
if sub_name and sub_name ~= "" then
sub_value[sub_name] = conf
else
node_value = conf
end

return nil, nil, node_value
end
_M.patch = patch


return _M
58 changes: 0 additions & 58 deletions apisix/utils/table-util.lua

This file was deleted.