Skip to content

Commit

Permalink
refactor(pdk): use resty.core.utils.str_replace_char instead of `gs…
Browse files Browse the repository at this point in the history
…ub` (#11823)

It is a sister PR of #11721, optimize the code of pdk.
  • Loading branch information
chronolaw authored Oct 25, 2023
1 parent 9b13810 commit d286856
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 8 additions & 4 deletions kong/pdk/service/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
local cjson = require "cjson.safe".new()
local multipart = require "multipart"
local phase_checker = require "kong.pdk.private.phases"
local string_tools = require "kong.tools.string"


local ngx = ngx
local sub = string.sub
local fmt = string.format
local gsub = string.gsub
local find = string.find
local type = type
local error = error
Expand All @@ -26,6 +26,10 @@ local check_phase = phase_checker.check
cjson.decode_array_with_array_mt(true)


local replace_dashes = string_tools.replace_dashes
local replace_dashes_lower = string_tools.replace_dashes_lower


local PHASES = phase_checker.phases


Expand All @@ -45,7 +49,7 @@ do
local resp_headers_mt = {
__index = function(t, name)
if type(name) == "string" then
local var = fmt("upstream_http_%s", gsub(lower(name), "-", "_"))
local var = fmt("upstream_http_%s", replace_dashes_lower(name))
if not ngx.var[var] then
return nil
end
Expand Down Expand Up @@ -94,7 +98,7 @@ do
return response_headers[name]
end

name = gsub(name, "-", "_")
name = replace_dashes(name)

if response_headers[name] then
return response_headers[name]
Expand All @@ -106,7 +110,7 @@ do
return nil
end

n = gsub(lower(n), "-", "_")
n = replace_dashes_lower(n)
if n == name then
return v
end
Expand Down
7 changes: 4 additions & 3 deletions kong/pdk/vault.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ local isempty = require "table.isempty"
local buffer = require "string.buffer"
local clone = require "table.clone"
local utils = require "kong.tools.utils"
local string_tools = require "kong.tools.string"
local cjson = require("cjson.safe").new()


local yield = utils.yield
local get_updated_now_ms = utils.get_updated_now_ms
local replace_dashes = string_tools.replace_dashes


local ngx = ngx
Expand All @@ -30,7 +32,6 @@ local max = math.max
local fmt = string.format
local sub = string.sub
local byte = string.byte
local gsub = string.gsub
local type = type
local sort = table.sort
local pcall = pcall
Expand Down Expand Up @@ -539,7 +540,7 @@ local function new(self)
base_config = {}
if self and self.configuration then
local configuration = self.configuration
local env_name = gsub(name, "-", "_")
local env_name = replace_dashes(name)
local _, err, schema = get_vault_strategy_and_schema(name)
if not schema then
return nil, err
Expand All @@ -553,7 +554,7 @@ local function new(self)
-- then you would configure it with KONG_VAULT_MY_VAULT_<setting>
-- or in kong.conf, where it would be called
-- "vault_my_vault_<setting>".
local n = lower(fmt("vault_%s_%s", env_name, gsub(k, "-", "_")))
local n = lower(fmt("vault_%s_%s", env_name, replace_dashes(k)))
local v = configuration[n]
v = arguments.infer_value(v, f)
-- TODO: should we be more visible with validation errors?
Expand Down

1 comment on commit d286856

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:d28685606ff80953acf3577309e437bde56dc3ce
Artifacts available https://github.com/Kong/kong/actions/runs/6635172448

Please sign in to comment.