Inheritance: NamedSchema
コード例 #1
0
ファイル: Message.cs プロジェクト: drzo/opensim4opencog
 /// <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;
 }
コード例 #2
0
ファイル: RecordSchema.cs プロジェクト: thirumg/Avro.NET
        internal static RecordSchema NewInstance(Type type, JToken j, Names names)
        {
            JToken jfields = j["fields"];

            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");
            }

            Name name = GetName(j);
            IDictionary<string, Field> fields = new Dictionary<string, Field>();
            RecordSchema result = new RecordSchema(type, name, fields, names);
            foreach (JObject jfield in jfields)
            {
                string fieldName = JsonHelper.GetRequiredString(jfield, "name");
                Field field = createField(jfield, names);
                fields.Add(fieldName, field);
            }
            return result;
        }
コード例 #3
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('\"'));
            }
        }
コード例 #4
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 at '{jtok.Path}'");
            }
            if (jfields.Type != JTokenType.Array)
            {
                throw new SchemaParseException($"'fields' not an array for record at '{jtok.Path}'");
            }

            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>();
            RecordSchema result;

            try
            {
                result = new RecordSchema(type, name, aliases, props, fields, request, fieldMap, fieldAliasMap, names,
                                          JsonHelper.GetOptionalString(jtok, "doc"));
            }
            catch (SchemaParseException e)
            {
                throw new SchemaParseException($"{e.Message} at '{jtok.Path}'", e);
            }

            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);
                try
                {
                    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);
                        }
                    }
                }
                catch (SchemaParseException e)
                {
                    throw new SchemaParseException($"{e.Message} at '{jfield.Path}'", e);
                }
            }
            return(result);
        }
コード例 #5
0
 public RecordSchemaPair(RecordSchema first, RecordSchema second)
 {
     this.first  = first;
     this.second = second;
 }
