Skip to content

Commit

Permalink
feat(tools): add readonly tools.table.EMPTY for common usage (#13492)
Browse files Browse the repository at this point in the history
Inspired by #13491
This PR did not replace all so-called `EMPTY` tables, only which are obviously read-only.

https://konghq.atlassian.net/browse/KAG-5140
  • Loading branch information
chronolaw authored Aug 13, 2024
1 parent c2860b1 commit 2ffd3b1
Show file tree
Hide file tree
Showing 21 changed files with 24 additions and 31 deletions.
2 changes: 1 addition & 1 deletion kong/clustering/compat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local COMPATIBILITY_CHECKERS = require("kong.clustering.compat.checkers")
local CLUSTERING_SYNC_STATUS = constants.CLUSTERING_SYNC_STATUS
local KONG_VERSION = meta.version

local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY


local _M = {}
Expand Down
2 changes: 1 addition & 1 deletion kong/conf_loader/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ local UPSTREAM_HEADER_KEY_TO_NAME = {
}


local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY


-- NOTE! Prefixes should always follow `nginx_[a-z]+_`.
Expand Down
2 changes: 1 addition & 1 deletion kong/hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local unpack = table.unpack
local insert = table.insert
local type = type
local select = select
local EMPTY = require("pl.tablex").readonly({})
local EMPTY = require("kong.tools.table").EMPTY

--[[
The preferred maximum number of return values from a hook,
Expand Down
2 changes: 1 addition & 1 deletion kong/llm/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local ai_shared = require("kong.llm.drivers.shared")
local re_match = ngx.re.match
local cjson = require("cjson.safe")
local fmt = string.format
local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY


-- The module table
Expand Down
2 changes: 1 addition & 1 deletion kong/llm/proxy/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end
--


local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY

local _M = {}

Expand Down
5 changes: 1 addition & 4 deletions kong/plugins/acl/groups.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
local tablex = require "pl.tablex"


local EMPTY = tablex.readonly {}
local EMPTY = require("kong.tools.table").EMPTY


local kong = kong
Expand Down
3 changes: 1 addition & 2 deletions kong/plugins/acl/handler.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local constants = require "kong.constants"
local tablex = require "pl.tablex"
local groups = require "kong.plugins.acl.groups"
local kong_meta = require "kong.meta"

Expand All @@ -10,7 +9,7 @@ local error = error
local kong = kong


local EMPTY = tablex.readonly {}
local EMPTY = require("kong.tools.table").EMPTY
local DENY = "DENY"
local ALLOW = "ALLOW"

Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/oauth2/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local ngx_encode_base64 = ngx.encode_base64
local _M = {}


local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY
local SLASH = string_byte("/")
local RESPONSE_TYPE = "response_type"
local STATE = "state"
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/proxy-cache/cache_key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local sha256_hex = require("kong.tools.sha256").sha256_hex
local _M = {}


local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY


local function keys(t)
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/proxy-cache/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local calculate_resource_ttl = require("kong.tools.http").calculate_resource_ttl

local STRATEGY_PATH = "kong.plugins.proxy-cache.strategies"
local CACHE_VERSION = 1
local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY


-- http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
Expand Down
4 changes: 2 additions & 2 deletions kong/plugins/rate-limiting/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local pdk_rl_store_response_header = pdk_private_rl.store_response_header
local pdk_rl_apply_response_headers = pdk_private_rl.apply_response_headers


local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY
local EXPIRATION = require "kong.plugins.rate-limiting.expiration"


Expand Down Expand Up @@ -212,4 +212,4 @@ function RateLimitingHandler:access(conf)
end


return RateLimitingHandler
return RateLimitingHandler
2 changes: 1 addition & 1 deletion kong/plugins/rate-limiting/policies/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local SYNC_RATE_REALTIME = -1

local EMPTY_UUID = "00000000-0000-0000-0000-000000000000"

local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY

local cur_usage = {
--[[
Expand Down
3 changes: 1 addition & 2 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local multipart = require "multipart"
local cjson = require("cjson.safe").new()
local pl_template = require "pl.template"
local pl_tablex = require "pl.tablex"
local sandbox = require "kong.tools.sandbox"
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy

Expand Down Expand Up @@ -35,7 +34,7 @@ local CONTENT_LENGTH = "content-length"
local CONTENT_TYPE = "content-type"
local HOST = "host"
local JSON, MULTI, ENCODED = "json", "multi_part", "form_encoded"
local EMPTY = pl_tablex.readonly({})
local EMPTY = require("kong.tools.table").EMPTY


local compile_opts = {
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/response-ratelimiting/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local pdk_rl_store_response_header = pdk_private_rl.store_response_header
local pdk_rl_apply_response_headers = pdk_private_rl.apply_response_headers


local EMPTY = {}
local EMPTY = require("kong.tools.table").EMPTY
local HTTP_TOO_MANY_REQUESTS = 429
local RATELIMIT_REMAINING = "X-RateLimit-Remaining"

Expand Down
3 changes: 1 addition & 2 deletions kong/resty/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ local COLON = string_byte(":")
local DEFAULT_TIMEOUT = 2000 -- 2000 is openresty default


local EMPTY = setmetatable({},
{__newindex = function() error("The 'EMPTY' table is read-only") end})
local EMPTY = require("kong.tools.table").EMPTY

-- resolver options
local config
Expand Down
2 changes: 1 addition & 1 deletion kong/router/traditional.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ local MATCH_SUBRULES = {
}


local EMPTY_T = {}
local EMPTY_T = require("kong.tools.table").EMPTY


local match_route
Expand Down
3 changes: 1 addition & 2 deletions kong/runloop/balancer/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local pl_tablex = require "pl.tablex"
local hostname_type = require("kong.tools.ip").hostname_type
local hooks = require "kong.hooks"
local recreate_request = require("ngx.balancer").recreate_request
Expand Down Expand Up @@ -34,7 +33,7 @@ local is_http_module = ngx.config.subsystem == "http"
local CRIT = ngx.CRIT
local ERR = ngx.ERR
local WARN = ngx.WARN
local EMPTY_T = pl_tablex.readonly {}
local EMPTY_T = require("kong.tools.table").EMPTY


local set_authority
Expand Down
3 changes: 1 addition & 2 deletions kong/runloop/balancer/least_connections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ local binaryHeap = require "binaryheap"
local ngx_log = ngx.log
local ngx_DEBUG = ngx.DEBUG

local EMPTY = setmetatable({},
{__newindex = function() error("The 'EMPTY' table is read-only") end})
local EMPTY = require("kong.tools.table").EMPTY


local lc = {}
Expand Down
3 changes: 1 addition & 2 deletions kong/runloop/balancer/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ local ERR = ngx.ERR
local WARN = ngx.WARN

local SRV_0_WEIGHT = 1 -- SRV record with weight 0 should be hit minimally, hence we replace by 1
local EMPTY = setmetatable({},
{__newindex = function() error("The 'EMPTY' table is read-only") end})
local EMPTY = require("kong.tools.table").EMPTY
local GLOBAL_QUERY_OPTS = { workspace = null, show_ws_id = true }

-- global binary heap for all balancers to share as a single update timer for
Expand Down
3 changes: 1 addition & 2 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 pl_tblx = require "pl.tablex"


local type = type
Expand All @@ -24,7 +23,7 @@ local lower = string.lower
local max = math.max
local tab_new = require("table.new")

local EMPTY = pl_tblx.readonly({})
local EMPTY = require("kong.tools.table").EMPTY

local _M = {}

Expand Down
3 changes: 3 additions & 0 deletions kong/tools/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ local getmetatable = getmetatable
local _M = {}


_M.EMPTY = require("pl.tablex").readonly({})


--- packs a set of arguments in a table.
-- Explicitly sets field `n` to the number of arguments, so it is `nil` safe
_M.pack = function(...) return {n = select("#", ...), ...} end
Expand Down

1 comment on commit 2ffd3b1

@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:2ffd3b1a85f334bd35b0493a3f8684f407f03adf
Artifacts available https://github.com/Kong/kong/actions/runs/10365268524

Please sign in to comment.