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

Commit 960cc49

Browse files
committed
Check the corner case in an avro-schema generation
This follows up the PR #58 (the work in the scope of the issue #7).
1 parent f555d70 commit 960cc49

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

graphql/query_to_avro.lua

+25-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
---
55
--- * The best way to use this module is to just call `avro_schema` method on
66
--- compiled query object.
7+
8+
local json = require('json')
79
local path = "graphql.core"
810
local introspection = require(path .. '.introspection')
911
local query_util = require(path .. '.query_util')
@@ -41,20 +43,38 @@ end
4143
--- In the current tarantool/avro-schema implementation we simply add '*' to
4244
--- the end of a type name.
4345
---
44-
--- If the type is already nullable the function leaves it as is.
46+
--- If the type is already nullable the function leaves it as if
47+
--- `opts.raise_on_nullable` is false or omitted. If `opts.raise_on_nullable`
48+
--- is true the function will raise an error.
4549
---
4650
--- @tparam table avro avro schema node to be converted to nullable one
4751
---
52+
--- @tparam[opt] table opts the following options:
53+
---
54+
--- * `raise_on_nullable` (boolean) raise an error on nullable type
55+
---
4856
--- @result `result` (string or table) nullable avro type
49-
local function make_avro_type_nullable(avro)
57+
local function make_avro_type_nullable(avro, opts)
5058
assert(avro ~= nil, "avro must not be nil")
59+
local opts = opts or {}
60+
assert(type(opts) == 'table',
61+
'opts must be nil or a table, got ' .. type(opts))
62+
local raise_on_nullable = opts.raise_on_nullable or false
63+
assert(type(raise_on_nullable) == 'boolean',
64+
'opts.raise_on_nullable must be nil or a boolean, got ' ..
65+
type(raise_on_nullable))
5166

5267
local value_type = type(avro)
5368

5469
if value_type == "string" then
55-
return avro:endswith("*") and avro or (avro .. '*')
70+
local is_nullable = avro:endswith("*")
71+
if raise_on_nullable and is_nullable then
72+
error('expected non-null type, got the nullable one: ' ..
73+
json.encode(avro))
74+
end
75+
return is_nullable and avro or (avro .. '*')
5676
elseif value_type == "table" then
57-
return make_avro_type_nullable(avro.type)
77+
return make_avro_type_nullable(avro.type, opts)
5878
end
5979

6080
error("avro should be a string or a table, got " .. value_type)
@@ -103,7 +123,7 @@ local function gql_type_to_avro(fieldType, subSelections, context)
103123
end
104124

105125
if not isNonNull then
106-
result = make_avro_type_nullable(result)
126+
result = make_avro_type_nullable(result, {raise_on_nullable = true})
107127
end
108128
return result
109129
end

0 commit comments

Comments
 (0)