private void GenerateSchemas(IEnumerable <Record> results, string outDir)
        {
            var codegen = new Avro.CodeGen();

            var schemaNames = new SchemaNames();

            foreach (var result in results)
            {
                Console.WriteLine("Start processing : {0}", result.Fullname);

                var scheme = Schema.Parse(result.Schema, schemaNames);

                schemaNames.Add((NamedSchema)scheme);

                codegen.AddSchema(scheme);

                Console.WriteLine("Generate : {0}", result.Fullname);
            }

            codegen.GenerateCode();
            codegen.WriteTypes(outDir);
            Console.WriteLine("Completed.");
        }
示例#2
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>
        protected virtual void addName(Schema schema, SchemaNames names)
        {
            NamedSchema ns = schema as NamedSchema;

            if (null != ns)
            {
                if (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:
                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);
            }
        }