models

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 24 Imported by: 0

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

View Source
var DerivedRulesByValidationType = load()

Functions

This section is empty.

Types

type ConvertSpec

type ConvertSpec struct {
	// TargetGoType is the target type of the conversion
	TargetGoType string
	// IsPtr is true when the target type is a pointer
	IsPtr bool
}

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

type DerivedRule added in v2.1.1

type DerivedRule struct {
	//Type related
	IsStruct       bool  `json:"struct,omitempty"`
	IsArray        bool  `json:"array,omitempty"`
	IsMap          bool  `json:"map,omitempty"`
	IsNumber       bool  `json:"number,omitempty"`
	IsInteger      bool  `json:"integer,omitempty"`
	IsString       bool  `json:"string,omitempty"`
	IsEnum         bool  `json:"enum,omitempty"`
	IsEnumWithNull *bool `json:"enum_with_null,omitempty"`
	IsEnumWithZero *bool `json:"enum_with_zero,omitempty"`
	IsPtr          bool  `json:"ptr,omitempty"`

	// Format related
	HasFormat  *bool `json:"format,omitempty"`
	HasPattern *bool `json:"pattern,omitempty"`

	// Validation related
	IsRequired *bool `json:"required,omitempty"`

	HasMin               *bool `json:"min,omitempty"`
	HasMax               *bool `json:"max,omitempty"`
	IsMinGreaterThanZero *bool `json:"min_gt_zero,omitempty"`
	IsMaxLessThanZero    *bool `json:"max_lt_zero,omitempty"`

	HasMinLength *bool `json:"min_length,omitempty"`
	HasMaxLength *bool `json:"max_length,omitempty"`

	HasMinItems   *bool `json:"min_items,omitempty"`
	HasMaxItems   *bool `json:"max_items,omitempty"`
	IsUniqueItems *bool `json:"unique_items,omitempty"`

	HasMinProps *bool `json:"min_props,omitempty"`
	HasMaxProps *bool `json:"max_props,omitempty"`
}

func (*DerivedRule) MakeConsistent added in v2.1.1

func (v *DerivedRule) MakeConsistent()

type Discriminator

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

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

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

NewGenerator creates a standard generator implementation

type Model

type Model struct {
	// Validation stuff
	ValidationSpec

	// 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. Key is the package and value is the alias
	Imports map[string]string
	// TemplateKind is a kind of generated model (e.g. `struct` or `enum`)
	TemplateKind TemplateKind
	// 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

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

NewModelFromParameters returns a model built from operation parameters

func NewModelFromRef

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

NewModelFromRef creates a model out of a schema

func (*Model) Render

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

Render renders the model to a Go file

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

type PropSpec struct {
	// Validation stuff
	ValidationSpec

	// 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
	// IsPtr is true when the property is a pointer. This is derived from the nullable property in the schema
	IsPtr 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
	// IsArray is true when the property is an array
	IsArray bool
	// IsMap is true when the property is a map
	IsMap bool
	// IsStruct is true when the property is a struct
	IsStruct bool
	// IsString is true when the property is a string
	IsString bool
	// IsNumber is true when the property is a number
	IsNumber bool
	// IsInteger is true when the property is an integer
	IsInteger bool
	// IsRef is true when the property is a Ref to a schema
	IsRef bool
}

PropSpec is a Go property descriptor

type TemplateKind added in v2.1.1

type TemplateKind string

TemplateKind is a kind of template to render

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

type ValidationSpec added in v2.1.0

type ValidationSpec struct {
	// Validation stuff
	HasPattern                 bool
	Pattern                    string
	PatternErrorMsg            string
	NeedsValidation            bool
	HasMin, HasMax             bool
	Min, Max                   float64
	HasMinLength, HasMaxLength bool
	MinLength, MaxLength       uint64
	HasMinItems, HasMaxItems   bool
	MinItems, MaxItems         uint64
	HasMinProps, HasMaxProps   bool
	MinProps, MaxProps         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
	IsEnumWithNil              bool
	IsEnumWithZero             bool

	// Derived validations that should be added to the property
	DerivedValidations []string
}

Jump to

Keyboard shortcuts

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