generator

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 28 Imported by: 1

Documentation

Index

Constants

View Source
const (
	RegexPkgPath = "regex"
)

Variables

View Source
var (
	// Debug when the env var DEBUG or SWAGGER_DEBUG is not empty
	// the generators will be very noisy about what they are doing
	Debug = os.Getenv("DEBUG") != "" || os.Getenv("SWAGGER_DEBUG") != ""
)
View Source
var (
	// DefaultLanguageFunc defines the default generation language
	DefaultLanguageFunc func() *LanguageOpts
)
View Source
var (

	// FuncMapFunc yields a map with all functions for templates
	FuncMapFunc func(*LanguageOpts) template.FuncMap
)

Functions

func AddXOrderOnDefaultExample

func AddXOrderOnDefaultExample(yamlDoc interface{}) interface{}

AddXOrderOnDefaultExample amends the spec to specify the map value order in "default" & "example" fields as they appear in the spec (supports yaml documents only).

func AddXOrderOnProperty

func AddXOrderOnProperty(yamlDoc interface{}) interface{}

AddXOrderOnProperty amends the spec to specify property order as they appear in the spec (supports yaml documents only).

func DefaultFuncMap

func DefaultFuncMap(lang *LanguageOpts) template.FuncMap

DefaultFuncMap yields a map with default functions for use n the templates. These are available in every template

func DefaultSectionOpts

func DefaultSectionOpts(gen *GenOpts)

DefaultSectionOpts for a given opts, this is used when no config file is passed and uses the embedded templates when no local override can be found

func Generate

func Generate(opts *GenOpts) error

func RecoverMapValueOrder

func RecoverMapValueOrder(oldValue interface{}) interface{}

func ToKCLValueString added in v0.6.0

func ToKCLValueString(value interface{}) (string, error)

func WithXOrder

func WithXOrder(specPath string, addXOrderFunc func(yamlDoc interface{}) interface{}) string

WithXOrder amends the spec to specify the order of some fields (such as property, default, example, ...). supports yaml documents only.

Types

type GenApp

type GenApp struct {
	GenCommon
	Package      string
	BasePath     string
	Info         *spec.Info
	ExternalDocs *spec.ExternalDocumentation
	Models       []GenDefinition
	GenOpts      *GenOpts
}

GenApp represents all the meta data needed to generate an application from a swagger spec

func (*GenApp) UseFlags

func (g *GenApp) UseFlags() bool

UseFlags returns true when the flag strategy is set to flag

func (*GenApp) UseGoStructFlags

func (g *GenApp) UseGoStructFlags() bool

UseGoStructFlags returns true when no strategy is specified or it is set to "go-flags"

func (*GenApp) UseModernMode

func (g *GenApp) UseModernMode() bool

UseModernMode for https://wiki.mozilla.org/Security/Server_Side_TLS#Modern_compatibility

func (*GenApp) UsePFlags

func (g *GenApp) UsePFlags() bool

UsePFlags returns true when the flag strategy is set to pflag

type GenCommon

type GenCommon struct {
	Copyright        string
	TargetImportPath string
}

GenCommon contains common properties needed across definitions, app and operations TargetImportPath may be used by templates to import other (possibly generated) packages in the generation path (e.g. relative to GOPATH). TargetImportPath is NOT used by standard templates.

type GenDefinition

type GenDefinition struct {
	GenCommon
	GenSchema
	Package              string
	Imports              []importStmt
	ExtraSchemas         GenSchemaList
	DependsOn            []string
	External             bool
	HasPatternValidation bool
}

GenDefinition contains all the properties to generate a definition from a swagger spec

type GenDefinitions

type GenDefinitions []GenDefinition

GenDefinitions represents a list of operations to generate this implements a sort by operation id

func (GenDefinitions) Len

func (g GenDefinitions) Len() int

func (GenDefinitions) Less

func (g GenDefinitions) Less(i, j int) bool

func (GenDefinitions) Swap

func (g GenDefinitions) Swap(i, j int)

type GenOpts

type GenOpts struct {
	ValidateSpec bool
	FlattenOpts  *analysis.FlattenOpts
	KeepOrder    bool

	Spec              string
	ModelPackage      string
	Target            string
	Sections          SectionOpts
	LanguageOpts      *LanguageOpts
	FlagStrategy      string
	CompatibilityMode string
	Copyright         string
}

GenOpts the options for the generator

func (*GenOpts) CheckOpts

