コード例 #1
0
        /// <summary>Generates the file.</summary>
        /// <returns>The file contents.</returns>
        public override string GenerateFile()
        {
            var output =
                GenerateType(_resolver.GenerateTypeName()).Code + "\n\n" +
                _resolver.GenerateTypes() + "\n\n" +
                Settings.TransformedExtensionCode + "\n\n";

            return(ConversionUtilities.TrimWhiteSpaces(output));
        }
コード例 #2
0
        /// <summary>Generates the type.</summary>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The code.</returns>
        public override TypeGeneratorResult GenerateType(string typeNameHint)
        {
            var typeName = !string.IsNullOrEmpty(_schema.TypeName) ? _schema.TypeName : _resolver.GenerateTypeName(typeNameHint);

            if (_schema.IsEnumeration)
            {
                var template = LoadTemplate("Enum");

                if (_schema.Type == JsonObjectType.Integer)
                {
                    typeName = typeName + "AsInteger";
                }

                template.Add("name", typeName);
                template.Add("enums", GetEnumeration());

                template.Add("hasDescription", !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description));
                template.Add("description", RemoveLineBreaks(_schema.Description));

                return(new TypeGeneratorResult
                {
                    TypeName = typeName,
                    Code = template.Render()
                });
            }
            else
            {
                var properties = _schema.Properties.Values.Select(property => new
                {
                    Name         = property.Name,
                    PropertyName = property.Name.Contains("-") ? '\"' + property.Name + '\"' : property.Name,
                    Type         = _resolver.Resolve(property, property.Type.HasFlag(JsonObjectType.Null), property.Name),

                    HasDescription = !string.IsNullOrEmpty(property.Description),
                    Description    = property.Description,
                    IsReadOnly     = property.IsReadOnly,

                    IsOptional = !property.IsRequired
                }).ToList();

                var template = LoadTemplate("Interface");
                template.Add("class", typeName);

                template.Add("hasDescription", !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description));
                template.Add("description", RemoveLineBreaks(_schema.Description));

                template.Add("inheritance", _schema.AllOf.Count == 1 ? " extends " + _resolver.Resolve(_schema.AllOf.First(), true, string.Empty) : string.Empty);
                template.Add("properties", properties);

                return(new TypeGeneratorResult
                {
                    TypeName = typeName,
                    Code = template.Render()
                });
            }
        }