コード例 #1
0
        private void AddRecordFields(
            IEnumerable <MemberSerializationInfo> members,
            Dictionary <string, NamedSchema> schemas,
            uint currentDepth,
            RecordSchema record)
        {
            int index = 0;

            foreach (MemberSerializationInfo info in members)
            {
                var property = info.MemberInfo as PropertyInfo;
                var field    = info.MemberInfo as FieldInfo;

                Type memberType;
                if (property != null)
                {
                    memberType = property.PropertyType;
                }
                else if (field != null)
                {
                    memberType = field.FieldType;
                }
                else
                {
                    throw new SerializationException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  "Type member '{0}' is not supported.",
                                  info.MemberInfo.GetType().Name));
                }

                TypeSchema fieldSchema = this.TryBuildUnionSchema(memberType, info.MemberInfo, schemas, currentDepth)
                                         ?? this.TryBuildFixedSchema(memberType, info.MemberInfo, record)
                                         ?? this.CreateSchema(info.Nullable, memberType, schemas, currentDepth + 1, info.DefaultValue?.GetType(), info.MemberInfo);



                var aliases = info
                              .Aliases
                              .ToList();
                var recordField = new RecordFieldSchema(
                    new NamedEntityAttributes(new SchemaName(info.Name), aliases, info.Doc),
                    fieldSchema,
                    SortOrder.Ascending,
                    info.HasDefaultValue,
                    info.DefaultValue,
                    info.MemberInfo,
                    index++);
                record.AddField(recordField);
            }
        }
コード例 #2
0
        private Schema.Schema BuildSchema(CodeType type, IDictionary <SchemaName, Schema.Schema> parsedSchemas)
        {
            var schemaName = new SchemaName(type.Name, type.CodeNamespace, null);

            Schema.Schema schema;
            if (parsedSchemas.TryGetValue(schemaName, out schema))
            {
                return(schema);
            }

            var definition = type.Definition;

            if (definition is Struct)
            {
                var record = new RecordSchema(schemaName, null, null, null, null, false);
                parsedSchemas[schemaName] = schema = record;

                int pos = 0;
                foreach (var idlField in ((Struct)definition).Fields)
                {
                    var actualFieldSchema = BuildSchema(type.IdlNamespace, idlField.Type, parsedSchemas);
                    var fieldSchema       =
                        new UnionSchema(new[] { actualFieldSchema, PrimitiveSchema.NewInstance("null") },
                                        null);
                    var field = new Field(fieldSchema, _typeMangler.MangleFieldName(idlField.Name), null, pos++, null,
                                          null, Field.SortOrder.Ignore, null);
                    record.AddField(field);
                }
            }
            else if (definition is IntegerEnum)
            {
                var fields  = ((IntegerEnum)definition).Fields;
                var symbols = new List <KeyValuePair <string, int?> >(fields.Count);
                foreach (var symbol in fields)
                {
                    symbols.Add(new KeyValuePair <string, int?>(_typeMangler.MangleConstantName(symbol.Name),
                                                                (int?)symbol.ExplicitValue));
                }
                parsedSchemas[schemaName] = schema = new EnumSchema(schemaName, null, null, symbols.ToArray(), null);
            }

            if (schema == null)
            {
                throw new NotSupportedException("Unsupport code type for schema generation: " + type);
            }
            return(schema);
        }