gen

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: Apache-2.0 Imports: 32 Imported by: 7

Documentation

Overview

Package gen contains the code generator for OpenAPI Spec.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateSchema added in v0.21.0

func GenerateSchema(schema *jsonschema.Schema, fs FileSystem, opts GenerateSchemaOptions) error

GenerateSchema generates type, validation and JSON encoding for given schema.

func TypeFormatMapping added in v0.15.0

func TypeFormatMapping() map[jsonschema.SchemaType]map[string]ir.PrimitiveType

Types

type ContentTypeAliases added in v0.44.0

type ContentTypeAliases map[string]ir.Encoding

ContentTypeAliases maps content type to concrete ogen encoding.

func (*ContentTypeAliases) Set added in v0.44.0

func (m *ContentTypeAliases) Set(value string) error

Set implements flag.Value.

func (ContentTypeAliases) String added in v0.44.0

func (m ContentTypeAliases) String() string

String implements fmt.Stringer.

type DefaultElem added in v0.15.0

type DefaultElem struct {
	// Type is type of this DefaultElem.
	Type *ir.Type
	// Var is decoding/encoding variable Go name (obj) or selector (obj.Field).
	Var string
	// Default is default value to set.
	Default ir.Default
}

DefaultElem is variable helper for setting default values.

type Elem

type Elem struct {
	// Sub whether this Elem has parent Elem.
	Sub bool
	// Type is type of this Elem.
	Type *ir.Type
	// Var is decoding/encoding variable Go name (obj) or selector (obj.Field).
	Var string
	// Tag contains info about field tags, if any.
	Tag ir.Tag
	// First whether this field is first.
	First bool
}

Elem is variable helper for recursive array or object encoding or decoding.

func (Elem) NextVar

func (e Elem) NextVar() string

NextVar returns name of variable for decoding recursive call.

Needed to make variable names unique.

type ErrBuildRouter added in v0.16.0

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

ErrBuildRouter reports that route tree building failed.

func (*ErrBuildRouter) Error added in v0.16.0

func (e *ErrBuildRouter) Error() string

Error implements error.

func (*ErrBuildRouter) Format added in v0.42.0

func (e *ErrBuildRouter) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*ErrBuildRouter) FormatError added in v0.42.0

func (e *ErrBuildRouter) FormatError(p errors.Printer) (next error)

FormatError implements errors.Formatter.

func (*ErrBuildRouter) Unwrap added in v0.16.0

func (e *ErrBuildRouter) Unwrap() error

Unwrap implements errors.Wrapper.

type ErrGoFormat added in v0.41.0

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

ErrGoFormat reports that generated code formatting failed.

func (*ErrGoFormat) Error added in v0.41.0

func (e *ErrGoFormat) Error() string

Error implements error.

func (*ErrGoFormat) Format added in v0.42.0

func (e *ErrGoFormat) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*ErrGoFormat) FormatError added in v0.42.0

func (e *ErrGoFormat) FormatError(p errors.Printer) (next error)

FormatError implements errors.Formatter.

func (*ErrGoFormat) Unwrap added in v0.41.0

func (e *ErrGoFormat) Unwrap() error

Unwrap implements errors.Wrapper.

type ErrNotImplemented

type ErrNotImplemented struct {
	Name string
}

ErrNotImplemented reports that feature is not implemented.

func (*ErrNotImplemented) Error

func (e *ErrNotImplemented) Error() string

Error implements error.

type ErrParseSpec added in v0.16.0

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

ErrParseSpec reports that specification parsing failed.

func (*ErrParseSpec) Error added in v0.16.0

func (e *ErrParseSpec) Error() string

Error implements error.

func (*ErrParseSpec) Format added in v0.42.0

func (e *ErrParseSpec) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*ErrParseSpec) FormatError added in v0.42.0

func (e *ErrParseSpec) FormatError(p errors.Printer) (next error)

FormatError implements errors.Formatter.

func (*ErrParseSpec) Unwrap added in v0.16.0

func (e *ErrParseSpec) Unwrap() error

Unwrap implements errors.Wrapper.

type ErrUnsupportedContentTypes

type ErrUnsupportedContentTypes struct {
	ContentTypes []string
}

ErrUnsupportedContentTypes reports that ogen does not support such content-type(s).