コード例 #6
0
ファイル: CodeGen.cs プロジェクト: yanivru/avro
        /// <summary>
        /// Creates a class declaration.
        /// </summary>
        /// <param name="schema">record schema.</param>
        /// <returns>
        /// A new class code type declaration.
        /// </returns>
        /// <exception cref="CodeGenException">
        /// Unable to cast schema into a record
        /// or
        /// Namespace required for record schema " + recordSchema.Name.
        /// </exception>
        protected virtual CodeTypeDeclaration processRecord(Schema schema)
        {
            RecordSchema recordSchema = schema as RecordSchema;

            if (recordSchema == null)
            {
                throw new CodeGenException("Unable to cast schema into a record");
            }

            bool isError = recordSchema.Tag == Schema.Type.Error;

            // declare the class
            var ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(recordSchema.Name));
            var baseTypeReference = new CodeTypeReference(
                isError ? typeof(Specific.SpecificException) : typeof(Specific.ISpecificRecord),
                CodeTypeReferenceOptions.GlobalReference);

            ctd.BaseTypes.Add(baseTypeReference);

            ctd.Attributes = MemberAttributes.Public;
            ctd.IsClass    = true;
            ctd.IsPartial  = true;

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

            createSchemaField(schema, ctd, isError);

            // declare Get() to be used by the Writer classes
            var cmmGet = new CodeMemberMethod();

            cmmGet.Name       = "Get";
            cmmGet.Attributes = MemberAttributes.Public;
            cmmGet.ReturnType = new CodeTypeReference("System.Object");
            cmmGet.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
            StringBuilder getFieldStmt = new StringBuilder("switch (fieldPos)")
                                         .AppendLine().AppendLine("\t\t\t{");

            // declare Put() to be used by the Reader classes
            var cmmPut = new CodeMemberMethod();

            cmmPut.Name       = "Put";
            cmmPut.Attributes = MemberAttributes.Public;
            cmmPut.ReturnType = new CodeTypeReference(typeof(void));
            cmmPut.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
            cmmPut.Parameters.Add(new CodeParameterDeclarationExpression("System.Object", "fieldValue"));
            var putFieldStmt = new StringBuilder("switch (fieldPos)")
                               .AppendLine().AppendLine("\t\t\t{");

            if (isError)
            {
                cmmGet.Attributes |= MemberAttributes.Override;
                cmmPut.Attributes |= MemberAttributes.Override;
            }

            foreach (Field field in recordSchema.Fields)
            {
                // Determine type of field
                bool   nullibleEnum = false;
                string baseType     = getType(field.Schema, false, ref nullibleEnum);
                var    ctrfield     = new CodeTypeReference(baseType);

                // Create field
                string privFieldName = string.Concat("_", field.Name);
                var    codeField     = new CodeMemberField(ctrfield, privFieldName);
                codeField.Attributes = MemberAttributes.Private;
                if (field.Schema is EnumSchema es && es.Default != null)
                {
                    codeField.InitExpression = new CodeTypeReferenceExpression($"{es.Name}.{es.Default}");
                }

                // Process field documentation if it exist and add to the field
                CodeCommentStatement propertyComment = null;
                if (!string.IsNullOrEmpty(field.Documentation))
                {
                    propertyComment = createDocComment(field.Documentation);
                    if (propertyComment != null)
                    {
                        codeField.Comments.Add(propertyComment);
                    }
                }

                // Add field to class
                ctd.Members.Add(codeField);

                // Create reference to the field - this.fieldname
                var fieldRef    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), privFieldName);
                var mangledName = CodeGenUtil.Instance.Mangle(field.Name);

                // Create field property with get and set methods
                var property = new CodeMemberProperty();
                property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
                property.Name       = mangledName;
                property.Type       = ctrfield;
                property.GetStatements.Add(new CodeMethodReturnStatement(fieldRef));
                property.SetStatements.Add(new CodeAssignStatement(fieldRef, new CodePropertySetValueReferenceExpression()));
                if (propertyComment != null)
                {
                    property.Comments.Add(propertyComment);
                }

                // Add field property to class
                ctd.Members.Add(property);

                // add to Get()
                getFieldStmt.Append("\t\t\tcase ");
                getFieldStmt.Append(field.Pos);
                getFieldStmt.Append(": return this.");
                getFieldStmt.Append(mangledName);
                getFieldStmt.AppendLine(";");

                // add to Put()
                putFieldStmt.Append("\t\t\tcase ");
                putFieldStmt.Append(field.Pos);
                putFieldStmt.Append(": this.");
                putFieldStmt.Append(mangledName);

                if (nullibleEnum)
                {
                    putFieldStmt.Append(" = fieldValue == null ? (");
                    putFieldStmt.Append(baseType);
                    putFieldStmt.Append(")null : (");

                    string type = baseType.Remove(0, 16);  // remove System.Nullable<
                    type = type.Remove(type.Length - 1);   // remove >

                    putFieldStmt.Append(type);
                    putFieldStmt.AppendLine(")fieldValue; break;");
                }
                else
                {
                    putFieldStmt.Append(" = (");
                    putFieldStmt.Append(baseType);
                    putFieldStmt.AppendLine(")fieldValue; break;");
                }
            }

            // end switch block for Get()
            getFieldStmt.AppendLine("\t\t\tdefault: throw new global::Avro.AvroRuntimeException(\"Bad index \" + fieldPos + \" in Get()\");")
            .Append("\t\t\t}");
            var cseGet = new CodeSnippetExpression(getFieldStmt.ToString());

            cmmGet.Statements.Add(cseGet);
            ctd.Members.Add(cmmGet);

            // end switch block for Put()
            putFieldStmt.AppendLine("\t\t\tdefault: throw new global::Avro.AvroRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");")
            .Append("\t\t\t}");
            var csePut = new CodeSnippetExpression(putFieldStmt.ToString());

            cmmPut.Statements.Add(csePut);
            ctd.Members.Add(cmmPut);

            string nspace = recordSchema.Namespace;

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

            CodeNamespace codens = AddNamespace(nspace);

            codens.Types.Add(ctd);

            return(ctd);
        }
