golang

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package golang provides the Go code generator; the name is golang because go is a keyword.

Index

Constants

This section is empty.

Variables

View Source
var RuntimeNames = map[string]bool{
	"BasePath": true, "Doer": true, "RequestEditorFn": true, "Configuration": true,
	"ResponseError": true, "Opt": true, "Some": true, "Null": true, "Ptr": true,
	"Date": true, "DateTime": true, "File": true, "ServerSentEvent": true,
}

RuntimeNames are the identifiers runtime.go declares; a model taking one is renamed with a Model suffix.

View Source
var TypeMapping = map[string]string{
	"array":   "Array",
	"boolean": "boolean",
	"integer": "number",
	"number":  "number",
	"object":  "any",
	"string":  "string",
	"AnyType": "any",
}

TypeMapping maps OpenAPI types to the TypeScript-shaped DataType strings the parser builds on (Array<X>, any, number). Go types are derived from a property's flags and tree instead, see GoType.

Functions

func EnumBaseType

func EnumBaseType(allowable map[string]any, isBoolean bool) string

EnumBaseType is the Go type an enum's values have: string when they are strings (isString on the enumVars), else bool, int when every value is whole, float64 otherwise; any when they are of more than one JSON type (pydantic's Literal["auto", 0]), which no Go constant type holds. A null among them makes the enum nullable, not mixed.

func GoParamType

func GoParamType(p *codegen.CodegenParameter, named string) string

GoParamType is the Go type of a parameter; a file is a File. named is the type declared for an inline object, as for GoType.

func GoType

func GoType(p *codegen.CodegenProperty, named string) string

GoType is the Go type of a property, from its flags and its Items tree. named is the type declared for an inline enum or object at its leaf, or the enum or union model a $ref names; "" types an enum by its values, an object as a map and a union as raw JSON.

func Snake

func Snake(s string) string

Snake converts s to snake_case, keeping an initialism one word.

Types

type BaseGenerator

type BaseGenerator struct {
	Config *config.GeneratorConfig

	TypeMapping   map[string]string
	ReservedWords map[string]bool

	ModelPackage string
	ApiPackage   string

	ApiTemplateFiles     map[string]string
	ModelTemplateFiles   map[string]string
	SupportingFiles      []generator.SupportingFile
	AdditionalProperties map[string]any
}

BaseGenerator holds what a Go generator shares: the CodegenConfig plumbing and Go naming.

func NewBaseGenerator

func NewBaseGenerator() *BaseGenerator

NewBaseGenerator creates a new Go base generator.

func (*BaseGenerator) EscapeReservedWord

func (g *BaseGenerator) EscapeReservedWord(name string) string

EscapeReservedWord suffixes a reserved name with Model.

func (*BaseGenerator) FromModel

func (g *BaseGenerator) FromModel(name string, _ any) *codegen.CodegenModel

FromModel is unused: the parser builds models.

func (*BaseGenerator) FromOperation

func (g *BaseGenerator) FromOperation(path, httpMethod string, _ any) *codegen.CodegenOperation

FromOperation is unused: the parser builds operations.

func (*BaseGenerator) FromParameter

func (g *BaseGenerator) FromParameter(_ any) *codegen.CodegenParameter

FromParameter is unused: the parser builds parameters.

func (*BaseGenerator) FromProperty

func (g *BaseGenerator) FromProperty(name string, _ any, required bool) *codegen.CodegenProperty

FromProperty is unused: the parser builds properties.

func (*BaseGenerator) FromResponse

func (g *BaseGenerator) FromResponse(code string, _ any) *codegen.CodegenResponse

FromResponse is unused: the parser builds responses.

func (*BaseGenerator) FromSecurityScheme

func (g *BaseGenerator) FromSecurityScheme(name string, _ any) *codegen.CodegenSecurity

FromSecurityScheme is unused: the parser builds security schemes.

func (*BaseGenerator) GetAdditionalProperties

func (g *BaseGenerator) GetAdditionalProperties() map[string]any

GetAdditionalProperties returns additional properties for templates.

func (*BaseGenerator) GetApiTemplateFiles

func (g *BaseGenerator) GetApiTemplateFiles() map[string]string

GetApiTemplateFiles returns API template files.

func (*BaseGenerator) GetConfig

func (g *BaseGenerator) GetConfig() *config.GeneratorConfig

GetConfig returns the generator config.

func (*BaseGenerator) GetImportMapping

func (g *BaseGenerator) GetImportMapping() map[string]string