func (*ErrUnsupportedContentTypes) Error

Error implements error.

type FileSystem

type FileSystem interface {
	WriteFile(baseName string, source []byte) error
}

FileSystem represents a directory of generated package.

type Filters added in v0.15.0

type Filters struct {
	PathRegex *regexp.Regexp
	Methods   []string
}

Filters contains filters to skip operations.

type GenerateSchemaOptions added in v0.29.0

type GenerateSchemaOptions struct {
	// TypeName is root schema type name. Defaults to "Type".
	TypeName string
	// FileName is output filename. Defaults to "output.gen.go".
	FileName string
	// PkgName is the package name. Defaults to GOPACKAGE environment variable, if any. Otherwise, to "output".
	PkgName string
	// TrimPrefix is a ref name prefixes to trim. Defaults to []string{"#/definitions/", "#/$defs/"}.
	TrimPrefix []string
	// Logger to use.
	Logger *zap.Logger
}

GenerateSchemaOptions is options structure for GenerateSchema.

type Generator

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

Generator is OpenAPI-to-Go generator.

func NewGenerator

func NewGenerator(spec *ogen.Spec, opts Options) (*Generator, error)

NewGenerator creates new Generator.

func (*Generator) API added in v0.27.0

func (g *Generator) API() *openapi.API

API returns api schema.

func (*Generator) Types added in v0.27.0

func (g *Generator) Types() map[string]*ir.Type

Types returns generated types.

func (*Generator) WriteSource

func (g *Generator) WriteSource(fs FileSystem, pkgName string) error

WriteSource writes generated definitions to fs.

type Options

type Options struct {
	// NoClient disables client generation.
	NoClient bool
	// NoServer disables server generation.
	NoServer bool
	// GenerateExampleTests whether to generate decoding tests using schema examples.
	GenerateExampleTests bool
	// SkipTestRegex is regex to skip generated tests.
	SkipTestRegex *regexp.Regexp
	// SkipUnimplemented disables generation of unimplemented Handler, like UnimplementedServer generated by gRPC.
	SkipUnimplemented bool
	// InferSchemaType enables type inference for schemas. Schema parser will try to detect schema type
	// by its properties.
	InferSchemaType bool

	// AllowRemote enables remote references resolving.
	//
	// See https://github.com/ogen-go/ogen/issues/385.
	AllowRemote bool
	// Remote is remote reference resolver options.
	Remote RemoteOptions

	// Filters contains filters to skip operations.
	Filters Filters
	// IgnoreNotImplemented contains ErrNotImplemented messages to ignore.
	IgnoreNotImplemented []string
	// NotImplementedHook is hook for ErrNotImplemented errors.
	NotImplementedHook func(name string, err error)

	// ContentTypeAliases contains content type aliases.
	ContentTypeAliases ContentTypeAliases

	// Filename is a name of the spec file.
	//
	// Used for error messages.
	Filename string
	// Logger to use.
	Logger *zap.Logger
}

Options is Generator options.

type RemoteOptions added in v0.36.0

type RemoteOptions struct {
	// HTTPClient sets http client to use. Defaults to http.DefaultClient.
	HTTPClient *http.Client
	// ReadFile sets function for reading files from fs. Defaults to os.ReadFile.
	ReadFile func(p string) ([]byte, error)
}

RemoteOptions is remote reference resolver options.

type ResponseElem

type ResponseElem struct {
	Response *ir.Response
	Ptr      bool
}

type Route

type Route struct {
	Method    string        // GET, POST, DELETE
	Path      string        // /api/v1/user/{name}/info
	Operation *ir.Operation // getUserInfo
}

Route describes route.

type RouteNode added in v0.7.0

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

RouteNode is node of Radix tree of routes.

func (*RouteNode) AddRoute added in v0.46.0

func (n *RouteNode) AddRoute(nr Route) error

AddRoute adds new method route to node.

func (*RouteNode) AllowedMethods added in v0.46.0

func (n *RouteNode) AllowedMethods() string

AllowedMethods returns list of allowed methods.

func (*RouteNode) Children added in v0.7.0

func (n *RouteNode) Children() []*RouteNode

Children returns child nodes.

func (*RouteNode) Head added in v0.7.0

