Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit b3dc9a9

Browse files
authored
Merge pull request #103 from tarantool/sb/type_assertion_simplified
Add check() function to reduce type assertion boilerplate
2 parents 0695610 + a0661d9 commit b3dc9a9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

graphql/utils.lua

+20
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,24 @@ function utils.do_have_keys(table, keys)
170170
return true
171171
end
172172

173+
174+
--- Check if passed obj has one of passed types.
175+
--- @tparam table obj to check
176+
--- @tparam {type_1, type_2} ... possible types
177+
function utils.check(obj, obj_name, type_1, type_2, type_3)
178+
if type(obj) == type_1 or type(obj) == type_2 or type(obj) == type_3 then
179+
return
180+
end
181+
182+
if type_3 ~= nil then
183+
error(('%s must be a %s or a % or a %, got %s'):format(obj_name, type_1,
184+
type_2, type_3, type(obj)))
185+
elseif type_2 ~= nil then
186+
error(('%s must be a %s or a %, got %s'):format(obj_name, type_1,
187+
type_2, type(obj)))
188+
else
189+
error(('%s must be a %s, got %s'):format(obj_name, type_1, type(obj)))
190+
end
191+
end
192+
173193
return utils

0 commit comments

Comments
 (0)