示例#1
0
文件: CodeGen.cs 项目: yanivru/avro
        /// <summary>
        /// Creates an enum declaration.
        /// </summary>
        /// <param name="schema">enum schema.</param>
        /// <exception cref="CodeGenException">
        /// Unable to cast schema into an enum
        /// or
        /// Enum symbol " + symbol + " is a C# reserved keyword
        /// or
        /// Namespace required for enum schema " + enumschema.Name.
        /// </exception>
        protected virtual void processEnum(Schema schema)
        {
            EnumSchema enumschema = schema as EnumSchema;

            if (enumschema == null)
            {
                throw new CodeGenException("Unable to cast schema into an enum");
            }

            CodeTypeDeclaration ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(enumschema.Name));

            ctd.IsEnum     = true;
            ctd.Attributes = MemberAttributes.Public;

            if (enumschema.Documentation != null)
            {
                ctd.Comments.Add(createDocComment(enumschema.Documentation));
            }

            foreach (string symbol in enumschema.Symbols)
            {
                if (CodeGenUtil.Instance.ReservedKeywords.Contains(symbol))
                {
                    throw new CodeGenException("Enum symbol " + symbol + " is a C# reserved keyword");
                }

                CodeMemberField field = new CodeMemberField(typeof(int), symbol);
                ctd.Members.Add(field);
            }

            string nspace = enumschema.Namespace;

            if (string.IsNullOrEmpty(nspace))
            {
                throw new CodeGenException("Namespace required for enum schema " + enumschema.Name);
            }

            CodeNamespace codens = AddNamespace(nspace);

            codens.Types.Add(ctd);
        }
示例#2
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>
        public override bool CanRead(Schema writerSchema)
        {
            if (writerSchema.Tag != Tag)
            {
                return(false);
            }

            EnumSchema that = writerSchema as EnumSchema;

            if (!that.SchemaName.Equals(SchemaName))
            {
                if (!InAliases(that.SchemaName))
                {
                    return(false);
                }
            }

            // we defer checking of symbols. Writer may have a symbol missing from the reader,
            // but if writer never used the missing symbol, then reader should still be able to read the data

            return(true);
        }
示例#3
0
 /// <summary>
 /// Checks equality of two enum schema
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public override bool Equals(object obj)
 {
     if (obj == this)
     {
         return(true);
     }
     if (obj != null && obj is EnumSchema)
     {
         EnumSchema that = obj as EnumSchema;
         if (SchemaName.Equals(that.SchemaName) && Count == that.Count)
         {
             for (int i = 0; i < Count; i++)
             {
                 if (!Symbols[i].Equals(that.Symbols[i]))
                 {
                     return(false);
                 }
             }
             return(areEqual(that.Props, this.Props));
         }
     }
     return(false);
 }
示例#4
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('\"'));
            }
        }
示例#5
0
        private static void WriteEnum(TextWriter writer, EnumSchema schema, string parentNamespace, WriterMode mode, bool stripNs)
        {
            var ns = parentNamespace;

            if (!string.IsNullOrEmpty(schema.Namespace))
            {
                ns = schema.Namespace;
            }
            if (stripNs && ns == parentNamespace)
            {
                ns = null;
            }
            switch (mode)
            {
            case WriterMode.Canonical:
                writer.Write(@"{""name"":""");
                writer.Write(schema.FullName);
                writer.Write(@"""");
                writer.Write(@",""type"":""enum""");
                writer.Write(@",""symbols"":[");
                writer.Write(string.Join(",", schema.Symbols.Select(r => $"\"{r}\"")));
                writer.Write(@"]");
                writer.Write(@"}");
                break;

            case WriterMode.Full:
                writer.Write(@"{ ""type"": ""enum""");
                writer.Write(@", ""name"": """);
                writer.Write(schema.Name);
                writer.Write(@"""");
                writer.Write(@", ""namespace"": """);
                writer.Write(schema.Namespace);
                writer.Write(@"""");
                writer.Write(@", ""symbols"": [");
                writer.Write(string.Join(", ", schema.Symbols.Select(r => $"\"{r}\"")));
                writer.Write(@"]");
                writer.Write(@", ""doc"": """);
                writer.Write(schema.Doc);
                writer.Write(@"""");
                writer.Write(@", ""aliases"": [");
                writer.Write(string.Join(", ", schema.Aliases.Select(r => $"\"{r}\"")));
                writer.Write(@"]");
                writer.Write(@" }");
                break;

            default:
                writer.Write(@"{ ""type"": ""enum""");
                writer.Write(@", ""name"": """);
                writer.Write(schema.Name);
                writer.Write(@"""");
                if (!string.IsNullOrEmpty(ns))
                {
                    writer.Write(@", ""namespace"": """);
                    writer.Write(ns);
                    writer.Write(@"""");
                }
                writer.Write(@", ""symbols"": [");
                writer.Write(string.Join(", ", schema.Symbols.Select(r => $"\"{r}\"")));
                writer.Write(@"]");
                if (!string.IsNullOrEmpty(schema.Doc))
                {
                    writer.Write(@", ""doc"": """);
                    writer.Write(schema.Doc);
                    writer.Write(@"""");
                }
                if (schema.Aliases.Count > 0)
                {
                    writer.Write(@", ""aliases"": [");
                    writer.Write(string.Join(", ", schema.Aliases.Select(r => $"\"{r}\"")));
                    writer.Write(@"]");
                }
                writer.Write(@" }");
                break;
            }
        }