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

Commit 2e287c7

Browse files
committed
Call user-provided functions inside internal ones
Fixes #167.
1 parent 21a0ca9 commit 2e287c7

File tree

4 files changed

+269
-188
lines changed

4 files changed

+269
-188
lines changed

graphql/accessor_general.lua

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ end
817817
--- Perform unflatten, skipping, filtering, limiting of objects. This is the
818818
--- core of the `select_internal` function.
819819
---
820+
--- @tparam table self accessor_general instance
821+
---
820822
--- @tparam table state table variable where the function holds state accross
821823
--- invokes; fields:
822824
---
@@ -844,7 +846,7 @@ end
844846
---
845847
--- Nothing returned, but after necessary count of invokes `state.objs` will
846848
--- hold list of resulting objects.
847-
local function process_tuple(state, tuple, opts)
849+
local function process_tuple(self, state, tuple, opts)
848850
local limit = opts.limit
849851
local filter = opts.filter
850852
local do_filter = opts.do_filter
@@ -865,7 +867,7 @@ local function process_tuple(state, tuple, opts)
865867
local resolveField = opts.resolveField
866868

867869
-- convert tuple -> object
868-
local obj = opts.unflatten_tuple(collection_name, tuple,
870+
local obj = opts.unflatten_tuple(self, collection_name, tuple,
869871
{ use_tomap = opts.use_tomap }, opts.default_unflatten_tuple)
870872

871873
-- skip all items before pivot (the item pointed by offset)
@@ -947,8 +949,8 @@ local function perform_primary_key_operation(self, collection_name, schema_name,
947949
end
948950
-- XXX: we can pass a tuple corresponding the object to update_tuple /
949951
-- delete_tuple to save one get operation
950-
local new_tuple = self.funcs[operation](collection_name, key, ...)
951-
local new_object = self.funcs.unflatten_tuple(collection_name,
952+
local new_tuple = self.funcs[operation](self, collection_name, key, ...)
953+
local new_object = self.funcs.unflatten_tuple(self, collection_name,
952954
new_tuple, {use_tomap = self.collection_use_tomap[collection_name]},
953955
self.default_unflatten_tuple[schema_name])
954956
table.insert(new_objects, new_object)
@@ -980,36 +982,28 @@ end
980982
---
981983
--- @treturn table list of matching objects
982984
local function select_internal(self, collection_name, from, filter, args, extra)
983-
assert(type(self) == 'table',
984-
'self must be a table, got ' .. type(self))
985-
assert(type(collection_name) == 'string',
986-
'collection_name must be a string, got ' ..
987-
type(collection_name))
985+
check(self, 'self', 'table')
986+
check(collection_name, 'collection_name', 'string')
988987
check(from, 'from', 'table')
989-
assert(type(filter) == 'table',
990-
'filter must be a table, got ' .. type(filter))
991-
assert(type(args) == 'table',
992-
'args must be a table, got ' .. type(args))
993-
assert(args.limit == nil or type(args.limit) == 'number',
994-
'args.limit must be a number of nil, got ' .. type(args.limit))
995-
-- XXX: save type at parsing and check here
996-
--assert(args.offset == nil or type(args.offset) == 'number',
997-
-- 'args.offset must be a number of nil, got ' .. type(args.offset))
998-
assert(args.pcre == nil or type(args.pcre) == 'table',
999-
'args.pcre must be nil or a table, got ' .. type(args.pcre))
988+
check(filter, 'filter', 'table')
989+
check(args, 'args', 'table')
990+
check(args.limit, 'args.limit', 'number', 'nil')
991+
-- XXX: save type of args.offset at parsing and check here
992+
-- check(args.offset, 'args.offset', ...)
993+
check(args.pcre, 'args.pcre', 'table', 'nil')
1000994

1001995
local collection = self.collections[collection_name]
1002996
assert(collection ~= nil,
1003997
('cannot find the collection "%s"'):format(
1004998
collection_name))
1005-
assert(self.funcs.is_collection_exists(collection_name),
999+
assert(self.funcs.is_collection_exists(self, collection_name),
10061000
('cannot find collection "%s"'):format(collection_name))
10071001

10081002
-- search for suitable index
10091003
local full_match, index_name, filter, index_value, pivot = get_index_name(
10101004
self, collection_name, from, filter, args) -- we redefine filter here
10111005
local index = index_name ~= nil and
1012-
self.funcs.get_index(collection_name, index_name) or nil
1006+
self.funcs.get_index(self, collection_name, index_name) or nil
10131007
if from.collection_name ~= nil then
10141008
-- allow fullscan only for a top-level object
10151009
assert(index ~= nil,
@@ -1052,11 +1046,13 @@ local function select_internal(self, collection_name, from, filter, args, extra)
10521046

10531047
if index == nil then
10541048
-- fullscan
1055-
local primary_index = self.funcs.get_primary_index(collection_name)
1049+
local primary_index = self.funcs.get_primary_index(self,
1050+
collection_name)
10561051
for _, tuple in primary_index:pairs() do
10571052
assert(pivot == nil,
10581053
'offset for top-level objects must use a primary index')
1059-
local continue = process_tuple(select_state, tuple, select_opts)
1054+
local continue = process_tuple(self, select_state, tuple,
1055+
select_opts)
10601056
if not continue then break end
10611057
end
10621058
else
@@ -1093,7 +1089,8 @@ local function select_internal(self, collection_name, from, filter, args, extra)
10931089
end
10941090

10951091
for _, tuple in index:pairs(index_value, iterator_opts) do
1096-
local continue = process_tuple(select_state, tuple, select_opts)
1092+
local continue = process_tuple(self, select_state, tuple,
1093+
select_opts)
10971094
if not continue then break end
10981095
end
10991096
end
@@ -1135,16 +1132,17 @@ local function insert_internal(self, collection_name, from, filter, args, extra)
11351132
assert(default_flatten_object ~= nil,
11361133
('cannot find default_flatten_object ' ..
11371134
'for collection "%s"'):format(collection_name))
1138-
local tuple = self.funcs.flatten_object(collection_name, object, {
1135+
local tuple = self.funcs.flatten_object(self, collection_name, object, {
11391136
service_fields_defaults =
11401137
self.service_fields_defaults[schema_name],
11411138
}, default_flatten_object)
11421139

11431140
-- insert tuple & tuple -> object (with default values set before)
1144-
local new_tuple = self.funcs.insert_tuple(collection_name, tuple)
1145-
local new_object = self.funcs.unflatten_tuple(collection_name, new_tuple, {
1146-
use_tomap = self.collection_use_tomap[collection_name]
1147-
}, self.default_unflatten_tuple[schema_name])
1141+
local new_tuple = self.funcs.insert_tuple(self, collection_name, tuple)
1142+
local use_tomap = self.collection_use_tomap[collection_name]
1143+
local new_object = self.funcs.unflatten_tuple(self, collection_name,
1144+
new_tuple, {use_tomap = use_tomap},
1145+
self.default_unflatten_tuple[schema_name])
11481146

11491147
return {new_object}
11501148
end
@@ -1177,7 +1175,7 @@ local function update_internal(self, collection_name, extra, selected)
11771175
assert(default_xflatten ~= nil,
11781176
('cannot find default_xflatten ' ..
11791177
'for collection "%s"'):format(collection_name))
1180-
local statements = self.funcs.xflatten(collection_name, xobject, {
1178+
local statements = self.funcs.xflatten(self, collection_name, xobject, {
11811179
service_fields_defaults =
11821180
self.service_fields_defaults[schema_name],
11831181
}, default_xflatten)
@@ -1279,7 +1277,7 @@ local function gen_default_object_tuple_map_funcs(models)
12791277
local default_flatten_object = {}
12801278
local default_xflatten = {}
12811279
for schema_name, model in pairs(models) do
1282-
default_unflatten_tuple[schema_name] = function(_, tuple, opts)
1280+
default_unflatten_tuple[schema_name] = function(_, _, tuple, opts)
12831281
local opts = opts or {}
12841282
check(opts, 'opts', 'table')
12851283

@@ -1288,7 +1286,7 @@ local function gen_default_object_tuple_map_funcs(models)
12881286
schema_name, tostring(obj)))
12891287
return obj
12901288
end
1291-
default_flatten_object[schema_name] = function(_, obj, opts)
1289+
default_flatten_object[schema_name] = function(_, _, obj, opts)
12921290
local opts = opts or {}
12931291
check(opts, 'opts', 'table')
12941292
local sf_defaults = opts.service_fields_defaults or {}
@@ -1299,7 +1297,7 @@ local function gen_default_object_tuple_map_funcs(models)
12991297
schema_name, tostring(tuple)))
13001298
return tuple
13011299
end
1302-
default_xflatten[schema_name] = function(_, xobject, opts)
1300+
default_xflatten[schema_name] = function(_, _, xobject, opts)
13031301
local opts = opts or {}
13041302
check(opts, 'opts', 'table')
13051303

0 commit comments

Comments
 (0)