Exemplo n.º 1
0
        public CompositeTypeGo(IModelType wrappedType)
        {
            if (!wrappedType.ShouldBeSyntheticType())
            {
                throw new ArgumentException("{0} is not a valid type for SyntheticType", wrappedType.ToString());
            }

            if (string.IsNullOrWhiteSpace(Documentation))
            {
                Documentation = "...";
            }

            // gosdk: Ensure the generated name does not collide with existing type names
            BaseType = wrappedType;

            IModelType elementType = GetElementType(wrappedType);

            if (elementType is PrimaryType)
            {
                var type = (elementType as PrimaryType).KnownPrimaryType;
                switch (type)
                {
                case KnownPrimaryType.Object:
                    Name += "SetObject";
                    break;

                case KnownPrimaryType.Boolean:
                    Name += "Bool";
                    break;

                case KnownPrimaryType.Double:
                    Name += "Float64";
                    break;

                case KnownPrimaryType.Int:
                    Name += "Int32";
                    break;

                case KnownPrimaryType.Long:
                    Name += "Int64";
                    break;

                case KnownPrimaryType.Stream:
                    Name += "ReadCloser";
                    break;

                default:
                    Name += type.ToString();
                    break;
                }
            }
            else if (elementType is EnumType)
            {
                Name += "String";
            }
            else
            {
                Name += elementType.Name;
            }

            // add the wrapped type as a property named Value
            var p = new PropertyGo
            {
                Name           = "Value",
                SerializedName = "value",
                ModelType      = wrappedType
            };

            base.Add(p);
            AddPolymorphicPropertyIfNecessary();

            IsWrapperType = true;
        }
        public string Fields()
        {
            var indented   = new IndentedStringBuilder("    ");
            var properties = Properties.Cast <PropertyGo>().ToList();

            if (BaseModelType != null)
            {
                indented.Append(((CompositeTypeGo)BaseModelType).Fields());
            }

            // If the type is a paged model type, ensure the nextLink field exists
            // Note: Inject the field into a copy of the property list so as to not pollute the original list
            if (!string.IsNullOrEmpty(NextLink))
            {
                var nextLinkField = NextLink;
                foreach (Property p in properties)
                {
                    p.Name = CodeNamerGo.PascalCaseWithoutChar(p.Name, '.');
                    if (p.Name.EqualsIgnoreCase(nextLinkField))
                    {
                        p.Name = nextLinkField;
                    }
                }

                var baseHasNextLink = false;
                if (BaseModelType != null)
                {
                    baseHasNextLink = BaseModelType.Properties.Any(p => p.Name.EqualsIgnoreCase(nextLinkField));
                }

                if (!baseHasNextLink && !properties.Any(p => p.Name.EqualsIgnoreCase(nextLinkField)))
                {
                    var property = new PropertyGo();
                    property.Name      = nextLinkField;
                    property.ModelType = new PrimaryTypeGo(KnownPrimaryType.String);
                    properties.Add(property);
                }
            }

            // Emit each property, except for named Enumerated types, as a pointer to the type
            foreach (var property in properties)
            {
                var enumType = property.ModelType as EnumTypeGo;
                if (enumType != null && enumType.IsNamed)
                {
                    indented.AppendFormat("{0} {1} {2}\n",
                                          property.Name,
                                          enumType.Name,
                                          property.JsonTag());
                }
                else if (property.ModelType is DictionaryType)
                {
                    indented.AppendFormat("{0} *{1} {2}\n", property.Name, (property.ModelType as DictionaryTypeGo).Name, property.JsonTag());
                }
                else if (property.ModelType.PrimaryType(KnownPrimaryType.Object))
                {
                    // TODO: I don't think this is the best way to handle object types
                    indented.AppendFormat("{0} *{1} {2}\n", property.Name, property.ModelType.Name, property.JsonTag());
                }
                else if (property.ShouldBeFlattened())
                {
                    // embed as an anonymous struct.  note that the ordering of this clause is
                    // important, i.e. we don't want to flatten primary types like dictionaries.
                    indented.AppendFormat("*{0} {1}\n", property.ModelType.Name, property.JsonTag());
                    property.Extensions[SwaggerExtensions.FlattenOriginalTypeName] = Name;
                }
                else
                {
                    indented.AppendFormat("{0} *{1} {2}\n", property.Name, property.ModelType.Name, property.JsonTag());
                }
            }

            return(indented.ToString());
        }