undying commented on code in PR #12187: URL: https://github.com/apache/apisix/pull/12187#discussion_r2124667723
########## apisix/core/utils.lua: ########## @@ -461,5 +461,35 @@ function _M.check_tls_bool(fields, conf, plugin_name) end end +--- +-- Checks if a string is an nginx variable. +-- An nginx variable starts with the '$' character. +-- +-- @function core.utils.is_nginx_variable +-- @tparam string str The string to check +-- @treturn boolean true if the string starts with '$', false otherwise +-- @usage +-- local utils = require("apisix.core.utils") +-- +-- -- Usage examples: +-- local is_var = utils.is_nginx_variable("$host") -- true +-- local is_var = utils.is_nginx_variable("host") -- false +-- local is_var = utils.is_nginx_variable("${host}") -- true +-- local is_var = utils.is_nginx_variable("\\$host") -- false +-- +-- -- Usage in APISIX context: +-- if utils.is_nginx_variable(up_conf.service_name) then +-- -- Handle as nginx variable +-- else +-- -- Handle as regular service name +-- end +function _M.is_nginx_variable(str) + if not str or type(str) ~= "string" then + return false + end + + -- Check if the string starts with '$' and it's not escaped + return str:sub(1, 1) == "$" and (str:sub(1, 2) ~= "\\$") +end Review Comment: My main idea was to avoid an unnecessary call to `resolve_var` if the string is not actually a variable. Well, as if it were superfluous, since `resolve_var` has such a check and an early exit, so optimization is unnecessary. I changed it to use `resolve_var` directly, thanks for the help. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org