コード例 #7
0
        /// <summary>
        /// Creates a class declaration
        /// </summary>
        /// <param name="schema">record schema</param>
        /// <param name="ns">namespace</param>
        /// <returns></returns>
        protected virtual CodeTypeDeclaration processRecord(Schema schema)
        {
            RecordSchema recordSchema = schema as RecordSchema;

            if (null == recordSchema)
            {
                throw new CodeGenException("Unable to cast schema into a record");
            }

            // declare the class
            var ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(recordSchema.Name));

            //@todo implments
            if (DoJava)
            {
                ctd.BaseTypes.Add(SystemObjectStr);
                ctd.BaseTypes.Add("SpecificRecord");
            }
            else
            {
                ctd.BaseTypes.Add("ISpecificRecord");
            }
            var interf = processRecordInterface(schema);

            ctd.BaseTypes.Add(interf.Name);
            ctd.Attributes = MemberAttributes.Public;
            ctd.IsClass    = true;
            ctd.IsPartial  = true;

            createSchemaField(schema, ctd, false, false);

            // declare Get() to be used by the Writer classes
            var cmmGet = new CodeMemberMethod();

            cmmGet.Name       = ToLangCase("Get", !DoJava);
            cmmGet.Attributes = MemberAttributes.Public;
            cmmGet.ReturnType = new CodeTypeReference(SystemObjectStr);
            cmmGet.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
            StringBuilder getFieldStmt = new StringBuilder("switch (fieldPos)\n\t\t\t{\n");

            // declare Put() to be used by the Reader classes
            var cmmPut = new CodeMemberMethod();

            cmmPut.Name       = ToLangCase("Put", !DoJava);
            cmmPut.Attributes = MemberAttributes.Public;
            cmmPut.ReturnType = new CodeTypeReference(typeof(void));
            cmmPut.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
            cmmPut.Parameters.Add(new CodeParameterDeclarationExpression(SystemObjectStr, "fieldValue"));
            var putFieldStmt = new StringBuilder("switch (fieldPos)\n\t\t\t{\n");
            HashSet <CodeTypeReference> feildRefs = new HashSet <CodeTypeReference>();

            foreach (Field field in recordSchema.Fields)
            {
                // Determine type of field
                bool   nullibleEnum = false;
                string baseType     = getType(field.Schema, false, ref nullibleEnum, false);
                var    ctrfield     = new CodeTypeReference(baseType);

                // Create field
                string privFieldName = string.Concat("_", field.Name);
                if (DoJava)
                {
                    privFieldName = "m" + privFieldName;
                }
                var codeField = new CodeMemberField(ctrfield, privFieldName);
                codeField.Attributes = MemberAttributes.Private;

                // Process field documentation if it exist and add to the field
                CodeCommentStatement propertyComment = null;
                if (!string.IsNullOrEmpty(field.Documentation))
                {
                    propertyComment = createDocComment(field.Documentation);
                    if (null != propertyComment)
                    {
                        codeField.Comments.Add(propertyComment);
                    }
                }

                // Add field to class
                ctd.Members.Add(codeField);

                // Create reference to the field - this.fieldname
                var fieldRef    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), privFieldName);
                var mangledName = CodeGenUtil.Instance.Mangle(field.Name);

                // Create field property with get and set methods
                AddProperty(ctd, ctrfield, propertyComment, mangledName, fieldRef, false);

                string getsetName = mangledName;
                if (DoJava)
                {
                    getsetName = privFieldName;
                }

                // add to Get()
                getFieldStmt.Append("\t\t\tcase ");
                getFieldStmt.Append(field.Pos);
                getFieldStmt.Append(": return this.");
                getFieldStmt.Append(getsetName);
                getFieldStmt.Append(";\n");

                // add to Put()
                putFieldStmt.Append("\t\t\tcase ");
                putFieldStmt.Append(field.Pos);
                putFieldStmt.Append(": this.");
                putFieldStmt.Append(getsetName);

                feildRefs.Add(ctrfield);
                if (baseType.EndsWith("long"))
                {
                }
                var castType = baseType;
                if (DoJava)
                {
                    castType = getType(field.Schema, true, ref nullibleEnum, false);
                }
                if (nullibleEnum)
                {
                    putFieldStmt.Append(" = fieldValue == null ? (");
                    putFieldStmt.Append(baseType);
                    putFieldStmt.Append(")null : (");

                    string type = baseType.Remove(0, 16); // remove System.Nullable<
                    type = type.Remove(type.Length - 1);  // remove >

                    putFieldStmt.Append(type);
                    putFieldStmt.Append(")fieldValue; break;\n");
                }
                else
                {
                    putFieldStmt.Append(" = (");
                    putFieldStmt.Append(castType);
                    putFieldStmt.Append(")fieldValue; break;\n");
                }
            }

            // end switch block for Get()
            getFieldStmt.Append(
                "\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Get()\");\n\t\t\t}");
            var    cseGet = new CodeSnippetExpression(getFieldStmt.ToString());
            string cs     = cseGet.Value;

            cmmGet.Statements.Add(cseGet);
            ctd.Members.Add(cmmGet);

            // end switch block for Put()
            putFieldStmt.Append(
                "\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");\n\t\t\t}");
            var csePut = new CodeSnippetExpression(putFieldStmt.ToString());

            cmmPut.Statements.Add(csePut);
            ctd.Members.Add(cmmPut);

            string nspace = recordSchema.Namespace;

            if (string.IsNullOrEmpty(nspace))
            {
                throw new CodeGenException("Namespace required for record schema " + recordSchema.Name);
            }
            CodeNamespace codens = addNamespace(nspace);

            codens.Types.Add(ctd);
            foreach (CodeTypeReference reference in feildRefs)
            {
                //codens.Types.Add(reference);
            }

            return(ctd);
        }
