Skip to content

Commit

Permalink
refactor(*): use kong.tools.string whenever possible
Browse files Browse the repository at this point in the history
### Summary

`kong.tools.string` has a faster implementation of e.g. `strip`. This makes our code use
the `kong.tools.string` whenever possible and removes `pl.stringx` usage in those cases.

Signed-off-by: Aapo Talvensaari <[email protected]>
  • Loading branch information
bungle committed Jun 7, 2024
1 parent 4450845 commit b5bdfbb
Show file tree
Hide file tree
Showing 31 changed files with 106 additions and 91 deletions.
3 changes: 1 addition & 2 deletions kong/clustering/data_plane.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ local config_helper = require("kong.clustering.config_helper")
local clustering_utils = require("kong.clustering.utils")
local declarative = require("kong.db.declarative")
local constants = require("kong.constants")
local pl_stringx = require("pl.stringx")
local inspect = require("inspect")

local assert = assert
Expand Down Expand Up @@ -37,7 +36,7 @@ local PING_WAIT = PING_INTERVAL * 1.5
local _log_prefix = "[clustering] "
local DECLARATIVE_EMPTY_CONFIG_HASH = constants.DECLARATIVE_EMPTY_CONFIG_HASH

local endswith = pl_stringx.endswith
local endswith = require("pl.stringx").endswith

local function is_timeout(err)
return err and sub(err, -7) == "timeout"
Expand Down
3 changes: 1 addition & 2 deletions kong/clustering/rpc/utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local _M = {}
local pl_stringx = require("pl.stringx")
local cjson = require("cjson")
local snappy = require("resty.snappy")

Expand All @@ -8,7 +7,7 @@ local string_sub = string.sub
local assert = assert
local cjson_encode = cjson.encode
local cjson_decode = cjson.decode
local rfind = pl_stringx.rfind
local rfind = require("pl.stringx").rfind
local snappy_compress = snappy.compress
local snappy_uncompress = snappy.uncompress

Expand Down
5 changes: 3 additions & 2 deletions kong/cmd/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ local log = require "kong.cmd.utils.log"
local kill = require "kong.cmd.utils.kill"
local pl_path = require "pl.path"
local pl_tablex = require "pl.tablex"
local pl_stringx = require "pl.stringx"
local conf_loader = require "kong.conf_loader"

local ljust = require("pl.stringx").ljust

local function execute(args)
log.disable()
-- retrieve default prefix or use given one
Expand All @@ -27,7 +28,7 @@ local function execute(args)
local count = 0
for k, v in pairs(pids) do
local running = kill.is_running(v)
local msg = pl_stringx.ljust(k, 12, ".") .. (running and "running" or "not running")
local msg = ljust(k, 12, ".") .. (running and "running" or "not running")
if running then
count = count + 1
end
Expand Down
4 changes: 2 additions & 2 deletions kong/cmd/utils/inject_confs.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local conf_loader = require "kong.conf_loader"
local pl_path = require "pl.path"
local pl_stringx = require "pl.stringx"
local prefix_handler = require "kong.cmd.utils.prefix_handler"
local log = require "kong.cmd.utils.log"
local strip = require("kong.tools.string").strip
local fmt = string.format

local compile_nginx_main_inject_conf = prefix_handler.compile_nginx_main_inject_conf
Expand Down Expand Up @@ -42,7 +42,7 @@ local function convert_directive_path_to_absolute(prefix, nginx_conf, paths)
return nil, err

elseif m then
local path = pl_stringx.strip(m[2])
local path = strip(m[2])

if path:sub(1, 1) ~= '/' then
local absolute_path = prefix .. "/" .. path
Expand Down
6 changes: 4 additions & 2 deletions kong/cmd/utils/nginx_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ local meta = require "kong.meta"
local pl_path = require "pl.path"
local version = require "version"
local pl_utils = require "pl.utils"
local pl_stringx = require "pl.stringx"
local process_secrets = require "kong.cmd.utils.process_secrets"


local strip = require("kong.tools.string").strip


local fmt = string.format
local ipairs = ipairs
local unpack = unpack
Expand Down Expand Up @@ -115,7 +117,7 @@ function _M.find_nginx_bin(kong_conf)
log.debug("finding executable absolute path from $PATH...")
local ok, code, stdout, stderr = pl_utils.executeex("command -v nginx")
if ok and code == 0 then
path_to_check = pl_stringx.strip(stdout)
path_to_check = strip(stdout)

else
log.error("could not find executable absolute path: %s", stderr)
Expand Down
9 changes: 6 additions & 3 deletions kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ local x509 = require "resty.openssl.x509"
local x509_extension = require "resty.openssl.x509.extension"
local x509_name = require "resty.openssl.x509.name"
local pl_template = require "pl.template"
local pl_stringx = require "pl.stringx"
local pl_tablex = require "pl.tablex"
local pl_utils = require "pl.utils"
local pl_file = require "pl.file"
Expand All @@ -27,6 +26,10 @@ local bit = require "bit"
local nginx_signals = require "kong.cmd.utils.nginx_signals"