func (g *GenOpts) CheckOpts() error

CheckOpts carries out some global consistency checks on options.

func (*GenOpts) EnsureDefaults

func (g *GenOpts) EnsureDefaults() error

EnsureDefaults for these gen opts

type GenSchema

type GenSchema struct {
	Example                    interface{}
	OriginalName               string
	Name                       string
	EscapedName                string
	Suffix                     string
	Path                       string
	ValueExpression            string
	IndexVar                   string
	KeyVar                     string
	Title                      string
	Description                string
	ReceiverName               string
	Items                      *GenSchema
	AllowsAdditionalItems      bool
	HasAdditionalItems         bool
	AdditionalItems            *GenSchema
	Object                     *GenSchema
	XMLName                    string
	CustomTag                  string
	Properties                 GenSchemaList
	AllOf                      GenSchemaList
	HasAdditionalProperties    bool
	IsAdditionalProperties     bool
	AdditionalProperties       *GenSchema
	StrictAdditionalProperties bool
	ReadOnly                   bool
	IsBaseType                 bool
	HasBaseType                bool
	IsSubType                  bool
	IsExported                 bool
	DiscriminatorField         string
	DiscriminatorValue         string
	Discriminates              map[string]string
	Parents                    []string
	Default                    interface{}
	ExternalDocs               *spec.ExternalDocumentation
	// contains filtered or unexported fields
}

GenSchema contains all the information needed to generate the code for a schema

type GenSchemaList

type GenSchemaList []GenSchema

GenSchemaList is a list of schemas for generation.

It can be sorted by name to get a stable struct layout for version control and such

func (GenSchemaList) Len

func (g GenSchemaList) Len() int

func (GenSchemaList) Less

func (g GenSchemaList) Less(i, j int) bool

func (GenSchemaList) Swap

func (g GenSchemaList) Swap(i, j int)

type GenSecurityRequirement

type GenSecurityRequirement struct {
	Name   string
	Scopes []string
}

GenSecurityRequirement represents a security requirement for an operation

type GenSecurityRequirements

type GenSecurityRequirements []GenSecurityRequirement

GenSecurityRequirements represents a compounded security requirement specification. In a []GenSecurityRequirements complete requirements specification, outer elements are interpreted as optional requirements (OR), and inner elements are interpreted as jointly required (AND).

func (GenSecurityRequirements) Len

func (g GenSecurityRequirements) Len() int

func (GenSecurityRequirements) Less

func (g GenSecurityRequirements) Less(i, j int) bool

func (GenSecurityRequirements) Swap

func (g GenSecurityRequirements) Swap(i, j int)

type GenSecurityScheme

type GenSecurityScheme struct {
	AppName      string
	ID           string
	Name         string
	ReceiverName string
	IsBasicAuth  bool
	IsAPIKeyAuth bool
	IsOAuth2     bool
	Scopes       []string
	Source       string
	// from spec.SecurityScheme
	Description      string
	Type             string
	In               string
	Flow             string
	AuthorizationURL string
	TokenURL         string
	Extensions       map[string]interface{}
}

GenSecurityScheme represents a security scheme for code generation

type GenSecuritySchemes

type GenSecuritySchemes []GenSecurityScheme

GenSecuritySchemes sorted representation of serializers

func (GenSecuritySchemes) Len

func (g GenSecuritySchemes) Len() int

func (GenSecuritySchemes) Less

func (g GenSecuritySchemes) Less(i, j int) bool

func (GenSecuritySchemes) Swap

func (g GenSecuritySchemes) Swap(i, j int)

type GenSerGroup

type GenSerGroup struct {
	GenSerializer

	// All media types for this serializer. The redundant representation allows for easier use in templates
	AllSerializers GenSerializers
}

GenSerGroup represents a group of serializers: this links a serializer to a list of prioritized media types (mime).

type GenSerGroups

type GenSerGroups []GenSerGroup

GenSerGroups sorted representation of serializer groups

func (GenSerGroups) Len

func (g GenSerGroups) Len() int

func (GenSerGroups) Less

func (g GenSerGroups) Less(i, j int) bool

func (GenSerGroups) Swap

func (g GenSerGroups) Swap(i, j int)

type GenSerializer

type GenSerializer struct {
	AppName        string // Application name
	ReceiverName   string
	Name           string   // Name of the Producer/Consumer (e.g. json, yaml, txt, bin)
	MediaType      string   // mime
	Implementation string   // func implementing the Producer/Consumer
	Parameters     []string // parameters supported by this serializer
}

