Skip to content

Commit

Permalink
refactor(tools/string): updating references of tools.string (#13104)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Jun 1, 2024
1 parent 5ba7462 commit d52e2fc
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 91 deletions.
2 changes: 1 addition & 1 deletion kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,7 @@
#
# ```
# local template = require "resty.template"
# local split = require "kong.tools.utils".split
# local split = require "kong.tools.string".split
# ```
#
# To run the plugin, add the modules to the
Expand Down
4 changes: 2 additions & 2 deletions kong/api/routes/health.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local utils = require "kong.tools.utils"
local declarative = require "kong.db.declarative"
local bytes_to_str = require("kong.tools.string").bytes_to_str

local tonumber = tonumber
local kong = kong
Expand Down Expand Up @@ -29,7 +29,7 @@ return {

-- validate unit and scale arguments

local pok, perr = pcall(utils.bytes_to_str, 0, unit, scale)
local pok, perr = pcall(bytes_to_str, 0, unit, scale)
if not pok then
return kong.response.exit(400, { message = perr })
end
Expand Down
4 changes: 1 addition & 3 deletions kong/clustering/compat/version.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local utils = require("kong.tools.utils")

local type = type
local tonumber = tonumber
local split = utils.split
local split = require("kong.tools.string").split

local MAJOR_MINOR_PATTERN = "^(%d+)%.(%d+)%.%d+"

Expand Down
4 changes: 2 additions & 2 deletions kong/db/schema/entities/targets.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local typedefs = require "kong.db.schema.typedefs"
local utils = require "kong.tools.utils"
local normalize_ip = require("kong.tools.ip").normalize_ip
local validate_utf8 = require("kong.tools.string").validate_utf8


local function validate_target(target)
local p = normalize_ip(target)
if not p then
local ok = utils.validate_utf8(target)
local ok = validate_utf8(target)
if not ok then
return nil, "Invalid target; not a valid hostname or ip address"
end
Expand Down
4 changes: 2 additions & 2 deletions kong/db/schema/entities/upstreams.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local Schema = require "kong.db.schema"
local typedefs = require "kong.db.schema.typedefs"
local utils = require "kong.tools.utils"
local normalize_ip = require("kong.tools.ip").normalize_ip
local validate_utf8 = require("kong.tools.string").validate_utf8
local null = ngx.null


local function get_name_for_error(name)
local ok = utils.validate_utf8(name)
local ok = validate_utf8(name)
if not ok then
return "Invalid name"
end
Expand Down
3 changes: 2 additions & 1 deletion kong/db/schema/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local Schema = require "kong.db.schema"
local socket_url = require "socket.url"
local constants = require "kong.constants"
local tools_ip = require "kong.tools.ip"
local validate_utf8 = require("kong.tools.string").validate_utf8


local DAO_MAX_TTL = constants.DATABASE.DAO_MAX_TTL
Expand Down Expand Up @@ -104,7 +105,7 @@ end


local function validate_utf8_string(str)
local ok, index = utils.validate_utf8(str)
local ok, index = validate_utf8(str)

if not ok then
return nil, "invalid utf-8 character sequence detected at position " .. tostring(index)
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/ai-proxy/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local cjson = require("cjson.safe")
local kong_utils = require("kong.tools.gzip")
local kong_meta = require("kong.meta")
local buffer = require "string.buffer"
local strip = require("kong.tools.utils").strip
local strip = require("kong.tools.string").strip
--


Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/bot-detection/handler.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local rules = require "kong.plugins.bot-detection.rules"
local strip = require("kong.tools.utils").strip
local strip = require("kong.tools.string").strip
local lrucache = require "resty.lrucache"
local kong_meta = require "kong.meta"

Expand Down
28 changes: 15 additions & 13 deletions kong/plugins/oauth2/secret.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require "kong.tools.utils"
local kong_string = require "kong.tools.string"


local type = type
Expand All @@ -11,6 +11,8 @@ local assert = assert
local tonumber = tonumber
local encode_base64 = ngx.encode_base64
local decode_base64 = ngx.decode_base64
local strip = kong_string.strip
local split = kong_string.split
local get_rand_bytes = require("kong.tools.rand").get_rand_bytes


Expand All @@ -34,20 +36,20 @@ local ENABLED_ALGORITHMS = {


local function infer(value)
value = utils.strip(value)
value = strip(value)
return tonumber(value, 10) or value
end


local function parse_phc(phc)
local parts = utils.split(phc, "$")
local parts = split(phc, "$")
local count = #parts
if count < 2 or count > 5 then
return nil, "invalid phc string format"
end

local id = parts[2]
local id_parts = utils.split(id, "-")
local id_parts = split(id, "-")
local id_count = #id_parts

local prefix
Expand All @@ -63,15 +65,15 @@ local function parse_phc(phc)
local params = {}
local prms = parts[3]
if prms then
local prm_parts = utils.split(prms, ",")
local prm_parts = split(prms, ",")
for i = 1, #prm_parts do
local param = prm_parts[i]
local kv = utils.split(param, "=")
local kv = split(param, "=")
local kv_count = #kv
if kv_count == 1 then
params[#params + 1] = infer(kv[1])
elseif kv_count == 2 then
local k = utils.strip(kv[1])
local k = strip(kv[1])
params[k] = infer(kv[2])
else
return nil, "invalid phc string format for parameter"
Expand All @@ -96,9 +98,9 @@ local function parse_phc(phc)
end

return {
id = utils.strip(id),
prefix = utils.strip(prefix),
digest = utils.strip(digest),
id = strip(id),
prefix = strip(prefix),
digest = strip(digest),
params = params,
salt = salt,
hash = hash,
Expand Down Expand Up @@ -133,7 +135,7 @@ if ENABLED_ALGORITHMS.ARGON2 then
}
do
local hash = argon2.hash_encoded("", get_rand_bytes(ARGON2_SALT_LEN), ARGON2_OPTIONS)
local parts = utils.split(hash, "$")
local parts = split(hash, "$")
remove(parts)
remove(parts)
ARGON2_PREFIX = concat(parts, "$")
Expand Down Expand Up @@ -171,7 +173,7 @@ if ENABLED_ALGORITHMS.BCRYPT then

do
local hash = bcrypt.digest("", BCRYPT_ROUNDS)
local parts = utils.split(hash, "$")
local parts = split(hash, "$")
remove(parts)
BCRYPT_PREFIX = concat(parts, "$")
end
Expand Down Expand Up @@ -253,7 +255,7 @@ if ENABLED_ALGORITHMS.PBKDF2 then

do
local hash = derive("")
local parts = utils.split(hash, "$")
local parts = split(hash, "$")
remove(parts)
remove(parts)
PBKDF2_PREFIX = concat(parts, "$")
Expand Down
4 changes: 2 additions & 2 deletions kong/plugins/proxy-cache/handler.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local require = require
local cache_key = require "kong.plugins.proxy-cache.cache_key"
local utils = require "kong.tools.utils"
local kong_meta = require "kong.meta"
local mime_type = require "kong.tools.mime_type"
local nkeys = require "table.nkeys"
local split = require("kong.tools.string").split


local ngx = ngx
Expand Down Expand Up @@ -260,7 +260,7 @@ function ProxyCacheHandler:init_worker()
kong.cluster_events:subscribe("proxy-cache:purge", function(data)
kong.log.err("handling purge of '", data, "'")

local plugin_id, cache_key = unpack(utils.split(data, ":"))
local plugin_id, cache_key = unpack(split(data, ":"))
local plugin, err = kong.db.plugins:select({ id = plugin_id })
if err then
kong.log.err("error in retrieving plugins: ", err)
Expand Down
12 changes: 7 additions & 5 deletions kong/plugins/response-ratelimiting/header_filter.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require "kong.tools.utils"
local kong_string = require "kong.tools.string"


local kong = kong
Expand All @@ -8,6 +8,8 @@ local pairs = pairs
local ipairs = ipairs
local tonumber = tonumber
local math_max = math.max
local strip = kong_string.strip
local split = kong_string.split


local RATELIMIT_LIMIT = "X-RateLimit-Limit"
Expand All @@ -22,15 +24,15 @@ local function parse_header(header_value, limits)
if type(header_value) == "table" then
parts = header_value
else
parts = utils.split(header_value, ",")
parts = split(header_value, ",")
end

for _, v in ipairs(parts) do
local increment_parts = utils.split(v, "=")
local increment_parts = split(v, "=")
if #increment_parts == 2 then
local limit_name = utils.strip(increment_parts[1])
local limit_name = strip(increment_parts[1])
if limits[limit_name] then -- Only if the limit exists
increments[utils.strip(increment_parts[1])] = tonumber(utils.strip(increment_parts[2]))
increments[strip(increment_parts[1])] = tonumber(strip(increment_parts[2]))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/zipkin/request_tags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
-- Will add two tags to the request span in Zipkin


local split = require "kong.tools.utils".split
local split = require "kong.tools.string".split

local match = string.match

Expand Down
7 changes: 3 additions & 4 deletions kong/runloop/events.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local utils = require "kong.tools.utils"
local constants = require "kong.constants"
local certificate = require "kong.runloop.certificate"
local balancer = require "kong.runloop.balancer"
Expand All @@ -11,7 +10,7 @@ local unpack = unpack
local ipairs = ipairs
local tonumber = tonumber
local fmt = string.format
local utils_split = utils.split
local split = require("kong.tools.string").split


local ngx = ngx
Expand Down Expand Up @@ -114,7 +113,7 @@ end

-- cluster event: "balancer:targets"
local function cluster_balancer_targets_handler(data)
local operation, key = unpack(utils_split(data, ":"))
local operation, key = unpack(split(data, ":"))

local entity = "all"
if key ~= "all" then
Expand Down Expand Up @@ -152,7 +151,7 @@ end


local function cluster_balancer_upstreams_handler(data)
local operation, ws_id, id, name = unpack(utils_split(data, ":"))
local operation, ws_id, id, name = unpack(split(data, ":"))
local entity = {
id = id,
name = name,
Expand Down
4 changes: 2 additions & 2 deletions kong/tools/emmy_debugger.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local pl_path = require "pl.path"
local utils = require "kong.tools.utils"
local split = require("kong.tools.string").split

local env_config = {
debugger = os.getenv("KONG_EMMY_DEBUGGER"),
Expand Down Expand Up @@ -64,7 +64,7 @@ local function init(config_)
local multi_worker = env_config.multi_worker or env_config.multi_worker

env_prefix = config.env_prefix or "KONG"
source_path = utils.split(config.source_path or env_config.source_path or "", ":")
source_path = split(config.source_path or env_config.source_path or "", ":")

if not debugger then
return
Expand Down
2 changes: 1 addition & 1 deletion kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ do
"kong.tools.uuid",
"kong.tools.rand",
"kong.tools.time",
-- ]] keep it here for compatibility
"kong.tools.string",
-- ]] keep it here for compatibility
"kong.tools.ip",
"kong.tools.http",
}
Expand Down
4 changes: 2 additions & 2 deletions kong/tracing/propagation/extractors/aws.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local _EXTRACTOR = require "kong.tracing.propagation.extractors._base"
local propagation_utils = require "kong.tracing.propagation.utils"

local split = require "kong.tools.utils".split
local strip = require "kong.tools.utils".strip
local split = require "kong.tools.string".split
local strip = require "kong.tools.string".strip

local from_hex = propagation_utils.from_hex
local match = string.match
Expand Down
Loading

1 comment on commit d52e2fc

@github-actions
Copy link
Contributor

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:d52e2fcabc11ee6d5093ee49bf884a44e85ed63e
Artifacts available https://github.com/Kong/kong/actions/runs/9327496709

Please sign in to comment.