openapigen

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2023 License: MIT Imports: 12 Imported by: 4

README

openapigen

generate the subset of openapi by builder

examples

see: examples

getting started

$ mkdir -p tools
$ go run github.com/podhmo/gos/openapigen/cmd/init@latest > tools/gen.go
$ go run tools/gen.go |& grep -F "go get" | bash -ux
$ go run tools/gen.go > openapi.json

The results is _examples/01router

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Define added in v0.0.4

func Define[T interface {
	TypeBuilder
	storeType(name string)
}](name string, t T) T

Define names Type value.

func ToGocode added in v0.0.4

func ToGocode(w io.Writer, b *Builder) error

func ToSchema

func ToSchema(b *Builder) (*orderedmap.OrderedMap, error)

func ToSchemaWith

func ToSchemaWith(doc *orderedmap.OrderedMap, b *Builder, useRef bool) (*orderedmap.OrderedMap, error)

func ToString

func ToString(t TypeBuilder) string

Types

type Action

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

func (Action) DefaultError added in v0.0.3

func (b Action) DefaultError(value Type) R

DefaultError set Metadata.DefaultError

func (*Action) GetMetadata

func (t *Action) GetMetadata() *ActionMetadata

func (Action) Method added in v0.0.3

func (b Action) Method(value string) R

Method set Metadata.Method

func (Action) Path added in v0.0.3

func (b Action) Path(value string) R

Path set Metadata.Path

func (Action) Tags added in v0.0.3

func (b Action) Tags(value []string) R

Tags set Metadata.Tags

type ActionMetadata

type ActionMetadata struct {
	Name string `json:"-"`

	Input *Input `json:"-"`

	Outputs []*Output `json:"-"`

	DefaultError Type `json:"-"`

	Method string `json:"-"`

	Path string `json:"-"`

	Tags []string `json:"tags,omitempty"`
}

type Array

type Array[Items Type] struct {
	// contains filtered or unexported fields
}

func (*Array[Items]) GetMetadata

func (t *Array[Items]) GetMetadata() *ArrayMetadata

func (Array) MaxItems added in v0.0.3

func (b Array) MaxItems(value int64) R

MaxItems set Metadata.MaxItems

func (Array) MinItems added in v0.0.3

func (b Array) MinItems(value int64) R

MinItems set Metadata.MinItems

type ArrayMetadata

type ArrayMetadata struct {
	MaxItems int64 `json:"maxItems,omitempty"`

	MinItems int64 `json:"minItems,omitempty"`
}

type Body

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

func (*Body) GetMetadata

func (t *Body) GetMetadata() *BodyMetadata

type BodyMetadata

type BodyMetadata struct {
	Typ Type `json:"-"`
}

type Bool

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

func (Bool) Default added in v0.0.3

func (b Bool) Default(value bool) R

Default set Metadata.Default

func (Bool) Format

func (b Bool) Format(value string) R

Format set Metadata.Format

func (*Bool) GetMetadata

func (t *Bool) GetMetadata() *BoolMetadata

type BoolMetadata

type BoolMetadata struct {
	Format string `json:"format,omitempty"`

	Default bool `json:"default,omitempty"`
}

type Builder

type Builder struct {
	Config *Config
	// contains filtered or unexported fields
}

func NewBuilder added in v0.0.4

func NewBuilder(config *Config) *Builder

func (*Builder) Action

func (b *Builder) Action(name string, inputoroutput ...InputOrOutput) *Action

Action builds Type for Action

func (*Builder) AllOf added in v0.0.6

func (b *Builder) AllOf(types ...Type) *_Container

func (*Builder) AnyOf added in v0.0.6

func (b *Builder) AnyOf(types ...Type) *_Container

func (*Builder) Array

func (b *Builder) Array(items Type) *Array[Type]

Array builds Type for Array

func (*Builder) Body

func (b *Builder) Body(typ Type) *Body

Body builds Type for Body

func (*Builder) Bool

func (b *Builder) Bool() *Bool

Bool builds Type for Bool

func (*Builder) EachTypes

func (b *Builder) EachTypes(fn func(TypeBuilder) error) error

EachType iterates named Type.

func (*Builder) Extension added in v0.0.4

func (b *Builder) Extension(name string, value any) *Extension

Extension builds Type for Extension

func (*Builder) Field

func (b *Builder) Field(name string, typ Type) *Field

Field builds Type for Field

func (*Builder) Float added in v0.0.2

func (b *Builder) Float() *Float

Float builds Type for Float

func (*Builder) Input

func (b *Builder) Input(params ...paramOrBody) *Input

Input builds Type for Input

func (*Builder) Int

