|
4 | 4 | ---
|
5 | 5 | --- * The best way to use this module is to just call `avro_schema` method on
|
6 | 6 | --- compiled query object.
|
| 7 | + |
| 8 | +local json = require('json') |
7 | 9 | local path = "graphql.core"
|
8 | 10 | local introspection = require(path .. '.introspection')
|
9 | 11 | local query_util = require(path .. '.query_util')
|
|
41 | 43 | --- In the current tarantool/avro-schema implementation we simply add '*' to
|
42 | 44 | --- the end of a type name.
|
43 | 45 | ---
|
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. |
45 | 49 | ---
|
46 | 50 | --- @tparam table avro avro schema node to be converted to nullable one
|
47 | 51 | ---
|
| 52 | +--- @tparam[opt] table opts the following options: |
| 53 | +--- |
| 54 | +--- * `raise_on_nullable` (boolean) raise an error on nullable type |
| 55 | +--- |
48 | 56 | --- @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) |
50 | 58 | 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)) |
51 | 66 |
|
52 | 67 | local value_type = type(avro)
|
53 | 68 |
|
54 | 69 | 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 .. '*') |
56 | 76 | elseif value_type == "table" then
|
57 |
| - return make_avro_type_nullable(avro.type) |
| 77 | + return make_avro_type_nullable(avro.type, opts) |
58 | 78 | end
|
59 | 79 |
|
60 | 80 | 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)
|
103 | 123 | end
|
104 | 124 |
|
105 | 125 | if not isNonNull then
|
106 |
| - result = make_avro_type_nullable(result) |
| 126 | + result = make_avro_type_nullable(result, {raise_on_nullable = true}) |
107 | 127 | end
|
108 | 128 | return result
|
109 | 129 | end
|
|
0 commit comments