Documentation
¶
Overview ¶
Package gen contains the code generator for OpenAPI Spec.
Index ¶
- Variables
- func GenerateSchema(schema *jsonschema.Schema, fs FileSystem, opts GenerateSchemaOptions) (rerr error)
- func TypeFormatMapping() map[jsonschema.SchemaType]map[string]ir.PrimitiveType
- type ContentTypeAliases
- type ConvenientErrors
- type DefaultElem
- type Elem
- type ErrBuildRouter
- type ErrGoFormat
- type ErrNotImplemented
- type ErrParseSpec
- type ErrUnsupportedContentTypes
- type Feature
- type FeatureOptions
- type FeatureSet
- type FileSystem
- type Filters
- type GenerateOptions
- type GenerateSchemaOptions
- type Generator
- type OperationElem
- type Options
- type ParseOptions
- type RemoteOptions
- type ResponseElem
- type Route
- type RouteNode
- func (n *RouteNode) AddRoute(nr Route) error
- func (n *RouteNode) AllowedMethods() string
- func (n *RouteNode) Children() []*RouteNode
- func (n *RouteNode) Head() byte
- func (n *RouteNode) IsLeaf() bool
- func (n *RouteNode) IsParam() bool
- func (n *RouteNode) IsStatic() bool
- func (n *RouteNode) Param() *ir.Parameter
- func (n *RouteNode) ParamChildren() (r []*RouteNode)
- func (n *RouteNode) ParamName() string
- func (n *RouteNode) Prefix() string
- func (n *RouteNode) Routes() []Route
- func (n *RouteNode) StaticChildren() (r []*RouteNode)
- func (n *RouteNode) Tails() (r []byte)
- type RouteTree
- type Router
- type RouterElem
- type Routes
- type TemplateConfig
- func (t TemplateConfig) AnyClientEnabled() bool
- func (t TemplateConfig) AnyInstrumentable() bool
- func (t TemplateConfig) AnyServerEnabled() bool
- func (t TemplateConfig) ErrorGoType() string
- func (t TemplateConfig) RatStrings() []string
- func (t TemplateConfig) RegexStrings() []string
- func (t TemplateConfig) SkipTest(typ *ir.Type) bool
- type WebhookRoute
- type WebhookRouter
- type WebhookRoutes
Constants ¶
This section is empty.
Variables ¶
var ( PathsClient = Feature{ "paths/client", `Enables paths client generation`, } PathsServer = Feature{ "paths/server", `Enables paths server generation`, } WebhooksClient = Feature{ "webhooks/client", `Enables webhooks client generation`, } WebhooksServer = Feature{ "webhooks/server", `Enables webhooks server generation`, } ClientRequestValidation = Feature{ "client/request/validation", `Enables validation of client requests`, } ServerResponseValidation = Feature{ "server/response/validation", `Enables validation of server responses`, } OgenOtel = Feature{ "ogen/otel", `Enables OpenTelemetry integration`, } OgenUnimplemented = Feature{ "ogen/unimplemented", `Enables stub Handler generation`, } DebugExampleTests = Feature{ "debug/example_tests", `Enables example tests generation`, } )
var AllFeatures = []Feature{ PathsClient, PathsServer, WebhooksClient, WebhooksServer, ClientRequestValidation, ServerResponseValidation, OgenOtel, OgenUnimplemented, DebugExampleTests, }
AllFeatures contains all ogen features.
var DefaultFeatures = []Feature{ PathsClient, PathsServer, WebhooksClient, WebhooksServer, OgenOtel, OgenUnimplemented, }
DefaultFeatures defines default ogen features.
Functions ¶
func GenerateSchema ¶
func GenerateSchema(schema *jsonschema.Schema, fs FileSystem, opts GenerateSchemaOptions) (rerr error)
GenerateSchema generates type, validation and JSON encoding for given schema.
func TypeFormatMapping ¶
func TypeFormatMapping() map[jsonschema.SchemaType]map[string]ir.PrimitiveType
Types ¶
type ContentTypeAliases ¶
ContentTypeAliases maps content type to concrete ogen encoding.
func (*ContentTypeAliases) Set ¶
func (m *ContentTypeAliases) Set(value string) error
Set implements flag.Value.
func (ContentTypeAliases) String ¶
func (m ContentTypeAliases) String() string
String implements fmt.Stringer.
type ConvenientErrors ¶
type ConvenientErrors int
ConvenientErrors is an option type to control `Convenient Errors` feature.
func (*ConvenientErrors) IsBoolFlag ¶
func (c *ConvenientErrors) IsBoolFlag() bool
IsBoolFlag implements flag.boolFlag.
func (ConvenientErrors) IsDisabled ¶
func (c ConvenientErrors) IsDisabled() bool
IsDisabled whether Convenient Errors is disabled.
func (ConvenientErrors) IsForced ¶
func (c ConvenientErrors) IsForced() bool
IsForced whether Convenient Errors is forced.
func (*ConvenientErrors) Set ¶
func (c *ConvenientErrors) Set(s string) error
Set implements flag.Value.
func (ConvenientErrors) String ¶
func (c ConvenientErrors) String() string
String implements fmt.Stringer.
func (*ConvenientErrors) UnmarshalYAML ¶
func (c *ConvenientErrors) UnmarshalYAML(n *yaml.Node) error
UnmarshalYAML implements yaml.Unmarshaler.
type DefaultElem ¶
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.
type ErrBuildRouter ¶
type ErrBuildRouter struct {
// contains filtered or unexported fields
}
ErrBuildRouter reports that route tree building failed.
func (*ErrBuildRouter) Format ¶
func (e *ErrBuildRouter) Format(s fmt.State, verb rune)
Format implements fmt.Formatter.
func (*ErrBuildRouter) FormatError ¶
func (e *ErrBuildRouter) FormatError(p errors.Printer) (next error)
FormatError implements errors.Formatter.
func (*ErrBuildRouter) Unwrap ¶
func (e *ErrBuildRouter) Unwrap() error
Unwrap implements errors.Wrapper.
type ErrGoFormat ¶
type ErrGoFormat struct {
// contains filtered or unexported fields
}
ErrGoFormat reports that generated code formatting failed.
func (*ErrGoFormat) Format ¶
func (e *ErrGoFormat) Format(s fmt.State, verb rune)
Format implements fmt.Formatter.
func (*ErrGoFormat) FormatError ¶
func (e *ErrGoFormat) FormatError(p errors.Printer) (next error)
FormatError implements errors.Formatter.
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 ¶
type ErrParseSpec struct {
// contains filtered or unexported fields
}
ErrParseSpec reports that specification parsing failed.
func (*ErrParseSpec) Format ¶
func (e *ErrParseSpec) Format(s fmt.State, verb rune)
Format implements fmt.Formatter.
func (*ErrParseSpec) FormatError ¶
func (e *ErrParseSpec) FormatError(p errors.Printer) (next error)
FormatError implements errors.Formatter.
func (*ErrParseSpec) Unwrap ¶
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 ¶
func (e *ErrUnsupportedContentTypes) Error() string
Error implements error.
type FeatureOptions ¶
type FeatureOptions struct {
Enable FeatureSet `json:"enable" yaml:"enable"`
Disable FeatureSet `json:"disable" yaml:"disable"`
DisableAll bool `json:"disable_all" yaml:"disable_all"`
}
FeatureOptions is features Options.
func (*FeatureOptions) Build ¶
func (cfg *FeatureOptions) Build() (set FeatureSet, _ error)
Build returns final set.
type FeatureSet ¶
type FeatureSet map[string]struct{}
FeatureSet is set of Feature names.
func (*FeatureSet) Disable ¶
func (s *FeatureSet) Disable(name string)
Disable removes a feature from set.
func (*FeatureSet) Enable ¶
func (s *FeatureSet) Enable(name string) error
Enable adds a feature to set.
func (FeatureSet) Has ¶
func (s FeatureSet) Has(feature Feature) bool
Has whether if set has given feature.
func (*FeatureSet) UnmarshalYAML ¶
func (s *FeatureSet) UnmarshalYAML(n *yaml.Node) error
UnmarshalYAML implements yaml.Unmarshaler.
type FileSystem ¶
FileSystem represents a directory of generated package.
type Filters ¶
Filters contains filters to skip operations.
func (*Filters) UnmarshalYAML ¶
UnmarshalYAML implements yaml.Unmarshaler.
type GenerateOptions ¶
type GenerateOptions struct {
// OgenModule is the module path of ogen, used for the runtime dependencies.
OgenModule string `json:"ogen_module" yaml:"ogen_module"`
// Features sets generator features.
Features *FeatureOptions `json:"features" yaml:"features"`
// Filters contains filters to skip operations.
Filters Filters `json:"filters" yaml:"filters"`
// IgnoreNotImplemented contains ErrNotImplemented messages to ignore.
IgnoreNotImplemented []string `json:"ignore_not_implemented" yaml:"ignore_not_implemented"`
// NotImplementedHook is hook for ErrNotImplemented errors.
NotImplementedHook func(name string, err error) `json:"-" yaml:"-"`
// ConvenientErrors control Convenient Errors feature.
//
// Default value is `auto` (0), NewError handler will be generated if possible.
//
// If value > 0 forces feature. An error will be returned if generator is unable to find common error pattern.
//
// If value < 0 disables feature entirely.
ConvenientErrors ConvenientErrors `json:"convenient_errors" yaml:"convenient_errors"`
// ContentTypeAliases contains content type aliases.
ContentTypeAliases ContentTypeAliases `json:"content_type_aliases" yaml:"content_type_aliases"`
}
GenerateOptions sets generator options.
type GenerateSchemaOptions ¶
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
// OgenModuleName is the ogen module named used in generated source. Defaults to "github.com/ogen-go/ogen".
OgenModuleName 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 ¶
NewGenerator creates new Generator.
func (*Generator) Operations ¶
Operations returns generated operations.
func (*Generator) WriteSource ¶
func (g *Generator) WriteSource(fs FileSystem, pkgName string) error
WriteSource writes generated definitions to fs.
type OperationElem ¶
type OperationElem struct {
// Operation is the operation.
Operation *ir.Operation
// Config is the template configuration.
Config TemplateConfig
}
OperationElem is variable name for generating per-operation functions.
type Options ¶
type Options struct {
// Parser sets parser options.
Parser ParseOptions `json:"parser" yaml:"parser"`
// Generator sets generator options.
Generator GenerateOptions `json:"generator" yaml:"generator"`
// ExpandSpec is a path to expanded spec.
ExpandSpec string `json:"expand" yaml:"expand"`
// Logger to use.
Logger *zap.Logger `json:"-" yaml:"-"`
}
Options is Generator options.
func (*Options) SetLocation ¶
func (o *Options) SetLocation(p string, opts RemoteOptions) ([]byte, error)
SetLocation sets File, RootURL and RemoteOptions using given path or URL and returns file data.
type ParseOptions ¶
type ParseOptions struct {
// InferSchemaType enables type inference for schemas. Schema parser will try to detect schema type
// by its properties.
InferSchemaType bool `json:"infer_types" yaml:"infer_types"`
// AllowRemote enables remote references resolving.
//
// See https://github.com/ogen-go/ogen/issues/385.
AllowRemote bool `json:"allow_remote" yaml:"allow_remote"`
// RootURL is root URL for remote references resolving.
RootURL *url.URL `json:"-" yaml:"-"`
// Remote is remote reference resolver options.
Remote RemoteOptions `json:"-" yaml:"-"`
// SchemaDepthLimit is maximum depth of schema generation. Default is 1000.
SchemaDepthLimit int `json:"depth_limit" yaml:"depth_limit"`
// File is the file that is being parsed.
//
// Used for error messages.
File location.File `json:"-" yaml:"-"`
}
ParseOptions sets parsing options.
func (*ParseOptions) SetLocation ¶
func (o *ParseOptions) SetLocation(p string, opts RemoteOptions) ([]byte, error)
SetLocation sets File, RootURL and RemoteOptions using given path or URL and returns file data.
type RemoteOptions ¶
type RemoteOptions = jsonschema.ExternalOptions
RemoteOptions is remote reference resolver options.
type ResponseElem ¶
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 ¶
type RouteNode struct {
// contains filtered or unexported fields
}
RouteNode is node of Radix tree of routes.
func (*RouteNode) AllowedMethods ¶
AllowedMethods returns list of allowed methods.
func (*RouteNode) ParamChildren ¶
ParamChildren returns slice of child parameter nodes.
func (*RouteNode) StaticChildren ¶
StaticChildren returns slice of child static nodes.
type Router ¶
type Router struct {
Tree RouteTree
// MaxParametersCount is maximum number of path parameters in one operation.
MaxParametersCount int
}
Router contains list of routes.
type RouterElem ¶
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 ¶
type Routes []Route
Routes is list of routes.
type TemplateConfig ¶
type TemplateConfig struct {
Package string
OgenModule string
Operations []*ir.Operation
Webhooks []*ir.Operation
Types map[string]*ir.Type
Interfaces map[string]*ir.Type
Error *ir.Response
ErrorType *ir.Type
Servers ir.Servers
Securities map[string]*ir.Security
Router Router
WebhookRouter WebhookRouter
PathsClientEnabled bool
PathsServerEnabled bool
WebhookClientEnabled bool
WebhookServerEnabled bool
OpenTelemetryEnabled bool
RequestValidationEnabled bool
ResponseValidationEnabled bool
// contains filtered or unexported fields
}
func (TemplateConfig) AnyClientEnabled ¶
func (t TemplateConfig) AnyClientEnabled() bool
AnyClientEnabled returns true, if webhooks or paths client is enabled.
func (TemplateConfig) AnyInstrumentable ¶
func (t TemplateConfig) AnyInstrumentable() bool
AnyInstrumentable returns true, if OpenTelemetry integration enabled and there is client/server to instrument.
func (TemplateConfig) AnyServerEnabled ¶
func (t TemplateConfig) AnyServerEnabled() bool
AnyServerEnabled returns true, if webhooks or paths server is enabled.
func (TemplateConfig) ErrorGoType ¶
func (t TemplateConfig) ErrorGoType() string
ErrorGoType returns Go type of error.
func (TemplateConfig) RatStrings ¶
func (t TemplateConfig) RatStrings() []string
RatStrings returns slice of all unique big.Rat (multipleOf validation).
func (TemplateConfig) RegexStrings ¶
func (t TemplateConfig) RegexStrings() []string
RegexStrings returns slice of all unique regex validators.
type WebhookRoute ¶
WebhookRoute is a webhook route.
type WebhookRouter ¶
type WebhookRouter struct {
Webhooks map[string]WebhookRoutes
}
WebhookRouter contains routing information for webhooks.
func (*WebhookRouter) Add ¶
func (r *WebhookRouter) Add(name string, nr WebhookRoute) error
Add adds new route.
type WebhookRoutes ¶
type WebhookRoutes struct {
Routes []WebhookRoute
}
WebhookRoutes is a list of webhook methods.
func (*WebhookRoutes) Add ¶
func (r *WebhookRoutes) Add(nr WebhookRoute) error
Add adds new operation to the route.
func (WebhookRoutes) AllowedMethods ¶
func (r WebhookRoutes) AllowedMethods() string
AllowedMethods returns comma-separated list of allowed methods.
Source Files
¶
- errors.go
- features.go
- fix.go
- gen.go
- gen_contents.go
- gen_headers.go
- gen_operation.go
- gen_parameters.go
- gen_request_body.go
- gen_responses.go
- gen_schema.go
- gen_security.go
- gen_server.go
- genctx.go
- generator.go
- generics.go
- names.go
- options.go
- reduce.go
- route_node.go
- route_tree.go
- router.go
- schema_gen.go
- schema_gen_primitive.go
- schema_gen_sum.go
- templates.go
- tstorage.go
- utils.go
- walk.go
- write.go
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. |