From d28685606ff80953acf3577309e437bde56dc3ce Mon Sep 17 00:00:00 2001 From: Chrono Date: Wed, 25 Oct 2023 10:55:47 +0800 Subject: [PATCH] refactor(pdk): use `resty.core.utils.str_replace_char` instead of `gsub` (#11823) It is a sister PR of #11721, optimize the code of pdk. --- kong/pdk/service/response.lua | 12 ++++++++---- kong/pdk/vault.lua | 7 ++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/kong/pdk/service/response.lua b/kong/pdk/service/response.lua index 7a0598a368ca..7a47419f96fb 100644 --- a/kong/pdk/service/response.lua +++ b/kong/pdk/service/response.lua @@ -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 @@ -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 @@ -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 @@ -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] @@ -106,7 +110,7 @@ do return nil end - n = gsub(lower(n), "-", "_") + n = replace_dashes_lower(n) if n == name then return v end diff --git a/kong/pdk/vault.lua b/kong/pdk/vault.lua index 08f3a0d03a1c..99e975f6e3f0 100644 --- a/kong/pdk/vault.lua +++ b/kong/pdk/vault.lua @@ -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 @@ -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 @@ -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 @@ -553,7 +554,7 @@ local function new(self) -- then you would configure it with KONG_VAULT_MY_VAULT_ -- or in kong.conf, where it would be called -- "vault_my_vault_". - 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?