Skip to content

Commit 05ad19a

Browse files
ligurioTotktonada
authored andcommitted
Add a function that get a sharding function stored in _G
Part of #76
1 parent abbdd57 commit 05ad19a

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

ddl/check.lua

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -713,21 +713,12 @@ local function is_callable(object)
713713
end
714714

715715
local function check_sharding_func_name(sharding_func_name)
716-
-- split sharding func name in dot notation by dot
717-
-- foo.bar.baz -> chunks: foo bar baz
718-
-- foo -> chunks: foo
719-
local chunks = string.split(sharding_func_name, '.')
720-
721-
local sharding_func = _G
722-
-- check is the each chunk an identifier
723-
for _, chunk in pairs(chunks) do
724-
if not utils.check_name_isident(chunk) or sharding_func == nil then
725-
return false
726-
end
727-
sharding_func = rawget(sharding_func, chunk)
716+
local sharding_func = utils.get_G_function(sharding_func_name)
717+
if sharding_func ~= nil then
718+
return is_callable(sharding_func)
728719
end
729720

730-
return is_callable(sharding_func)
721+
return false
731722
end
732723

733724
local function check_sharding_func(space)

ddl/utils.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@ local function check_name_isident(name)
186186
return true
187187
end
188188

189+
-- split sharding func name in dot notation by dot
190+
-- foo.bar.baz -> chunks: foo bar baz
191+
-- foo -> chunks: foo
192+
local function get_G_function(func_name)
193+
local chunks = string.split(func_name, '.')
194+
local sharding_func = _G
195+
-- check is the each chunk an identifier
196+
for _, chunk in pairs(chunks) do
197+
if not check_name_isident(chunk) or sharding_func == nil then
198+
return nil
199+
end
200+
sharding_func = rawget(sharding_func, chunk)
201+
end
202+
203+
return sharding_func
204+
end
205+
189206
return {
190207
deepcmp = deepcmp,
191208
is_array = is_array,
@@ -194,5 +211,5 @@ return {
194211
lj_char_isident = lj_char_isident,
195212
lj_char_isdigit = lj_char_isdigit,
196213
LUA_KEYWORDS = LUA_KEYWORDS,
197-
check_name_isident = check_name_isident,
214+
get_G_function = get_G_function,
198215
}

0 commit comments

Comments
 (0)