models

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const DefaultPackageName = "openapi"

DefaultPackageName used in the models source code

View Source
const ExtensionPatternError = "x-pattern-error"

ExtensionPatternError is an extension property that, if set, allows the API author to control the error message for pattern validation failures.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConvertSpec added in v0.9.0

type ConvertSpec struct {
	// TargetGoType is the target type of the conversion
	TargetGoType string
}

ConvertSpec holds all info to build one As{Type}() function

type Discriminator added in v0.12.0

type Discriminator struct {
	// Field points to the property that specifies the data type name
	Field string
	// Map contains the mapping from Field values to target schemas
	Map map[string]string
}

type Generator added in v0.7.0

type Generator interface {
	// Generate generates and writes model files that can be generated out of the given spec.
	Generate(ctx context.Context) error
}

Generator defines generator operations needed for generating Go code for the spec

func NewGenerator added in v0.7.0

func NewGenerator(specFile io.Reader, opts Options) (Generator, error)

NewGenerator creates a standard generator implementation

type Model added in v0.2.0

type Model struct {
	// Name is a name of the generated type that follows the `type` keyword in the definition
	// For example, `type Name GoType`.
	Name string
	// Description is a description that will become a comment on the generated type
	Description string
	// Imports is a list of imports that will be present in the Go file
	Imports []string
	// Kind is a kind of generated model (e.g. `struct` or `enum`)
	Kind ModelKind
	// Properties is a list of type's property descriptors
	Properties []PropSpec
	// ConvertSpecs contains a list of convert functions for this model
	ConvertSpecs []ConvertSpec
	// Discriminator contains the optional oneOf discriminator
	Discriminator Discriminator
	// GoType is a string that represents the Go type that follows the model type name.
	// For example, `type Name GoType`.
	GoType string
	// SpecTitle is the spec title used in the auto-generated header
	SpecTitle string
	// SpecVersion is the spec version used in the auto-generated header
	SpecVersion string
	// PackageName is the name of the package used in the Go code
	PackageName string
	// AdditionalPropertiesGoType is the optional type of additional properties
	// that exist _in addition_ to `Properties`
	AdditionalPropertiesGoType string
}

Model is a template model for rendering Go code for a given API schema

func NewModelFromParameters added in v0.6.0

func NewModelFromParameters(params openapi3.Parameters) (model *Model, err error)

NewModelFromParameters returns a model built from operation parameters

func NewModelFromRef added in v0.2.0

func NewModelFromRef(ref *openapi3.SchemaRef) (model *Model, err error)

NewModelFromRef creates a model out of a schema

func (*Model) Render added in v0.2.0

func (m *Model) Render(ctx context.Context, writer io.Writer) error

Render renders the model to a Go file

type ModelKind added in v0.2.0

type ModelKind string

ModelKind is a kind of a Go model to render

const (
	// Struct is a regular Go struct
	Struct ModelKind = "struct"
	// Enum is a Go enum definition
	Enum ModelKind = "enum"
	// Value is a value type
	Value ModelKind = "value"
	// OneOf is a oneof value
	OneOf ModelKind = "oneof"
)

type Options

type Options struct {
	// PackageName of the generated models source code (`DefaultPackageName` by default)
	PackageName string
	// Destination is a path to a folder where create all the Go files
	Destination string
	// Logger instance to use for messaging
	Logger zerolog.Logger
}

Options represent all the possible options of the generator

type PropSpec added in v0.2.0

type PropSpec struct {
	// Name is a property name in structs, variable name in enums, etc
	Name string
	// PropertyName is the original name of the property
	PropertyName string
	// Description used in the comment of the property
	Description string
	// GoType used for this property (e.g. `string`, `int`, etc)
	GoType string
	// JSONTags is a string of JSON tags used for marshaling (e.g. `json:omitempty`)
	JSONTags string
	// Value is a value used for a enum item
	Value string
	// IsRequired is true when the property is a required value and should not be omitted on marshaling
	IsRequired bool
	// IsEnum is true when the property is a enum item
	IsEnum bool
	// IsOneOf is true when the property is a `oneof` schema
	IsOneOf bool

	// Validation stuff
	Pattern                    string
	PatternErrorMsg            string
	IsNullable                 bool
	NeedsValidation            bool
	IsRequiredInValidation     bool
	HasMin, HasMax             bool
	Min, Max                   float64
	HasMinLength, HasMaxLength bool
	MinLength, MaxLength       uint64
	HasFormat                  bool
	IsDate                     bool
	IsDateTime                 bool
	IsBase64                   bool
	IsEmail                    bool
	IsUUID                     bool
	IsURL                      bool
	IsURI                      bool
	IsRequestURI               bool
	IsHostname                 bool
	IsIP                       bool
	IsIPv4                     bool
	IsIPv6                     bool
}

PropSpec is a Go property descriptor

Jump to

Keyboard shortcuts

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