コード例 #1
0
        /// <summary>Generates the type.</summary>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The code.</returns>
        public override string GenerateType(string typeNameHint)
        {
            if (_schema.Type == JsonObjectType.String && _schema.Enumeration.Count > 0)
            {
                var template = LoadTemplate("Enum");
                template.Add("name", !string.IsNullOrEmpty(_schema.TypeName) ? _schema.TypeName : typeNameHint);
                template.Add("enums", _schema.Enumeration);
                return(template.Render());
            }
            else
            {
                var properties = _schema.Properties.Values.Select(property => new
                {
                    Name       = property.Name,
                    Type       = _resolver.Resolve(property, property.IsRequired, property.Name),
                    IsOptional = !property.IsRequired
                }).ToList();

                var template = LoadTemplate("Interface");
                template.Add("class", _schema.TypeName);
                template.Add("inheritance", _schema.AllOf.Count == 1 ? " extends " + _resolver.Resolve(_schema.AllOf.First(), true, string.Empty) : string.Empty);
                template.Add("properties", properties);
                return(template.Render());
            }
        }
コード例 #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()
                });
            }
        }
コード例 #3
0
        /// <summary>Generates the file.</summary>
        /// <returns>The file contents.</returns>
        public override string GenerateFile()
        {
            _resolver.Resolve(_schema, false, string.Empty); // register root type

            var template = new FileTemplate() as ITemplate;

            template.Initialize(new FileTemplateModel
            {
                Types = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateTypes(Settings.ProcessedExtensionCode)),

                HasModuleName = !string.IsNullOrEmpty(Settings.ModuleName),
                ModuleName    = Settings.ModuleName,

                ExtensionCodeBefore = Settings.ProcessedExtensionCode.CodeBefore,
                ExtensionCodeAfter  = Settings.ProcessedExtensionCode.CodeAfter
            });
            return(ConversionUtilities.TrimWhiteSpaces(template.Render()));
        }
コード例 #4
0
        /// <summary>Generates the type.</summary>
        /// <param name="fallbackTypeName">The fallback type name.</param>
        /// <returns>The code.</returns>
        public override TypeGeneratorResult GenerateType(string fallbackTypeName)
        {
            var typeName = !string.IsNullOrEmpty(_schema.TypeName) ? _schema.TypeName : fallbackTypeName;

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

                var template = new EnumTemplate() as ITemplate;
                template.Initialize(new EnumTemplateModel(typeName, _schema));
                return(new TypeGeneratorResult
                {
                    TypeName = typeName,
                    Code = template.Render()
                });
            }
            else
            {
                var properties     = _schema.Properties.Values.Select(property => new PropertyModel(property, _resolver, Settings, this)).ToList();
                var hasInheritance = _schema.AllOf.Count == 1;

                var template = Settings.CreateTemplate();
                template.Initialize(new // TODO: Create model class
                {
                    Class = typeName,

                    HasDescription = !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description),
                    Description    = ConversionUtilities.RemoveLineBreaks(_schema.Description),

                    HasInheritance = hasInheritance,
                    Inheritance    = hasInheritance ? " extends " + _resolver.Resolve(_schema.AllOf.First(), true, string.Empty) : string.Empty,
                    Properties     = properties
                });

                return(new TypeGeneratorResult
                {
                    TypeName = typeName,
                    Code = template.Render()
                });
            }
        }
コード例 #5
0
        /// <summary>Generates the file.</summary>
        /// <param name="rootTypeNameHint">The root type name hint.</param>
        /// <returns>The file contents.</returns>
        public override string GenerateFile(string rootTypeNameHint)
        {
            _resolver.Resolve(_schema, false, rootTypeNameHint); // register root type

            var extensionCode = new TypeScriptExtensionCode(Settings.ExtensionCode, Settings.ExtendedClasses);
            var model         = new FileTemplateModel(Settings)
            {
                Types         = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateTypes(extensionCode)),
                ExtensionCode = extensionCode
            };

            var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File", model);

            return(ConversionUtilities.TrimWhiteSpaces(template.Render()));
        }
コード例 #6
0
        /// <summary>Generates the file.</summary>
        /// <returns>The file contents.</returns>
        public override string GenerateFile()
        {
            _resolver.Resolve(_schema, false, string.Empty); // register root type

            var model = new FileTemplateModel(Settings)
            {
                Types = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateTypes(Settings.ProcessedExtensionCode)),

                ExtensionCodeBefore = Settings.ProcessedExtensionCode.CodeBefore,
                ExtensionCodeAfter  = Settings.ProcessedExtensionCode.CodeAfter
            };

            var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File", model);

            return(ConversionUtilities.TrimWhiteSpaces(template.Render()));
        }