Class for array type schemas
Inheritance: Avro.UnnamedSchema
Exemplo n.º 1
0
        /// <summary>
        /// Function to compare equality of two array schemas
        /// </summary>
        /// <param name="obj">other array schema</param>
        /// <returns>true two schemas are equal, false otherwise</returns>
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }

            if (obj != null && obj is ArraySchema)
            {
                ArraySchema that = obj as ArraySchema;
                if (ItemSchema.Equals(that.ItemSchema))
                {
                    return(areEqual(that.Props, this.Props));
                }
            }
            return(false);
        }
Exemplo n.º 2
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.º 3
0
        private static void WriteArray(TextWriter writer, ArraySchema schema, WriterMode mode, string parentNamespace, ISet <string> namedSchemas, bool stripNs)
        {
            switch (mode)
            {
            case WriterMode.Canonical:
                writer.Write(@"{""type"":""array"",""items"":");
                Write(writer, schema.Items, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@"}");
                break;

            case WriterMode.Full:
                writer.Write(@"{ ""type"": ""array"", ""items"": ");
                Write(writer, schema.Items, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@" }");
                break;

            default:
                writer.Write(@"{ ""type"": ""array"", ""items"": ");
                Write(writer, schema.Items, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@" }");
                break;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Static class to return new instance of schema object
        /// </summary>
        /// <param name="jtok">JSON object</param>
        /// <param name="names">list of named schemas already read</param>
        /// <param name="encspace">enclosing namespace of the schema</param>
        /// <returns>new Schema object</returns>
        internal static Schema ParseJson(JToken jtok, SchemaNames names, string encspace)
        {
            if (null == jtok)
            {
                throw new ArgumentNullException("j", "j cannot be null.");
            }

            if (jtok.Type == JTokenType.String) // primitive schema with no 'type' property or primitive or named type of a record field
            {
                string value = (string)jtok;

                PrimitiveSchema ps = PrimitiveSchema.NewInstance(value);
                if (null != ps)
                {
                    return(ps);
                }

                NamedSchema schema = null;
                if (names.TryGetValue(value, null, encspace, out schema))
                {
                    return(schema);
                }

                throw new SchemaParseException($"Undefined name: {value} at '{jtok.Path}'");
            }

            if (jtok is JArray) // union schema with no 'type' property or union type for a record field
            {
                return(UnionSchema.NewInstance(jtok as JArray, null, names, encspace));
            }

            if (jtok is JObject) // JSON object with open/close parenthesis, it must have a 'type' property
            {
                JObject jo = jtok as JObject;

                JToken jtype = jo["type"];
                if (null == jtype)
                {
                    throw new SchemaParseException($"Property type is required at '{jtok.Path}'");
                }

                var props = Schema.GetProperties(jtok);

                if (jtype.Type == JTokenType.String)
                {
                    string type = (string)jtype;

                    if (type.Equals("array", StringComparison.Ordinal))
                    {
                        return(ArraySchema.NewInstance(jtok, props, names, encspace));
                    }
                    if (type.Equals("map", StringComparison.Ordinal))
                    {
                        return(MapSchema.NewInstance(jtok, props, names, encspace));
                    }
                    if (null != jo["logicalType"]) // logical type based on a primitive
                    {
                        return(LogicalSchema.NewInstance(jtok, props, names, encspace));
                    }

                    Schema schema = PrimitiveSchema.NewInstance((string)type, props);
                    if (null != schema)
                    {
                        return(schema);
                    }

                    return(NamedSchema.NewInstance(jo, props, names, encspace));
                }
                else if (jtype.Type == JTokenType.Array)
                {
                    return(UnionSchema.NewInstance(jtype as JArray, props, names, encspace));
                }
                else if (jtype.Type == JTokenType.Object && null != jo["logicalType"]) // logical type based on a complex type
                {
                    return(LogicalSchema.NewInstance(jtok, props, names, encspace));
                }
            }
            throw new AvroTypeException($"Invalid JSON for schema: {jtok} at '{jtok.Path}'");
        }
Exemplo n.º 5
0
        private static StringBuilder Build(IDictionary <string, string> env, Schema s, StringBuilder o)
        {
            bool firstTime = true;

            Schema.Type st = s.Tag;
            switch (st)
            {
            case Schema.Type.Union:
                UnionSchema us = s as UnionSchema;
                o.Append('[');
                foreach (Schema b in us.Schemas)
                {
                    if (!firstTime)
                    {
                        o.Append(',');
                    }
                    else
                    {
                        firstTime = false;
                    }
                    Build(env, b, o);
                }
                return(o.Append(']'));

            case Schema.Type.Array:
            case Schema.Type.Map:
                o.Append("{\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append('\"');
                if (st == Schema.Type.Array)
                {
                    ArraySchema arraySchema = s as ArraySchema;
                    Build(env, arraySchema.ItemSchema, o.Append(",\"items\":"));
                }
                else
                {
                    MapSchema mapSchema = s as MapSchema;
                    Build(env, mapSchema.ValueSchema, o.Append(",\"values\":"));
                }
                return(o.Append('}'));

            case Schema.Type.Enumeration:
            case Schema.Type.Fixed:
            case Schema.Type.Record:
                NamedSchema namedSchema = s as NamedSchema;
                var         name        = namedSchema.Fullname;
                if (env.ContainsKey(name))
                {
                    return(o.Append(env[name]));
                }
                var qname = "\"" + name + "\"";
                env.Add(name, qname);
                o.Append("{\"name\":").Append(qname);
                o.Append(",\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append('\"');
                if (st == Schema.Type.Enumeration)
                {
                    EnumSchema enumSchema = s as EnumSchema;
                    o.Append(",\"symbols\":[");
                    foreach (var enumSymbol in enumSchema.Symbols)
                    {
                        if (!firstTime)
                        {
                            o.Append(',');
                        }
                        else
                        {
                            firstTime = false;
                        }
                        o.Append('\"').Append(enumSymbol).Append('\"');
                    }
                    o.Append(']');
                }
                else if (st == Schema.Type.Fixed)
                {
                    FixedSchema fixedSchema = s as FixedSchema;
                    o.Append(",\"size\":")
                    .Append(fixedSchema.Size.ToString(CultureInfo.InvariantCulture));
                }
                else      // st == Schema.Type.Record
                {
                    RecordSchema recordSchema = s as RecordSchema;
                    o.Append(",\"fields\":[");
                    foreach (var field in recordSchema.Fields)
                    {
                        if (!firstTime)
                        {
                            o.Append(',');
                        }
                        else
                        {
                            firstTime = false;
                        }
                        o.Append("{\"name\":\"").Append(field.Name).Append('\"');
                        Build(env, field.Schema, o.Append(",\"type\":")).Append('}');
                    }
                    o.Append(']');
                }
                return(o.Append('}'));

            default:        //boolean, bytes, double, float, int, long, null, string
                return(o.Append('\"').Append(s.Name).Append('\"'));
            }
        }