Class for union schemas
Inheritance: Avro.UnnamedSchema
        /// <summary>
        /// Constructor for Message class
        /// </summary>
        /// <param name="name">name property</param>
        /// <param name="doc">doc property</param>
        /// <param name="request">list of parameters</param>
        /// <param name="response">response property</param>
        /// <param name="error">error union schema</param>
        public Message(string name, string doc, RecordSchema request, Schema response, UnionSchema error, bool?oneway)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "name cannot be null.");
            }
            this.Request  = request;
            this.Response = response;
            this.Error    = error;
            this.Name     = name;
            this.Doc      = doc;
            this.Oneway   = oneway;

            if (error != null && error.CanRead(Schema.Parse("string")))
            {
                this.SupportedErrors = error;
            }
            else
            {
                this.SupportedErrors = (UnionSchema)Schema.Parse("[\"string\"]");

                if (error != null)
                {
                    for (int i = 0; i < error.Schemas.Count; ++i)
                    {
                        this.SupportedErrors.Schemas.Add(error.Schemas[i]);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the schema of a union with null
        /// </summary>
        /// <param name="schema">union schema</param>
        /// <returns>schema that is nullible</returns>
        public static Schema getNullableType(UnionSchema schema)
        {
            Schema ret = null;

            if (schema.Count == 2)
            {
                bool nullable = false;
                foreach (Schema childSchema in schema.Schemas)
                {
                    if (childSchema.Tag == Schema.Type.Null)
                    {
                        nullable = true;
                    }
                    else
                    {
                        ret = childSchema;
                    }
                }
                if (!nullable)
                {
                    ret = null;
                }
            }
            return(ret);
        }
        /// <summary>
        /// Parses the messages section of a protocol definition
        /// </summary>
        /// <param name="jmessage">messages JSON object</param>
        /// <param name="names">list of parsed names</param>
        /// <param name="encspace">enclosing namespace</param>
        /// <returns></returns>
        internal static Message Parse(JProperty jmessage, SchemaNames names, string encspace)
        {
            string name   = jmessage.Name;
            string doc    = JsonHelper.GetOptionalString(jmessage.Value, "doc");
            bool?  oneway = JsonHelper.GetOptionalBoolean(jmessage.Value, "one-way");

            PropertyMap  props  = Schema.GetProperties(jmessage.Value);
            RecordSchema schema = RecordSchema.NewInstance(Schema.Type.Record, jmessage.Value as JObject, props, names, encspace);

            JToken jresponse = jmessage.Value["response"];
            var    response  = Schema.ParseJson(jresponse, names, encspace);

            JToken      jerrors      = jmessage.Value["errors"];
            UnionSchema uerrorSchema = null;

            if (null != jerrors)
            {
                Schema errorSchema = Schema.ParseJson(jerrors, names, encspace);
                if (!(errorSchema is UnionSchema))
                {
                    throw new AvroException("");
                }

                uerrorSchema = errorSchema as UnionSchema;
            }

            return(new Message(name, doc, schema, response, uerrorSchema, oneway));
        }
Ejemplo n.º 4
0
 public Message(string name, string doc, IList<Parameter> request, Schema response, UnionSchema error)
 {
     if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name", "name cannot be null.");
     this.Request = request;
     this.Response = response;
     this.Error = error;
     this.Name = name;
     this.Doc = doc;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Constructor for Message class
 /// </summary>
 /// <param name="name">name property</param>
 /// <param name="doc">doc property</param>
 /// <param name="request">list of parameters</param>
 /// <param name="response">response property</param>
 /// <param name="error">error union schema</param>
 public Message(string name, string doc, RecordSchema request, Schema response, UnionSchema error, bool? oneway)
 {
     if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name", "name cannot be null.");
     this.Request = request;
     this.Response = response;
     this.Error = error;
     this.Name = name;
     this.Doc = doc;
     this.Oneway = oneway;
 }
Ejemplo n.º 6
0
        private static AvroSchema ParseUnionSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var jArray      = jToken as JArray;
            var unionSchema = new UnionSchema();

            foreach (var item in jArray)
            {
                var schema = ParseSchema(item, namedTypes, enclosingNamespace);
                unionSchema.Add(schema);
            }
            return(unionSchema);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the schema of a union with null.
        /// </summary>
        /// <param name="schema">union schema.</param>
        /// <returns>
        /// schema that is nullable.
        /// </returns>
        /// <exception cref="ArgumentNullException">schema - UnionSchema can not be null.</exception>
        public static Schema GetNullableType(UnionSchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema), "UnionSchema can not be null");
            }

            if (schema.Count == 2 && !schema.Schemas.All(x => x.Tag != Schema.Type.Null))
            {
                return(schema.Schemas.FirstOrDefault(x => x.Tag != Schema.Type.Null));
            }

            return(null);
        }
Ejemplo n.º 8
0
        private static void WriteUnion(TextWriter writer, UnionSchema schema, WriterMode mode, string parentNamespace, ISet <string> namedSchemas, bool stripNs)
        {
            var i = 0;

            switch (mode)
            {
            case WriterMode.Canonical:
                writer.Write(@"[");
                foreach (var item in schema)
                {
                    if (i++ > 0)
                    {
                        writer.Write(",");
                    }
                    Write(writer, item, mode, parentNamespace, namedSchemas, stripNs);
                }
                writer.Write(@"]");
                break;

            case WriterMode.Full:
                writer.Write(@"[");
                foreach (var item in schema)
                {
                    if (i++ > 0)
                    {
                        writer.Write(", ");
                    }
                    Write(writer, item, mode, parentNamespace, namedSchemas, stripNs);
                }
                writer.Write(@"]");
                break;

            default:
                writer.Write(@"[");
                foreach (var item in schema)
                {
                    if (i++ > 0)
                    {
                        writer.Write(", ");
                    }
                    Write(writer, item, mode, parentNamespace, namedSchemas, stripNs);
                }
                writer.Write(@"]");
                break;
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Compares two union schema objects
 /// </summary>
 /// <param name="obj">union schema object to compare against this schema</param>
 /// <returns>true if objects are equal, false otherwise</returns>
 public override bool Equals(object obj)
 {
     if (obj == this)
     {
         return(true);
     }
     if (obj != null && obj is UnionSchema)
     {
         UnionSchema that = obj as UnionSchema;
         if (that.Count == Count)
         {
             for (int i = 0; i < Count; i++)
             {
                 if (!that[i].Equals(this[i]))
                 {
                     return(false);
                 }
             }
             return(areEqual(that.Props, this.Props));
         }
     }
     return(false);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets the schema of a union with null
 /// </summary>
 /// <param name="schema">union schema</param>
 /// <returns>schema that is nullible</returns>
 public static Schema getNullableType(UnionSchema schema)
 {
     Schema ret = null;
     if (schema.Count == 2)
     {
         bool nullable = false;
         foreach (Schema childSchema in schema.Schemas)
         {
             if (childSchema.Tag == Schema.Type.Null)
                 nullable = true;
             else
                 ret = childSchema;
         }
         if (!nullable)
             ret = null;
     }
     return ret;
 }
Ejemplo n.º 11
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}'");
        }
Ejemplo n.º 12
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('\"'));
            }
        }
