Esempio n. 1
0
        /// <summary>
        /// Static function to return a new instance of the named schema
        /// </summary>
        /// <param name="jo">JSON object of the named schema</param>
        /// <param name="names">list of named schemas already read</param>
        /// <param name="encspace">enclosing namespace of the named schema</param>
        /// <returns></returns>
        internal static NamedSchema NewInstance(JObject jo, PropertyMap props, SchemaNames names, string encspace)
        {
            string type = JsonHelper.GetRequiredString(jo, "type");

            switch (type)
            {
            case "fixed":
                return(FixedSchema.NewInstance(jo, props, names, encspace));

            case "enum":
                return(EnumSchema.NewInstance(jo, props, names, encspace));

            case "record":
                return(RecordSchema.NewInstance(Type.Record, jo, props, names, encspace));

            case "error":
                return(RecordSchema.NewInstance(Type.Error, jo, props, names, encspace));

            default:
                NamedSchema result;
                if (names.TryGetValue(type, null, encspace, out result))
                {
                    return(result);
                }
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Compares two fixed schemas
        /// </summary>
        /// <param name="obj">fixed schema to compare against this schema</param>
        /// <returns>true if two schemas are the same, false otherwise</returns>
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }

            if (obj != null && obj is FixedSchema)
            {
                FixedSchema that = obj as FixedSchema;
                return(SchemaName.Equals(that.SchemaName) && Size == that.Size && areEqual(that.Props, this.Props));
            }
            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Checks if this schema can read data written by the given schema. Used for decoding data.
        /// </summary>
        /// <param name="writerSchema">writer schema</param>
        /// <returns>true if this and writer schema are compatible based on the AVRO specification, false otherwise</returns>
        internal override bool CanRead(Schema writerSchema)
        {
            if (writerSchema.Tag != Tag)
            {
                return(false);
            }
            FixedSchema that = writerSchema as FixedSchema;

            if (that.Size != Size)
            {
                return(false);
            }
            if (that.SchemaName.Equals(SchemaName))
            {
                return(true);
            }
            else
            {
                return(InAliases(that.SchemaName));
            }
        }
        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());
                }
                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("\""));
            }
        }