コード例 #8
0
        }  // end ContentsToString

        /// <summary>
        /// Decodes binary encoded data of a dynamically created table returned by the server.
        /// </summary>
        /// <param name="dynamic_table_schema_string">The schema string for the dynamically created table.</param>
        /// <param name="encoded_data">The binary encoded data.</param>
        /// <returns>A list of KineticaRecord objects with the decoded data.</returns>
        public static IList <KineticaRecord> DecodeDynamicTableRecords(string dynamic_table_schema_string, byte[] encoded_data)
        {
            // Get a record schema from the schema string
            Schema dynamic_table_schema;

            try
            {
                dynamic_table_schema = Avro.Schema.Parse(dynamic_table_schema_string);
            }
            catch (Exception ex)
            {
                throw new KineticaException(ex.ToString());
            }

            // The container for the decoded data (put into distinct records)
            IList <KineticaRecord> records = new List <KineticaRecord>();

            // Decode the dynamic table schema to extract the column names and types.
            // Then, decode each individual record.
            using (var ms = new MemoryStream(encoded_data))
            {
                // Read the table schema into a new GenericRecord
                // ----------------------------------------------
                var           reader           = new Avro.Generic.DefaultReader(dynamic_table_schema, dynamic_table_schema);
                BinaryDecoder decoder          = new BinaryDecoder(ms);
                Avro.Generic.GenericRecord obj = (Avro.Generic.GenericRecord)reader.Read(null, dynamic_table_schema, dynamic_table_schema, decoder);

                // Extract the column names from the encoded data
                object   column_headers_0 = new object();
                Object[] column_headers   = null;
                if (obj.TryGetValue("column_headers", out column_headers_0))     // try to get the data out
                {
                    column_headers = ( Object[] )column_headers_0;
                }

                // Extract the column types from the encoded data
                object   column_types_0 = new object();
                Object[] column_types   = null;
                if (obj.TryGetValue("column_datatypes", out column_types_0))     // try to get the data out
                {
                    column_types = ( Object[] )column_types_0;
                }

                // Find out how many columns are returned
                int num_columns = column_headers.Length;

                // Extract the column data from the encoded data (ignore the headers and types)
                // and create a list with only the record data
                Object[][] encoded_column_data = new Object[num_columns][];

                for (int i = 0; i < num_columns; ++i)
                {
                    // Get the column name (e.g. the first column is titled "column_1")
                    string column_name = $"column_{i+1}";

                    // Get the column data out
                    object   column_data_0 = new object();
                    Object[] column_data   = null;
                    if (obj.TryGetValue(column_name, out column_data_0))     // try to get the data out
                    {
                        column_data = ( Object[] )column_data_0;
                    }

                    // Save this column's data in the 2D array declared above
                    encoded_column_data[i] = column_data;
                }  // done separating the column data from the headers and type


                // Find out how many values per column are returned
                int num_records = encoded_column_data[0].Length;

                // Make sure that all the column data are of the same length
                foreach (Object[] l in encoded_column_data)
                {
                    if (l.Length != num_records)
                    {
                        throw new KineticaException("Dynamic table has uneven column data lengths");
                    }
                }

                // Based on the column headers and types, create a KineticaType
                KineticaType dynamic_record_type = KineticaType.fromDynamicSchema(dynamic_table_schema_string, column_headers, column_types);

                // Using the dynamic record type, create a RecordSchema object
                // ( off which we'll create the records)
                Avro.RecordSchema record_schema = (Avro.RecordSchema)dynamic_record_type.getSchema();

                // Create the records by decoding the binary encoded data
                // (column-major data into row-major data)
                for (int record_idx = 0; record_idx < num_records; ++record_idx)
                {
                    // Create a GenericRecord object based on the KineticaType
                    KineticaRecord record = new KineticaRecord(record_schema);

                    // Go through each column, decode the next value and put it into the record
                    for (int column_idx = 0; column_idx < num_columns; ++column_idx)
                    {
                        // Get the value to be put
                        var val = encoded_column_data[column_idx][record_idx];

                        // Get the property of the record into which the value need to saved
                        var field = record_schema.Fields[column_idx];

                        // Set the value
                        record.Add(field.Name, val);
                    }  // end inner for loop

                    // Save the record
                    records.Add(record);
                } // end outer for loop
            }     // end decoding block

            return(records);
        } // end DecodeDynamicTableRecords