Ejemplo n.º 13
0
 public static Schema getNullableType(UnionSchema schema)
 {
     return(GetNullableType(schema));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Constructor for Message class
 /// </summary>
 /// <param name="name">name property</param>
 /// <param name="doc">doc property</param>
 /// <param name="request">list of parameters</param>
 /// <param name="response">response property</param>
 /// <param name="error">error union schema</param>
 public Message(string name, string doc, RecordSchema request, Schema response, UnionSchema error, bool?oneway)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentNullException("name", "name cannot be null.");
     }
     this.Request  = request;
     this.Response = response;
     this.Error    = error;
     this.Name     = name;
     this.Doc      = doc;
     this.Oneway   = oneway;
 }
Ejemplo n.º 15
-1
        /// <summary>
        /// Constructor for Message class
        /// </summary>
        /// <param name="name">name property</param>
        /// <param name="doc">doc property</param>
        /// <param name="request">list of parameters</param>
        /// <param name="response">response property</param>
        /// <param name="error">error union schema</param>
        public Message(string name, string doc, RecordSchema request, Schema response, UnionSchema error, bool? oneway)
        {
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name", "name cannot be null.");
            this.Request = request;
            this.Response = response;
            this.Error = error;
            this.Name = name;
            this.Doc = doc;
            this.Oneway = oneway;

            if (error != null && error.CanRead(Schema.Parse("string")))
            {
                this.SupportedErrors = error;
            }
            else
            {
                this.SupportedErrors = (UnionSchema) Schema.Parse("[\"string\"]");

                if (error != null)
                {
                    for (int i = 0; i < error.Schemas.Count; ++i)
                    {
                        this.SupportedErrors.Schemas.Add(error.Schemas[i]);
                    }
                }
            }
        }