예제 #1
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);
        }
 public virtual EnumFieldContext FieldFromIdl(IntegerEnumField field)
 {
     return(new EnumFieldContext(field.DocStringLines, _typeMangler.MangleConstantName(field.Name), field.Value));
 }