func (n *RouteNode) Head() byte

Head returns first byte of prefix.

func (*RouteNode) IsLeaf added in v0.7.0

func (n *RouteNode) IsLeaf() bool

IsLeaf whether node has no children.

func (*RouteNode) IsParam added in v0.7.0

func (n *RouteNode) IsParam() bool

IsParam whether node is a parameter node.

func (*RouteNode) IsStatic added in v0.7.0

func (n *RouteNode) IsStatic() bool

IsStatic whether node is not a parameter node.

func (*RouteNode) Param added in v0.7.0

func (n *RouteNode) Param() *ir.Parameter

Param returns associated parameter, if any.

May be nil.

func (*RouteNode) ParamChildren added in v0.7.0

func (n *RouteNode) ParamChildren() (r []*RouteNode)

ParamChildren returns slice of child parameter nodes.

func (*RouteNode) ParamName added in v0.7.0

func (n *RouteNode) ParamName() string

ParamName returns parameter name, if any.

func (*RouteNode) Prefix added in v0.7.0

func (n *RouteNode) Prefix() string

Prefix returns common prefix.

func (*RouteNode) Routes added in v0.46.0

func (n *RouteNode) Routes() []Route

Routes returns list of associated MethodRoute.

func (*RouteNode) StaticChildren added in v0.7.0

func (n *RouteNode) StaticChildren() (r []*RouteNode)

StaticChildren returns slice of child static nodes.

func (*RouteNode) Tails added in v0.7.0

func (n *RouteNode) Tails() (r []byte)

Tails returns heads of child nodes.

Used for matching end of parameter node between two static.

type RouteTree added in v0.7.0

type RouteTree struct {
	Root *RouteNode
}

RouteTree is Radix tree of routes.

func (*RouteTree) Walk added in v0.7.0

func (t *RouteTree) Walk(cb func(level int, n *RouteNode))

type Router

type Router struct {
	Tree RouteTree
	// MaxParametersCount is maximum number of path parameters in one operation.
	MaxParametersCount int
}

Router contains list of routes.

func (*Router) Add added in v0.7.0

func (s *Router) Add(r Route) error

Add adds new route.

type RouterElem added in v0.8.0

type RouterElem struct {
	// ParameterIndex is index of parameter of this route part.
	ParameterIndex int
	Route          *RouteNode
}

RouterElem is variable helper for router generation.

type Routes added in v0.46.0

type Routes []Route

Routes is list of routes.

func (*Routes) AddRoute added in v0.46.0

func (n *Routes) AddRoute(nr Route) error

AddRoute adds new route. If the route is already added, it returns error.

func (Routes) Len added in v0.46.0

func (n Routes) Len() int

Len implements sort.Interface.

func (Routes) Less added in v0.46.0

func (n Routes) Less(i, j int) bool

Less implements sort.Interface.

func (Routes) Swap added in v0.46.0

func (n Routes) Swap(i, j int)

Swap implements sort.Interface.

type TemplateConfig

type TemplateConfig struct {
	Package       string
	Operations    []*ir.Operation
	Types         map[string]*ir.Type
	Interfaces    map[string]*ir.Type
	Error         *ir.Response
	ErrorType     *ir.Type
	Securities    map[string]*ir.Security
	Router        Router
	ClientEnabled bool
	ServerEnabled bool
	// contains filtered or unexported fields
}

func (TemplateConfig) RatStrings added in v0.16.0

func (t TemplateConfig) RatStrings() []string

RatStrings returns slice of all unique big.Rat (multipleOf validation).

func (TemplateConfig) RegexStrings added in v0.5.0

func (t TemplateConfig) RegexStrings() []string

RegexStrings returns slice of all unique regex validators.

func (TemplateConfig) SkipTest added in v0.10.1

func (t TemplateConfig) SkipTest(typ *ir.Type) bool

SkipTest returns true, if test should be skipped.

Directories

Path Synopsis
Package genfs contains gen.FileSystem implementations.
Package genfs contains gen.FileSystem implementations.
Package ir contains definitions for the intermediate representation of OpenAPI objects and generated Go types.
Package ir contains definitions for the intermediate representation of OpenAPI objects and generated Go types.

Jump to

Keyboard shortcuts

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