コード例 #9
0
ファイル: GenericRecord.cs プロジェクト: drzo/opensim4opencog
 public GenericRecord(RecordSchema schema)
 {
     this.Schema = schema;
 }
コード例 #10
0
 private void SaveOutSchemeR(Protocol protocol, RecordSchema schema, TextWriter tw)
 {
     protocol = protocol ?? findProtocall(Protos.Values, schema);
     foreach (Field field in schema)
     {
     }
 }
コード例 #11
0
        private static void WriteRecord(TextWriter writer, RecordSchema schema, WriterMode mode, string parentNamespace, ISet <string> namedSchemas, 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"":""record""");
                writer.Write(@",""fields"":[");
                WriteFields(writer, schema, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@"]");
                writer.Write(@"}");
                break;

            case WriterMode.Full:
                writer.Write(@"{ ""type"": ""record""");
                writer.Write(@", ""name"": """);
                writer.Write(schema.Name);
                writer.Write(@"""");
                writer.Write(@", ""namespace"": """);
                writer.Write(schema.Namespace);
                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(@", ""fields"": [");
                WriteFields(writer, schema, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@"]");
                writer.Write(@" }");
                break;

            default:
                writer.Write(@"{ ""type"": ""record""");
                writer.Write(@", ""name"": """);
                writer.Write(schema.Name);
                writer.Write(@"""");
                if (!string.IsNullOrEmpty(ns))
                {
                    writer.Write(@", ""namespace"": """);
                    writer.Write(ns);
                    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(@", ""fields"": [");
                WriteFields(writer, schema, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@"]");
                writer.Write(@" }");
                break;
            }
        }