local strip = require("kong.tools.string").strip
local split = require("kong.tools.string").split


local getmetatable = getmetatable
local makepath = pl_dir.makepath
local tonumber = tonumber
Expand Down Expand Up @@ -229,7 +232,7 @@ local function get_ulimit()
if not ok then
return nil, stderr
end
local sanitized_limit = pl_stringx.strip(stdout)
local sanitized_limit = strip(stdout)
if sanitized_limit:lower():match("unlimited") then
return 65536
else
Expand Down Expand Up @@ -725,7 +728,7 @@ local function prepare_prefix(kong_config, nginx_custom_template_path, skip_writ
end

local template_env = {}
nginx_conf_flags = nginx_conf_flags and pl_stringx.split(nginx_conf_flags, ",") or {}
nginx_conf_flags = nginx_conf_flags and split(nginx_conf_flags, ",") or {}
for _, flag in ipairs(nginx_conf_flags) do
template_env[flag] = true
end
Expand Down
7 changes: 4 additions & 3 deletions kong/conf_loader/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local require = require
local kong_default_conf = require "kong.templates.kong_defaults"
local process_secrets = require "kong.cmd.utils.process_secrets"
local pl_stringio = require "pl.stringio"
local pl_stringx = require "pl.stringx"
local socket_url = require "socket.url"
local conf_constants = require "kong.conf_loader.constants"
local listeners = require "kong.conf_loader.listeners"
Expand All @@ -15,19 +14,21 @@ local pl_config = require "pl.config"
local pl_file = require "pl.file"
local pl_path = require "pl.path"
local tablex = require "pl.tablex"
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy
local log = require "kong.cmd.utils.log"
local env = require "kong.cmd.utils.env"
local ffi = require "ffi"


local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy
local strip = require("kong.tools.string").strip


local fmt = string.format
local sub = string.sub
local type = type
local sort = table.sort
local find = string.find
local gsub = string.gsub
local strip = pl_stringx.strip
local lower = string.lower
local match = string.match
local pairs = pairs
Expand Down
6 changes: 3 additions & 3 deletions kong/conf_loader/listeners.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local pl_stringx = require "pl.stringx"
local tools_ip = require "kong.tools.ip"
local strip = require("kong.tools.string").strip


local type = type
Expand Down Expand Up @@ -73,7 +73,7 @@ local function parse_option_flags(value, flags)
end
end

return pl_stringx.strip(value), result, pl_stringx.strip(sanitized)
return strip(value), result, strip(sanitized)
end


Expand All @@ -98,7 +98,7 @@ local function parse_listeners(values, flags)
return nil, usage
end

if pl_stringx.strip(values[1]) == "off" then
if strip(values[1]) == "off" then
return list
end

Expand Down
12 changes: 5 additions & 7 deletions kong/conf_loader/parse.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local require = require


local pl_stringx = require "pl.stringx"
local pl_path = require "pl.path"
local socket_url = require "socket.url"
local tablex = require "pl.tablex"
Expand All @@ -10,15 +9,15 @@ local openssl_pkey = require "resty.openssl.pkey"
local log = require "kong.cmd.utils.log"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local conf_constants = require "kong.conf_loader.constants"


local tools_system = require("kong.tools.system") -- for unit-testing
local tools_ip = require("kong.tools.ip")
local tools_system = require "kong.tools.system" -- for unit-testing
local tools_ip = require "kong.tools.ip"


local normalize_ip = tools_ip.normalize_ip
local is_valid_ip_or_cidr = tools_ip.is_valid_ip_or_cidr
local try_decode_base64 = require("kong.tools.string").try_decode_base64
local strip = require("kong.tools.string").strip
local split = require("kong.tools.string").split
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy
local is_valid_uuid = require("kong.tools.uuid").is_valid_uuid

Expand All @@ -40,7 +39,6 @@ local insert = table.insert
local concat = table.concat
local getenv = os.getenv
local re_match = ngx.re.match
local strip = pl_stringx.strip
local exists = pl_path.exists
local isdir = pl_path.isdir

Expand Down Expand Up @@ -93,7 +91,7 @@ local function parse_value(value, typ)
-- must check type because pl will already convert comma
-- separated strings to tables (but not when the arr has
-- only one element)
value = setmetatable(pl_stringx.split(value, ","), nil) -- remove List mt
value = setmetatable(split(value, ","), nil) -- remove List mt

for i = 1, #value do
value[i] = strip(value[i])
Expand Down
10 changes: 5 additions & 5 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local logger = require "kong.cmd.utils.log"
local pgmoon = require "pgmoon"
local arrays = require "pgmoon.arrays"
local stringx = require "pl.stringx"
local semaphore = require "ngx.semaphore"
local kong_global = require "kong.global"
local constants = require "kong.constants"
local db_utils = require "kong.db.utils"
local kong_global = require "kong.global"
local constants = require "kong.constants"
local db_utils = require "kong.db.utils"


local setmetatable = setmetatable
Expand All @@ -31,6 +30,7 @@ local sub = string.sub
local utils_toposort = db_utils.topological_sort
local insert = table.insert
local table_merge = require("kong.tools.table").table_merge
local strip = require("kong.tools.string").strip


local WARN = ngx.WARN
Expand Down Expand Up @@ -867,7 +867,7 @@ function _mt:run_up_migration(name, up_sql)
error("no connection")
end

local sql = stringx.strip(up_sql)
local sql = strip(up_sql)
if sub(sql, -1) ~= ";" then
sql = sql .. ";"
end
Expand Down
3 changes: 1 addition & 2 deletions kong/plugins/aws-lambda/request-util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ local ngx_encode_base64 = ngx.encode_base64
local ngx_decode_base64 = ngx.decode_base64
local cjson = require "cjson.safe"

local pl_stringx = require("pl.stringx")
local date = require("date")
local get_request_id = require("kong.tracing.request_id").get

local EMPTY = {}

local isempty = require "table.isempty"
local split = pl_stringx.split
local split = require("kong.tools.string").split
local ngx_req_get_headers = ngx.req.get_headers
local ngx_req_get_uri_args = ngx.req.get_uri_args
local ngx_get_http_version = ngx.req.http_version
Expand Down
7 changes: 2 additions & 5 deletions kong/plugins/oauth2/secret.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
local kong_string = require "kong.tools.string"


local type = type
local fmt = string.format
local find = string.find
Expand All @@ -11,8 +8,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 strip = require("kong.tools.string").strip
local split = require("kong.tools.string").split
local get_rand_bytes = require("kong.tools.rand").get_rand_bytes


Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/request-size-limiting/handler.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Copyright (C) Kong Inc.

local strip = require("pl.stringx").strip
local strip = require("kong.tools.string").strip
local kong_meta = require "kong.meta"
local tonumber = tonumber

Expand Down
7 changes: 2 additions & 5 deletions kong/plugins/response-ratelimiting/header_filter.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
local kong_string = require "kong.tools.string"


local kong = kong
local next = next
local type = type
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 strip = require("kong.tools.string").strip
local split = require("kong.tools.string").split


local RATELIMIT_LIMIT = "X-RateLimit-Limit"
Expand Down
8 changes: 4 additions & 4 deletions kong/plugins/response-transformer/header_transformer.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local isempty = require "table.isempty"
local mime_type = require "kong.tools.mime_type"
local ngx_re = require "ngx.re"
local pl_stringx = require "pl.stringx"


local kong = kong
local type = type
Expand All @@ -10,8 +10,8 @@ local noop = function() end
local ipairs = ipairs
local parse_mime_type = mime_type.parse_mime_type
local mime_type_includes = mime_type.includes
local split = ngx_re.split
local strip = pl_stringx.strip
local re_split = ngx_re.split
local strip = require("kong.tools.string").strip


local _M = {}
Expand Down Expand Up @@ -44,7 +44,7 @@ local function is_json_body(content_type)
if not content_type then
return false
end
local content_types = split(content_type, ",")
local content_types = re_split(content_type, ",")
local expected_media_type = { type = "application", subtype = "json" }
for _, content_type in ipairs(content_types) do
local t, subtype = parse_mime_type(strip(content_type))
Expand Down
7 changes: 3 additions & 4 deletions kong/tools/http.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local pl_path = require "pl.path"
local pl_file = require "pl.file"
local tools_str = require "kong.tools.string"


local type = type
Expand All @@ -13,9 +12,9 @@ local sort = table.sort
local concat = table.concat
local fmt = string.format
local re_match = ngx.re.match
local join = tools_str.join
local split = tools_str.split
local strip = tools_str.strip
local join = require("kong.tools.string").join
local split = require("kong.tools.string").split
local strip = require("kong.tools.string").strip


local _M = {}
Expand Down
4 changes: 2 additions & 2 deletions kong/tools/ip.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local ipmatcher = require "resty.ipmatcher"
local pl_stringx = require "pl.stringx"



local type = type
Expand All @@ -11,7 +11,7 @@ local sub = string.sub
local fmt = string.format
local lower = string.lower
local find = string.find
local split = pl_stringx.split
local split = require("kong.tools.string").split


local _M = {}
Expand Down
Loading

0 comments on commit b5bdfbb

Please sign in to comment.