Exemplo n.º 1
0
        public void AssertValues()
        {
            var jToken = JToken.Parse(@"{""num"":""1"",""happy"":""face""}");

            Assert.DoesNotThrow(
                () => JsonUtil.AssertValue(jToken, "num", "1")
                );

            Assert.DoesNotThrow(
                () => JsonUtil.AssertValue(jToken, "happy", "face")
                );

            Assert.DoesNotThrow(
                () => JsonUtil.AssertValues(jToken, "happy", "face", "X", "Y")
                );


            Assert.Throws(
                typeof(ArgumentException),
                () => JsonUtil.AssertValue(jToken, "num", "X")
                );

            Assert.Throws(
                typeof(ArgumentException),
                () => JsonUtil.AssertValue(jToken, "q2345", "X")
                );

            Assert.Throws(
                typeof(ArgumentException),
                () => JsonUtil.AssertValues(jToken, "num", "face", "X", "Y")
                );
        }
Exemplo n.º 2
0
        private static AvroSchema ParseFixedSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var keys = new HashSet <string>()
            {
                "type", "size", "name"
            };
            var optionalKeys = new HashSet <string>()
            {
                "namespace"
            };

            JsonUtil.AssertKeys(jToken, keys, optionalKeys, out var tags);

            JsonUtil.AssertValue(jToken, "type", "fixed");
            var size = JsonUtil.GetValue <int>(jToken, "size");
            var name = JsonUtil.GetValue <string>(jToken, "name");

            JsonUtil.TryGetValue <string>(jToken, "namespace", out var ns);
            var fixedSchema = new FixedSchema(name, ns, size);

            if (string.IsNullOrEmpty(fixedSchema.Namespace))
            {
                fixedSchema.Namespace = enclosingNamespace.Peek();
            }
            fixedSchema.AddTags(tags);
            namedTypes.Add(fixedSchema.FullName, fixedSchema);
            return(fixedSchema);
        }
Exemplo n.º 3
0
        private static AvroSchema ParseDurationSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var keys = new HashSet <string>()
            {
                "logicalType", "type"
            };

            JsonUtil.AssertKeys(jToken, keys, null, out var tags);

            JsonUtil.AssertValue(jToken, "logicalType", "duration");
            var type           = JsonUtil.GetValue <JToken>(jToken, "type");
            var underlyingType = ParseSchema(type, namedTypes, enclosingNamespace);
            var durationSchema = new DurationSchema(underlyingType);

            durationSchema.AddTags(tags);
            return(durationSchema);
        }
Exemplo n.º 4
0
        private static AvroSchema ParseMapSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var keys = new HashSet <string>()
            {
                "type", "values"
            };

            JsonUtil.AssertKeys(jToken, keys, null, out var tags);

            JsonUtil.AssertValue(jToken, "type", "map");
            var values       = JsonUtil.GetValue <JToken>(jToken, "values");
            var valuesSchema = ParseSchema(values, namedTypes, enclosingNamespace);
            var mapSchema    = new MapSchema(valuesSchema);

            mapSchema.AddTags(tags);
            return(mapSchema);
        }
Exemplo n.º 5
0
        private static AvroSchema ParseArraySchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var keys = new HashSet <string>()
            {
                "type", "items"
            };

            JsonUtil.AssertKeys(jToken, keys, null, out var tags);

            JsonUtil.AssertValue(jToken, "type", "array");
            var items       = JsonUtil.GetValue <JToken>(jToken, "items");
            var itemsSchema = ParseSchema(items, namedTypes, enclosingNamespace);
            var arraySchema = new ArraySchema(itemsSchema);

            arraySchema.AddTags(tags);
            return(arraySchema);
        }
Exemplo n.º 6
0
        private static AvroSchema ParseEnumType(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var keys = new HashSet <string>()
            {
                "type", "name", "symbols"
            };
            var optionalKeys = new HashSet <string>()
            {
                "namespace", "aliases", "doc"
            };

            JsonUtil.AssertKeys(jToken, keys, optionalKeys, out var tags);

            JsonUtil.AssertValue(jToken, "type", "enum");
            var name    = JsonUtil.GetValue <string>(jToken, "name");
            var symbols = JsonUtil.GetValue <JArray>(jToken, "symbols");

            var enumSchema = new EnumSchema(name, null, symbols.Values <string>());

            enumSchema.AddTags(tags);
            if (JsonUtil.TryGetValue <string>(jToken, "namespace", out var ns))
            {
                enumSchema.Namespace = ns;
            }

            if (string.IsNullOrEmpty(enumSchema.Namespace))
            {
                enumSchema.Namespace = enclosingNamespace.Peek();
            }

            if (JsonUtil.TryGetValue <JArray>(jToken, "aliases", out var aliases))
            {
                enumSchema.Aliases = aliases.Values <string>().ToArray();
            }

            if (JsonUtil.TryGetValue <string>(jToken, "doc", out var doc))
            {
                enumSchema.Doc = doc;
            }

            namedTypes.Add(enumSchema.FullName, enumSchema);

            return(enumSchema);
        }