GetImportMapping returns the import mapping.

func (*BaseGenerator) GetLanguageSpecificPrimitives

func (g *BaseGenerator) GetLanguageSpecificPrimitives() map[string]bool

GetLanguageSpecificPrimitives returns the primitive types.

func (*BaseGenerator) GetModelTemplateFiles

func (g *BaseGenerator) GetModelTemplateFiles() map[string]string

GetModelTemplateFiles returns model template files.

func (*BaseGenerator) GetReservedWords

func (g *BaseGenerator) GetReservedWords() map[string]bool

GetReservedWords returns the reserved words.

func (*BaseGenerator) GetSchemaType

func (g *BaseGenerator) GetSchemaType(schemaType, _ string) string

GetSchemaType returns the parser's DataType string for an OpenAPI type.

func (*BaseGenerator) GetSupportingFiles

func (g *BaseGenerator) GetSupportingFiles() []generator.SupportingFile

GetSupportingFiles returns supporting files.

func (*BaseGenerator) GetTypeDeclaration

func (g *BaseGenerator) GetTypeDeclaration(schemaType, format string) string

GetTypeDeclaration returns the parser's DataType string for an OpenAPI type.

func (*BaseGenerator) GetTypeMapping

func (g *BaseGenerator) GetTypeMapping() map[string]string

GetTypeMapping returns the type mapping.

func (*BaseGenerator) IsReservedWord

func (g *BaseGenerator) IsReservedWord(word string) bool

IsReservedWord reports whether word is a name runtime.go declares.

func (*BaseGenerator) SanitizeOperationId

func (g *BaseGenerator) SanitizeOperationId(operationId string) string

SanitizeOperationId converts an operation ID to an exported method name.

func (*BaseGenerator) SetConfig

func (g *BaseGenerator) SetConfig(cfg *config.GeneratorConfig)

SetConfig sets the generator config.

func (*BaseGenerator) ToApiFilename

func (g *BaseGenerator) ToApiFilename(name string) string

ToApiFilename returns the file name of an API type, api_pet for PetAPI.

func (*BaseGenerator) ToApiName

func (g *BaseGenerator) ToApiName(name string) string

ToApiName converts a tag to the API type name, PetAPI.

func (*BaseGenerator) ToModelFilename

func (g *BaseGenerator) ToModelFilename(name string) string

ToModelFilename returns the file name of a model, model_pet.

func (*BaseGenerator) ToModelName

func (g *BaseGenerator) ToModelName(name string) string

ToModelName converts a schema name to an exported Go type name.

func (*BaseGenerator) ToParamName

func (g *BaseGenerator) ToParamName(name string) string

ToParamName converts a parameter name to an exported field name.

func (*BaseGenerator) ToVarName

func (g *BaseGenerator) ToVarName(name string) string

ToVarName converts a property or parameter name to an exported field name.

type FetchGenerator

type FetchGenerator struct {
	*BaseGenerator

	GoConfig *config.GoFetchConfig
	// contains filtered or unexported fields
}

FetchGenerator implements the go-fetch generator: a client in the shape of typescript-fetch (an API type per tag, a Request, Raw and typed method per operation) that imports only the standard library.

Templates read what they render from vendorExtensions keys x-go-*, which PostProcessModels and PostProcessOperations fill in, and the api template also from goDeclareParams, which the pipeline sets.

func NewFetchGenerator

func NewFetchGenerator() *FetchGenerator

NewFetchGenerator creates a new go-fetch generator.

func (*FetchGenerator) GetHelp

func (g *FetchGenerator) GetHelp() string

GetHelp returns the help text.

func (*FetchGenerator) GetName

func (g *FetchGenerator) GetName() string

GetName returns the generator name.

func (*FetchGenerator) GetTag

GetTag returns the generator type.

func (*FetchGenerator) PostProcessModels

func (g *FetchGenerator) PostProcessModels(models []*codegen.CodegenModel) []*codegen.CodegenModel

PostProcessModels types every model for Go: struct fields with allOf flattened, enums, aliases and union wrappers.

func (*FetchGenerator) PostProcessOperations

func (g *FetchGenerator) PostProcessOperations(ops []*codegen.CodegenOperation) []*codegen.CodegenOperation

PostProcessOperations names each operation's methods and Params type and types its parameters, body, responses and auth for the api template.

func (*FetchGenerator) ProcessOpts

func (g *FetchGenerator) ProcessOpts() error

ProcessOpts processes options and initializes the generator.

Jump to

Keyboard shortcuts

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