コード例 #12
0
        /// <summary>
        /// Creates a class declaration
        /// </summary>
        /// <param name="schema">record schema</param>
        /// <param name="ns">namespace</param>
        /// <returns></returns>
        protected virtual CodeTypeDeclaration processRecord(Schema schema)
        {
            RecordSchema recordSchema = schema as RecordSchema;

            if (null == recordSchema)
            {
                throw new CodeGenException("Unable to cast schema into a record");
            }

            bool isError = recordSchema.Tag == Schema.Type.Error;

            // declare the class
            var ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(recordSchema.Name));

            // ctd.BaseTypes.Add(isError ? "SpecificException" : "ISpecificRecord");

            ctd.Attributes = MemberAttributes.Public;
            ctd.IsClass    = true;
            //ctd.IsPartial = true;

            //createSchemaField(schema, ctd, isError);

            //// declare Get() to be used by the Writer classes
            //var cmmGet = new CodeMemberMethod();
            //cmmGet.Name = "Get";
            //cmmGet.Attributes = MemberAttributes.Public;
            //cmmGet.ReturnType = new CodeTypeReference("System.Object");
            //cmmGet.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
            //StringBuilder getFieldStmt = new StringBuilder("switch (fieldPos)\n\t\t\t{\n");

            //// declare Put() to be used by the Reader classes
            //var cmmPut = new CodeMemberMethod();
            //cmmPut.Name = "Put";
            //cmmPut.Attributes = MemberAttributes.Public;
            //cmmPut.ReturnType = new CodeTypeReference(typeof(void));
            //cmmPut.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
            //cmmPut.Parameters.Add(new CodeParameterDeclarationExpression("System.Object", "fieldValue"));
            //var putFieldStmt = new StringBuilder("switch (fieldPos)\n\t\t\t{\n");

            //if (isError)
            //{
            //    cmmGet.Attributes |= MemberAttributes.Override;
            //    cmmPut.Attributes |= MemberAttributes.Override;
            //}

            foreach (Field field in recordSchema.Fields)
            {
                // Determine type of field
                bool   nullibleEnum = false;
                string baseType     = getType(field.Schema, false, ref nullibleEnum);
                var    ctrfield     = new CodeTypeReference(baseType);

                // Create field
                string privFieldName = string.Concat("_", field.Name);
                var    codeField     = new CodeMemberField(ctrfield, privFieldName);
                codeField.Attributes = MemberAttributes.Private;

                // Process field documentation if it exist and add to the field
                CodeCommentStatement propertyComment = null;
                if (!string.IsNullOrEmpty(field.Documentation))
                {
                    propertyComment = createDocComment(field.Documentation);
                    if (null != propertyComment)
                    {
                        codeField.Comments.Add(propertyComment);
                    }
                }

                // Add field to class
                ctd.Members.Add(codeField);

                // Create reference to the field - this.fieldname
                var fieldRef    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), privFieldName);
                var mangledName = CodeGenUtil.Instance.Mangle(field.Name);

                // Create field property with get and set methods
                var property = new CodeMemberProperty();
                property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
                property.Name       = mangledName.FirstCharToUpper();
                property.Type       = ctrfield;
                property.GetStatements.Add(new CodeMethodReturnStatement(fieldRef));
                property.SetStatements.Add(new CodeAssignStatement(fieldRef, new CodePropertySetValueReferenceExpression()));
                if (null != propertyComment)
                {
                    property.Comments.Add(propertyComment);
                }

                // Add field property to class
                ctd.Members.Add(property);

                if (field.Schema.Tag == Schema.Type.Union) // union
                {
                    var unionSchema = (UnionSchema)field.Schema;

                    var attr = new CodeAttributeDeclaration("Microsoft.Hadoop.Avro.AvroUnion");

                    foreach (var item in unionSchema.Schemas)
                    {
                        var name = item.Name;

                        if (name == "null")
                        {
                            name = "Microsoft.Hadoop.Avro.AvroNull";
                        }
                        else if (name == "bytes")
                        {
                            name = "byte[]";
                        }
                        else if (name == "boolean")
                        {
                            name = "bool";
                        }

                        attr.Arguments.Add(new CodeAttributeArgument(new CodeSnippetExpression(string.Format("typeof({0})", name))));
                    }

                    property.CustomAttributes.Add(attr);
                }

                // add to Get()
                //getFieldStmt.Append("\t\t\tcase ");
                //getFieldStmt.Append(field.Pos);
                //getFieldStmt.Append(": return this.");
                //getFieldStmt.Append(privFieldName);
                //getFieldStmt.Append(";\n");

                //// add to Put()
                //putFieldStmt.Append("\t\t\tcase ");
                //putFieldStmt.Append(field.Pos);
                //putFieldStmt.Append(": this.");
                //putFieldStmt.Append(privFieldName);

                //if (nullibleEnum)
                //{
                //    putFieldStmt.Append(" = fieldValue == null ? (");
                //    putFieldStmt.Append(baseType);
                //    putFieldStmt.Append(")null : (");

                //    string type = baseType.Remove(0, 16);  // remove System.Nullable<
                //    type = type.Remove(type.Length - 1);   // remove >

                //    putFieldStmt.Append(type);
                //    putFieldStmt.Append(")fieldValue; break;\n");
                //}
                //else
                //{
                //    putFieldStmt.Append(" = (");
                //    putFieldStmt.Append(baseType);
                //    putFieldStmt.Append(")fieldValue; break;\n");
                //}
            }

            //// end switch block for Get()
            //getFieldStmt.Append("\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Get()\");\n\t\t\t}");
            //var cseGet = new CodeSnippetExpression(getFieldStmt.ToString());
            //cmmGet.Statements.Add(cseGet);
            //ctd.Members.Add(cmmGet);

            //// end switch block for Put()
            //putFieldStmt.Append("\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");\n\t\t\t}");
            //var csePut = new CodeSnippetExpression(putFieldStmt.ToString());
            //cmmPut.Statements.Add(csePut);
            //ctd.Members.Add(cmmPut);

            string nspace = recordSchema.Namespace;

            if (string.IsNullOrEmpty(nspace))
            {
                throw new CodeGenException("Namespace required for record schema " + recordSchema.Name);
            }
            CodeNamespace codens = addNamespace(nspace);

            codens.Types.Add(ctd);

            return(ctd);
        }
コード例 #13
-1
ファイル: Message.cs プロジェクト: torgebo/avro
        /// <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]);
                    }
                }
            }
        }