func (b *Builder) Int() *Int

Int builds Type for Int

func (*Builder) IntFromEnum

func (b *Builder) IntFromEnum(enum *enumgen.Int) *Int

func (*Builder) Map

func (b *Builder) Map(items Type) *Map[Type]

Map builds Type for Map

func (*Builder) Object

func (b *Builder) Object(fields ...*Field) *Object

Object builds Type for Object

func (*Builder) OneOf added in v0.0.6

func (b *Builder) OneOf(types ...Type) *_Container

func (*Builder) Output

func (b *Builder) Output(typ Type) *Output

Output builds Type for Output

func (*Builder) Param

func (b *Builder) Param(name string, typ Type) *Param

Param builds Type for Param

func (*Builder) Reference

func (b *Builder) Reference(typ TypeBuilder) *TypeRef

func (*Builder) ReferenceByName

func (b *Builder) ReferenceByName(name string) *TypeRef

func (*Builder) String

func (b *Builder) String() *String

String builds Type for String

func (*Builder) StringFromEnum

func (b *Builder) StringFromEnum(enum *enumgen.String) *String

type Config

type Config struct {
	DisableRefLinks bool // if true, does not use $ref links

	FuncMap  template.FuncMap
	Template *template.Template
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig() *Config

func (*Config) ToGoCode added in v0.0.4

func (c *Config) ToGoCode(w io.Writer, tb TypeBuilder) error

type Contact added in v0.0.2

type Contact struct {
	Name string `json:"name"`

	URL string `json:"url"`

	Email string `json:"email"`
}

type Extension added in v0.0.4

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

func (*Extension) GetMetadata added in v0.0.4

func (t *Extension) GetMetadata() *ExtensionMetadata

type ExtensionMetadata added in v0.0.4

type ExtensionMetadata struct {
	Name string

	Value any
}

for x-<extension-name>

type Field

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

func (Field) AllowEmptyValue added in v0.0.3

func (b Field) AllowEmptyValue(value bool) R

AllowEmptyValue set Metadata.AllowEmptyValue

func (Field) Deprecated added in v0.0.3

func (b Field) Deprecated(value bool) R

Deprecated set Metadata.Deprecated

func (Field) Doc

func (b Field) Doc(stmts ...string) R

func (*Field) GetMetadata

func (t *Field) GetMetadata() *FieldMetadata

func (Field) Nullable added in v0.0.3

func (b Field) Nullable(value bool) R

Nullable set Metadata.Nullable

func (Field) ReadOnly added in v0.0.3

func (b Field) ReadOnly(value bool) R

ReadOnly set Metadata.ReadOnly

func (Field) Required added in v0.0.3

func (b Field) Required(value bool) R

Required set Metadata.Required

func (Field) WriteOnly added in v0.0.3

func (b Field) WriteOnly(value bool) R

WriteOnly set Metadata.WriteOnly

type FieldMetadata

type FieldMetadata struct {
	Name string `json:"-"`

	Typ Type `json:"-"`

	Doc string `json:"description,omitempty"`

	Nullable bool `json:"nullable,omitempty"`

	Required bool `json:"-"`

	ReadOnly bool `json:"readonly,omitempty"`

	WriteOnly bool `json:"writeonly,omitempty"`

	AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`

	Deprecated bool `json:"deprecated,omitempty"`
}

type Float added in v0.0.2

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

func (Float) Default added in v0.0.3

func (b Float) Default(value string) R

Default set Metadata.Default

func (Float) ExclusiveMax added in v0.0.3

func (b Float) ExclusiveMax(value bool) R

ExclusiveMax set Metadata.ExclusiveMax

func (Float) ExclusiveMin added in v0.0.3

func (b Float) ExclusiveMin(value bool) R

ExclusiveMin set Metadata.ExclusiveMin

func (Float) Format added in v0.0.2

func (b Float) Format(value string) R

Format set Metadata.Format

func (*Float) GetMetadata added in v0.0.2

func (t *Float) GetMetadata() *FloatMetadata

func (Float) Maximum added in v0.0.3

func (b Float) Maximum(value float64) R

Maximum set Metadata.Maximum

func (Float) Minimum added in v0.0.3

func (b Float) Minimum(value float64) R

Minimum set Metadata.Minimum

func (Float) MultipleOf added in v0.0.3

func (b Float) MultipleOf(value float64) R

MultipleOf set Metadata.MultipleOf

type FloatMetadata added in v0.0.2

type FloatMetadata struct {
	Format string `json:"format,omitempty"`

	Default string `json:"default,omitempty"`

	Maximum float64 `json:"maximum,omitempty"`

	Minimum float64 `json:"minimum,omitempty"`

	MultipleOf float64 `json:"multipleOf,omitempty"`

	ExclusiveMin bool `json:"exclusiveMin,omitempty"`

	ExclusiveMax bool `json:"exclusiveMax,omitempty"`
}

type Info added in v0.0.2

type Info struct {

	// required
	Title string `json:"title"`

	Summary string `json:"summary,omitempty"`

	Doc string `json:"description,omitempty"`

	TermsOfService string `json:"termOfService,omitempty"`

	Contact *Contact `json:"contact,omitempty"`

	License *License `json:"license,omitempty"`
	// required
	Version string `json:"version"`
}

type Input

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

func (*Input) GetMetadata

func (t *Input) GetMetadata() *InputMetadata

type InputMetadata

type InputMetadata struct {
	Params []*Param

	Body *Body
}

type InputOrOutput added in v0.0.3

type InputOrOutput interface {
	// contains filtered or unexported methods
}

type Int

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

func (Int) Default added in v0.0.3

func (b Int) Default(value int64) R

Default set Metadata.Default

func (Int) Enum added in v0.0.3

func (b Int) Enum(value []int64) R

Enum set Metadata.Enum

func (Int) ExclusiveMax added in v0.0.3

func (b Int) ExclusiveMax(value bool) R

ExclusiveMax set Metadata.ExclusiveMax

func (Int) ExclusiveMin added in v0.0.3

func (b Int) ExclusiveMin(value bool) R

ExclusiveMin set Metadata.ExclusiveMin

func (Int) Format

func (b Int) Format(value string) R

Format set Metadata.Format

func (*Int) GetMetadata

func (t *Int) GetMetadata() *IntMetadata

func (Int) Maximum added in v0.0.3

func (b Int) Maximum(value int64) R

Maximum set Metadata.Maximum

func (Int) Minimum added in v0.0.3

func (b Int) Minimum(value int64) R

Minimum set Metadata.Minimum

type IntMetadata

type IntMetadata struct {
	Format string `json:"format,omitempty"`

	Enum []int64 `json:"enum,omitempty"`

	Default int64 `json:"default,omitempty"`

	Maximum int64 `json:"maximum,omitempty"`

	Minimum int64 `json:"minimum,omitempty"`

	ExclusiveMin bool `json:"exclusiveMin,omitempty"`

	ExclusiveMax bool `json:"exclusiveMax,omitempty"`
}

type License added in v0.0.2

type License struct {

	// required
	Name string `json:"name"`

	Identifier string `json:"identifier"`

	URL string `json:"url"`
}

type Map

type Map[Items Type] struct {
	// contains filtered or unexported fields
}

func (*Map[Items]) GetMetadata

func (t *Map[Items]) GetMetadata() *MapMetadata

func (Map) Pattern added in v0.0.3

func (b Map) Pattern(value string) R

Pattern set Metadata.Pattern

type MapMetadata

type MapMetadata struct {
	Pattern string `json:"pattern,omitempty"`
}

type Object

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

func (*Object) GetMetadata

func (t *Object) GetMetadata() *ObjectMetadata

func (*Object) IgnoreFields

func (t *Object) IgnoreFields(names ...string) []*Field

func (Object) MaxProperties added in v0.0.3

func (b Object) MaxProperties(value uint64) R

MaxProperties set Metadata.MaxProperties

func (Object) MinProperties added in v0.0.3

func (b Object) MinProperties(value uint64) R

MinProperties set Metadata.MinProperties

func (*Object) OnlyFields

func (t *Object) OnlyFields(names ...string) []*Field

func (Object) Strict added in v0.0.3

func (b Object) Strict(value bool) R

Strict set Metadata.Strict

type ObjectMetadata

type ObjectMetadata struct {
	Fields []*Field `json:"-"`

	MaxProperties uint64 `json:"maxProeprties,omitempty"`

	MinProperties uint64 `json:"minProeprties,omitempty"`

	Strict bool `json:"-"`
}

type OpenAPI added in v0.0.2

type OpenAPI struct {

	// required
	OpenAPI string `json:"openapi"`
	// required
	Info Info `json:"info"`

	Servers []Server `json:"servers"`
}

type Output

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

func (*Output) GetMetadata

func (t *Output) GetMetadata() *OutputMetadata

func (Output) IsDefault added in v0.0.3

func (b Output) IsDefault(value bool) R

IsDefault set Metadata.IsDefault

func (Output) Status added in v0.0.3

func (b Output) Status(value int) R

Status set Metadata.Status

type OutputMetadata

type OutputMetadata struct {
	Typ Type `json:"-"`

	Status int `json:"-"`

	IsDefault bool `json:"-"`
}

type Param

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

func (Param) AllowEmptyValue added in v0.0.3

func (b Param) AllowEmptyValue(value bool) R

AllowEmptyValue set Metadata.AllowEmptyValue

func (*Param) AsCookie

func (b *Param) AsCookie() *Param

func (*Param) AsHeader

func (b *Param) AsHeader() *Param

func (*Param) AsPath

func (b *Param) AsPath() *Param

func (*Param) AsQuery

func (b *Param) AsQuery() *Param

func (Param) Deprecated added in v0.0.3

func (b Param) Deprecated(value bool) R

Deprecated set Metadata.Deprecated

func (Param) Doc

func (b Param) Doc(stmts ...string) R

func (*Param) GetMetadata

func (t *Param) GetMetadata() *ParamMetadata

func (Param) In added in v0.0.3

func (b Param) In(value string) R

In set Metadata.In

func (Param) Required added in v0.0.3

func (b Param) Required(value bool) R

Required set Metadata.Required

type ParamMetadata

type ParamMetadata struct {
	Name string `json:"name"`
	// openapi's in parameter {query, header, path, cookie} (default is query)
	In string `json:"in"`

	Typ Type `json:"-"`

	Doc string `json:"description,omitempty"`

	Required bool `json:"required"`

	Deprecated bool `json:"deprecated,omitempty"`

	AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
}

type Router

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

func NewRouter

func NewRouter(defaultError Type) *Router

func (*Router) Delete

func (r *Router) Delete(path string, action *Action)

func (*Router) Get

func (r *Router) Get(path string, action *Action)

func (*Router) Method

func (r *Router) Method(method string, path string, action *Action)

func (*Router) Patch

func (r *Router) Patch(path string, action *Action)

func (*Router) Post

func (r *Router) Post(path string, action *Action)

func (*Router) Put

func (r *Router) Put(path string, action *Action)

func (*Router) Tagged

func (r *Router) Tagged(tags ...string) *Router

func (*Router) ToSchemaWith

func (r *Router) ToSchemaWith(b *Builder, doc *orderedmap.OrderedMap) error

type Server added in v0.0.2

type Server struct {

	// required
	URL string `json:"url"`

	Doc string `json:"description"`
	// todo: typed
	Variables map[string]any `json:"variables,omitempty"`
}

type String

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

func (String) Default added in v0.0.3

func (b String) Default(value string) R

Default set Metadata.Default

func (String) Enum added in v0.0.3

func (b String) Enum(value []string) R

Enum set Metadata.Enum

func (String) Format

func (b String) Format(value string) R

Format set Metadata.Format

func (*String) GetMetadata

func (t *String) GetMetadata() *StringMetadata

func (String) MaxLength added in v0.0.3

func (b String) MaxLength(value int64) R

MaxLength set Metadata.MaxLength

func (String) MinLength added in v0.0.3

func (b String) MinLength(value int64) R

MinLength set Metadata.MinLength

func (String) Pattern added in v0.0.3

func (b String) Pattern(value string) R

Pattern set Metadata.Pattern

type StringMetadata

type StringMetadata struct {
	Format string `json:"format,omitempty"`

	Enum []string `json:"enum,omitempty"`

	Default string `json:"default,omitempty"`

	Pattern string `json:"pattern,omitempty"`

	MaxLength int64 `json:"maxLength,omitempty"`

	MinLength int64 `json:"minLength,omitempty"`
}

type Type added in v0.0.3

type Type interface {
	TypeBuilder
	// contains filtered or unexported methods
}

Type is the one of pseudo sum types (union). Type is union of bool | int | float | string | object | array[T] | map[T]

type TypeBuilder

type TypeBuilder interface {
	GetTypeMetadata() *TypeMetadata
	// contains filtered or unexported methods
}

type TypeMetadata

type TypeMetadata struct {
	Name string `json:"-"` // required by reference (and toString)

	Doc        string                 `json:"description,omitempty"`
	Title      string                 `json:"title,omitempty"`
	Example    string                 `json:"example,omitempty"`
	Extensions *orderedmap.OrderedMap `json:"-"`
	// contains filtered or unexported fields
}

type TypeRef

type TypeRef struct {
	Name string
	// contains filtered or unexported fields
}

func (*TypeRef) GetTypeMetadata

func (t *TypeRef) GetTypeMetadata() *TypeMetadata

Directories

Path Synopsis
_examples
00schema command
01router command
02separated command
03tocode command
04pagination command
05oneof command
cmd
init command

Jump to

Keyboard shortcuts

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