[patches] [AVRO 1/3] Add primitive type & union normalization

AKhatskevich avkhatskevich at tarantool.org
Tue Feb 20 11:26:31 MSK 2018


From: "AKhatskevich avkhatskevich at tarantool.org" <avkhatskevich at gmail.com>

Add normalization for primitive types:
1. {type="int"} -> type="int"

Normalization of this patterns is neccessary for further fingerprinting.

Part of #30
---
 avro_schema/frontend.lua | 8 ++++++--
 test/api_tests.lua       | 7 ++++++-
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/avro_schema/frontend.lua b/avro_schema/frontend.lua
index 35c3d91..646b448 100644
--- a/avro_schema/frontend.lua
+++ b/avro_schema/frontend.lua
@@ -212,6 +212,7 @@ copy_schema = function(schema, ns, scope, open_rec)
             -- this check is necessary for unnamed complex types (union, array map)
             copy_schema_error('Infinite loop detected in the data')
         end
+        -- array, describing union [type1, type2...]
         if #schema > 0 then
             local tagmap = {}
             scope[schema] = 1
@@ -247,8 +248,11 @@ copy_schema = function(schema, ns, scope, open_rec)
             nullable, xtype = extract_nullable(xtype)
 
             if primitive_type[xtype] then
-                res = {type = xtype, nullable = nullable}
-                return res
+                -- primitive type normalization
+                if nullable == nil then
+                    return xtype
+                end
+                return {type = xtype, nullable = nullable}
             elseif xtype == 'record' then
                 res = { type = 'record' }
                 res.nullable = nullable
diff --git a/test/api_tests.lua b/test/api_tests.lua
index a764117..b1628bb 100644
--- a/test/api_tests.lua
+++ b/test/api_tests.lua
@@ -5,7 +5,7 @@ local msgpack = require('msgpack')
 
 local test = tap.test('api-tests')
 
-test:plan(42)
+test:plan(50)
 
 test:is_deeply({schema.create()}, {false, 'Unknown Avro type: nil'},
                'error unknown type')
@@ -203,5 +203,10 @@ local s65400 = string.rep('@', 65400)
 test:is_deeply({strm.unflatten({s260})}, {true, s260}, 'large string 260')
 test:is_deeply({strm.unflatten({s65400})}, {true, s65400}, 'large string 65400')
 
+for _, type in ipairs({"int", "string", "null", "boolean", "long", "float", "double", "bytes"}) do
+    res = {schema.create({type=type})}
+    test:is_deeply(schema.export(res[2]), type, 'schema normalization '..type)
+end
+
 test:check()
 os.exit(test.planned == test.total and test.failed == 0 and 0 or -1)
-- 
2.14.1




More information about the Tarantool-patches mailing list