GenSerializer represents a single serializer for a particular media type

type GenSerializers

type GenSerializers []GenSerializer

GenSerializers sorted representation of serializers

func (GenSerializers) Len

func (g GenSerializers) Len() int

func (GenSerializers) Less

func (g GenSerializers) Less(i, j int) bool

func (GenSerializers) Swap

func (g GenSerializers) Swap(i, j int)

type LanguageOpts

type LanguageOpts struct {
	ReservedWords  []string
	SystemModules  []string
	BaseImportFunc func(string) string            `json:"-"`
	ImportsFunc    func(map[string]string) string `json:"-"`
	// contains filtered or unexported fields
}

LanguageOpts to describe a language to the code generator

func KclLangOpts

func KclLangOpts() *LanguageOpts

KclLangOpts for rendering items as kcl code

func (*LanguageOpts) FormatContent

func (l *LanguageOpts) FormatContent(name string, content []byte) ([]byte, error)

FormatContent formats a file with a language specific formatter

func (*LanguageOpts) Init

func (l *LanguageOpts) Init()

Init the language option

func (*LanguageOpts) MangleFileName

func (l *LanguageOpts) MangleFileName(name string) string

MangleFileName makes sure a file name gets a safe name

func (*LanguageOpts) MangleModelName

func (l *LanguageOpts) MangleModelName(modelName string) string

MangleModelName adds "$" prefix to name if it is conflict with KCL keyword

func (*LanguageOpts) MangleName

func (l *LanguageOpts) MangleName(name, suffix string) string

MangleName makes sure a reserved word gets a safe name

func (*LanguageOpts) ManglePackageName

func (l *LanguageOpts) ManglePackageName(name, suffix string) string

ManglePackageName makes sure a package gets a safe name. In case of a file system path (e.g. name contains "/" or "\" on Windows), this return only the last element.

func (*LanguageOpts) ManglePackagePath

func (l *LanguageOpts) ManglePackagePath(name string, suffix string) string

ManglePackagePath makes sure a full package path gets a safe name. Only the last part of the path is altered.

func (*LanguageOpts) MangleVarName

func (l *LanguageOpts) MangleVarName(name string) string

MangleVarName makes sure a reserved word gets a safe name

func (*LanguageOpts) NonEmptyValue added in v0.5.2

func (l *LanguageOpts) NonEmptyValue(data interface{}) bool

NonEmptyValue checks if a value is non-empty

func (*LanguageOpts) ToKclValue

func (l *LanguageOpts) ToKclValue(data interface{}) string

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

Repository is the repository for the generator templates

func NewRepository

func NewRepository(funcs template.FuncMap) *Repository

NewRepository creates a new template repository with the provided functions defined

func (*Repository) AddFile

func (t *Repository) AddFile(name, data string) error

AddFile adds a file to the repository. It will create a new template based on the filename. It trims the .gotmpl from the end and converts the name using swag.ToJSONName. This will strip directory separators and Camelcase the next letter. e.g validation/primitive.gotmpl will become validationPrimitive

If the file contains a definition for a template that is protected the whole file will not be added

func (*Repository) DumpTemplates

func (t *Repository) DumpTemplates()

DumpTemplates prints out a dump of all the defined templates, where they are defined and what their dependencies are.

func (*Repository) Get

func (t *Repository) Get(name string) (*template.Template, error)

Get will return the named template from the repository, ensuring that all dependent templates are loaded. It will return an error if a dependent template is not defined in the repository.

func (*Repository) LoadDefaults

func (t *Repository) LoadDefaults()

LoadDefaults will load the embedded templates

func (*Repository) MustGet

func (t *Repository) MustGet(name string) *template.Template

MustGet a template by name, panics when fails

type SectionOpts

type SectionOpts struct {
	Models []TemplateOpts `mapstructure:"models"`
}

SectionOpts allows for specifying options to customize the templates used for generation

type TemplateOpts

type TemplateOpts struct {
	Name       string `mapstructure:"name"`
	Source     string `mapstructure:"source"`
	Target     string `mapstructure:"target"`
	FileName   string `mapstructure:"file_name"`
	SkipExists bool   `mapstructure:"skip_exists"`
	SkipFormat bool   `mapstructure:"skip_format"`
}

TemplateOpts allows for codegen customization

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL