Exemplo n.º 1
0
        /// <summary>
        /// Static function to return new instance of EnumSchema
        /// </summary>
        /// <param name="jtok">JSON object for enum schema</param>
        /// <param name="names">list of named schema already parsed in</param>
        /// <param name="encspace">enclosing namespace for the enum schema</param>
        /// <returns>new instance of enum schema</returns>
        internal static EnumSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            SchemaName name    = NamedSchema.GetName(jtok, encspace);
            var        aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);

            JArray jsymbols = jtok["symbols"] as JArray;

            if (null == jsymbols)
            {
                throw new SchemaParseException("Enum has no symbols: " + name);
            }

            List <string>             symbols   = new List <string>();
            IDictionary <string, int> symbolMap = new Dictionary <string, int>();
            int i = 0;

            foreach (JValue jsymbol in jsymbols)
            {
                string s = (string)jsymbol.Value;
                if (symbolMap.ContainsKey(s))
                {
                    throw new SchemaParseException("Duplicate symbol: " + s);
                }

                symbolMap[s] = i++;
                symbols.Add(s);
            }
            return(new EnumSchema(name, aliases, symbols, symbolMap, props, names,
                                  JsonHelper.GetOptionalString(jtok, "doc")));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Static function to return new instance of the fixed schema class
        /// </summary>
        /// <param name="jtok">JSON object for the fixed schema</param>
        /// <param name="names">list of named schema already parsed in</param>
        /// <param name="encspace">enclosing namespace of the fixed schema</param>
        /// <returns></returns>
        internal static FixedSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            SchemaName name    = NamedSchema.GetName(jtok, encspace);
            var        aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);

            return(new FixedSchema(name, aliases, JsonHelper.GetRequiredInteger(jtok, "size"), props, names));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds a schema name to the map if it doesn't exist yet
        /// </summary>
        /// <param name="name">schema name</param>
        /// <param name="schema">schema object</param>
        /// <returns>true if schema was added to the list, false if schema is already in the list</returns>
        public bool Add(SchemaName name, NamedSchema schema)
        {
            if (Names.ContainsKey(name))
            {
                return(false);
            }

            Names.Add(name, schema);
            return(true);
        }
Exemplo n.º 4
0
 public void AddType(NamedSchema schema)
 {
     if (string.IsNullOrEmpty(schema.Namespace))
     {
         schema.Namespace = Namespace;
     }
     if (_types.Contains(schema))
     {
         throw new AvroException($"Protocol already contains the type: '{schema.FullName}'");
     }
     _types.Add(schema);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Static function to return new instance of the record schema
        /// </summary>
        /// <param name="type">type of record schema, either record or error</param>
        /// <param name="jtok">JSON object for the record schema</param>
        /// <param name="props">dictionary that provides access to custom properties</param>
        /// <param name="names">list of named schema already read</param>
        /// <param name="encspace">enclosing namespace of the records schema</param>
        /// <returns>new RecordSchema object</returns>
        internal static RecordSchema NewInstance(Type type, JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            bool   request = false;
            JToken jfields = jtok["fields"];    // normal record

            if (null == jfields)
            {
                jfields = jtok["request"];      // anonymous record from messages
                if (null != jfields)
                {
                    request = true;
                }
            }
            if (null == jfields)
            {
                throw new SchemaParseException("'fields' cannot be null for record");
            }
            if (jfields.Type != JTokenType.Array)
            {
                throw new SchemaParseException("'fields' not an array for record");
            }

            var name          = GetName(jtok, encspace);
            var aliases       = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);
            var fields        = new List <Field>();
            var fieldMap      = new Dictionary <string, Field>();
            var fieldAliasMap = new Dictionary <string, Field>();
            var result        = new RecordSchema(type, name, aliases, props, fields, request, fieldMap, fieldAliasMap, names,
                                                 JsonHelper.GetOptionalString(jtok, "doc"));

            int fieldPos = 0;

            foreach (JObject jfield in jfields)
            {
                string fieldName = JsonHelper.GetRequiredString(jfield, "name");
                Field  field     = createField(jfield, fieldPos++, names, name.Namespace); // add record namespace for field look up
                fields.Add(field);
                addToFieldMap(fieldMap, fieldName, field);
                addToFieldMap(fieldAliasMap, fieldName, field);

                if (null != field.aliases)    // add aliases to field lookup map so reader function will find it when writer field name appears only as an alias on the reader field
                {
                    foreach (string alias in field.aliases)
                    {
                        addToFieldMap(fieldAliasMap, alias, field);
                    }
                }
            }
            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Static function to return new instance of the fixed schema class
        /// </summary>
        /// <param name="jtok">JSON object for the fixed schema</param>
        /// <param name="props">dictionary that provides access to custom properties</param>
        /// <param name="names">list of named schema already parsed in</param>
        /// <param name="encspace">enclosing namespace of the fixed schema</param>
        /// <returns></returns>
        internal static FixedSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            SchemaName name    = NamedSchema.GetName(jtok, encspace);
            var        aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);

            try
            {
                return(new FixedSchema(name, aliases, JsonHelper.GetRequiredInteger(jtok, "size"), props, names,
                                       JsonHelper.GetOptionalString(jtok, "doc")));
            }
            catch (Exception e)
            {
                throw new SchemaParseException($"{e.Message} at '{jtok.Path}'", e);
            }
        }
Exemplo n.º 7
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.º 8
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('\"'));
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Tries to get the value for the given name fields
 /// </summary>
 /// <param name="name">name of the schema</param>
 /// <param name="space">namespace of the schema</param>
 /// <param name="encspace">enclosing namespace of the schema</param>
 /// <param name="schema">schema object found</param>
 /// <returns>true if name is found in the map, false otherwise</returns>
 public bool TryGetValue(string name, string space, string encspace, out NamedSchema schema)
 {
     SchemaName schemaname = new SchemaName(name, space, encspace);
     return Names.TryGetValue(schemaname, out schema);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Adds a named schema to the list
 /// </summary>
 /// <param name="schema">schema object</param>
 /// <returns>true if schema was added to the list, false if schema is already in the list</returns>
 public bool Add(NamedSchema schema)
 {
     SchemaName name = schema.SchemaName;
     return Add(name, schema);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Adds a schema name to the map if it doesn't exist yet
        /// </summary>
        /// <param name="name">schema name</param>
        /// <param name="schema">schema object</param>
        /// <returns>true if schema was added to the list, false if schema is already in the list</returns>
        public bool Add(SchemaName name, NamedSchema schema)
        {
            if (Names.ContainsKey(name))
                return false;

            Names.Add(name, schema);
            return true;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Recursively search the given schema for named schemas and adds them to the given container.
        /// </summary>
        /// <param name="schema">schema object to search.</param>
        /// <param name="names">list of named schemas.</param>
        /// <exception cref="CodeGenException">Unable to add name for " + schema.Name + " type " + schema.Tag.</exception>
        protected virtual void addName(Schema schema, SchemaNames names)
        {
            NamedSchema ns = schema as NamedSchema;

            if (ns != null && names.Contains(ns.SchemaName))
            {
                return;
            }

            switch (schema.Tag)
            {
            case Schema.Type.Null:
            case Schema.Type.Boolean:
            case Schema.Type.Int:
            case Schema.Type.Long:
            case Schema.Type.Float:
            case Schema.Type.Double:
            case Schema.Type.Bytes:
            case Schema.Type.String:
            case Schema.Type.Logical:
                break;

            case Schema.Type.Enumeration:
            case Schema.Type.Fixed:
                names.Add(ns);
                break;

            case Schema.Type.Record:
            case Schema.Type.Error:
                var rs = schema as RecordSchema;
                names.Add(rs);
                foreach (Field field in rs.Fields)
                {
                    addName(field.Schema, names);
                }

                break;

            case Schema.Type.Array:
                var asc = schema as ArraySchema;
                addName(asc.ItemSchema, names);
                break;

            case Schema.Type.Map:
                var ms = schema as MapSchema;
                addName(ms.ValueSchema, names);
                break;

            case Schema.Type.Union:
                var us = schema as UnionSchema;
                foreach (Schema usc in us.Schemas)
                {
                    addName(usc, names);
                }

                break;

            default:
                throw new CodeGenException("Unable to add name for " + schema.Name + " type " + schema.Tag);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Tries to get the value for the given name fields
        /// </summary>
        /// <param name="name">name of the schema</param>
        /// <param name="space">namespace of the schema</param>
        /// <param name="encspace">enclosing namespace of the schema</param>
        /// <param name="schema">schema object found</param>
        /// <returns>true if name is found in the map, false otherwise</returns>
        public bool TryGetValue(string name, string space, string encspace, out NamedSchema schema)
        {
            SchemaName schemaname = new SchemaName(name, space, encspace);

            return(Names.TryGetValue(schemaname, out schema));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Adds a named schema to the list
        /// </summary>
        /// <param name="schema">schema object</param>
        /// <returns>true if schema was added to the list, false if schema is already in the list</returns>
        public bool Add(NamedSchema schema)
        {
            SchemaName name = schema.SchemaName;

            return(Add(name, schema));
        }