goai

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package goai 实现并提供了针对 OpenApi 规范的文档生成功能。

参考链接:https://editor.swagger.io/

Index

Constants

View Source
const (
	TypeInteger    = `integer`
	TypeNumber     = `number`
	TypeBoolean    = `boolean`
	TypeArray      = `array`
	TypeString     = `string`
	TypeFile       = `file`
	TypeObject     = `object`
	FormatInt32    = `int32`
	FormatInt64    = `int64`
	FormatDouble   = `double`
	FormatByte     = `byte`
	FormatBinary   = `binary`
	FormatDate     = `date`
	FormatDateTime = `date-time`
	FormatPassword = `password`
)
View Source
const (
	ParameterInHeader = `header`
	ParameterInPath   = `path`
	ParameterInQuery  = `query`
	ParameterInCookie = `cookie`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AddInput

type AddInput struct {
	Path   string      // Path 指定自定义路径,如果在结构体标签的 Meta 中未配置此路径,则使用该指定路径。
	Prefix string      // Prefix 指定自定义路由路径前缀,它将与结构体标签中 Meta 的 path 标签相结合。
	Method string      // Method 指定自定义的 HTTP 方法,如果在结构体标签的 Meta 中未配置该方法时使用。
	Object interface{} // Object 可以是结构体实例或路由函数。
}

AddInput 是函数 OpenApiV3.Add 的结构化参数。

type Callback

type Callback map[string]*Path

Callback 根据 OpenAPI/Swagger 3.0 标准进行定义。

type CallbackRef

type CallbackRef struct {
	Ref   string
	Value *Callback
}

func (CallbackRef) MarshalJSON

func (r CallbackRef) MarshalJSON() ([]byte, error)

type Callbacks

type Callbacks map[string]*CallbackRef

type Components

type Components struct {
	Schemas         Schemas         `json:"schemas,omitempty"`
	Parameters      ParametersMap   `json:"parameters,omitempty"`
	Headers         Headers         `json:"headers,omitempty"`
	RequestBodies   RequestBodies   `json:"requestBodies,omitempty"`
	Responses       Responses       `json:"responses,omitempty"`
	SecuritySchemes SecuritySchemes `json:"securitySchemes,omitempty"`
	Examples        Examples        `json:"examples,omitempty"`
	Links           Links           `json:"links,omitempty"`
	Callbacks       Callbacks       `json:"callbacks,omitempty"`
}

Components是按照OpenAPI/Swagger 3.0标准指定的。

type Config

type Config struct {
	ReadContentTypes       []string    // ReadContentTypes 指定了在未配置 MIME 类型时默认用于消费的 MIME 类型。
	WriteContentTypes      []string    // WriteContentTypes 指定在未配置 MIME 类型时生成内容时的默认 MIME 类型。
	CommonRequest          interface{} // 通用请求结构,适用于所有路径。
	CommonRequestDataField string      // 该注释描述了一个通用请求字段名称,它将在特定业务请求结构中被替换。例如:`Data`, `Request`。
	// 翻译为:
	// 这是一个通用请求字段名,通常会在特定的业务请求结构中用相应的内容进行替换。例如:`Data`、`Request`。
	CommonResponse          interface{} // 通用响应结构,适用于所有路径。
	CommonResponseDataField string      // 常用的响应字段名称,将会被替换为特定业务响应结构。例如:`Data`、`Response`。
	IgnorePkgPath           bool        // 忽略包名以获取模式名称。
}

Config 为 OpenApiV3 的实现提供了额外的配置功能。

type Contact

type Contact struct {
	Name  string `json:"name,omitempty"`
	URL   string `json:"url,omitempty"`
	Email string `json:"email,omitempty"`
}

Contact 是由 OpenAPI/Swagger 标准版本 3.0 规定的联系信息结构体。

type Content

type Content map[string]MediaType

Content 是由 OpenAPI/Swagger 3.0 标准指定的。

type Discriminator

type Discriminator struct {
	PropertyName string            `json:"propertyName"`
	Mapping      map[string]string `json:"mapping,omitempty"`
}

Discriminator 是由 OpenAPI/Swagger 3.0 标准指定的。

type Encoding

type Encoding struct {
	ContentType   string  `json:"contentType,omitempty"`
	Headers       Headers `json:"headers,omitempty"`
	Style         string  `json:"style,omitempty"`
	Explode       *bool   `json:"explode,omitempty"`
	AllowReserved bool    `json:"allowReserved,omitempty"`
}

编码由 OpenAPI/Swagger 3.0 标准指定。

type Example

type Example struct {
	Summary       string      `json:"summary,omitempty"`
	Description   string      `json:"description,omitempty"`
	Value         interface{} `json:"value,omitempty"`
	ExternalValue string      `json:"externalValue,omitempty"`
}

示例由OpenAPI/Swagger 3.0标准指定。

type ExampleRef

type ExampleRef struct {
	Ref   string
	Value *Example
}

func (ExampleRef) MarshalJSON

func (r ExampleRef) MarshalJSON() ([]byte, error)

type Examples

type Examples map[string]*ExampleRef

type ExternalDocs

type ExternalDocs struct {
	URL         string `json:"url,omitempty"`
	Description string `json:"description,omitempty"`
}

ExternalDocs 是由 OpenAPI/Swagger 标准 3.0 版本定义的。

func (*ExternalDocs) UnmarshalValue

func (ed *ExternalDocs) UnmarshalValue(value interface{}) error
type Header struct {
	Parameter
}

Header 按照 OpenAPI/Swagger 3.0 标准进行指定。 详情请参阅 https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#headerObject

type HeaderRef

type HeaderRef struct {
	Ref   string
	Value *Header
}

func (HeaderRef) MarshalJSON

func (r HeaderRef) MarshalJSON() ([]byte, error)

type Headers

type Headers map[string]HeaderRef

type Info

type Info struct {
	Title          string   `json:"title"`
	Description    string   `json:"description,omitempty"`
	TermsOfService string   `json:"termsOfService,omitempty"`
	Contact        *Contact `json:"contact,omitempty"`
	License        *License `json:"license,omitempty"`
	Version        string   `json:"version"`
}

Info 由 OpenAPI/Swagger 标准版本 3.0 规定。

type License

type License struct {
	Name string `json:"name"`
	URL  string `json:"url,omitempty"`
}

License 由 OpenAPI/Swagger 标准版本 3.0 规定。

type Link struct {
	OperationID  string                 `json:"operationId,omitempty"`
	OperationRef string                 `json:"operationRef,omitempty"`
	Description  string                 `json:"description,omitempty"`
	Parameters   map[string]interface{} `json:"parameters,omitempty"`
	Server       *Server                `json:"server,omitempty"`
	RequestBody  interface{}            `json:"requestBody,omitempty"`
}

Link 是由 OpenAPI/Swagger 标准版本 3.0 规定的。

type LinkRef

type LinkRef struct {
	Ref   string
	Value *Link
}

func (LinkRef) MarshalJSON

func (r LinkRef) MarshalJSON() ([]byte, error)
type Links map[string]LinkRef

type MediaType

type MediaType struct {
	Schema   *SchemaRef           `json:"schema,omitempty"`
	Example  interface{}          `json:"example,omitempty"`
	Examples Examples             `json:"examples,omitempty"`
	Encoding map[string]*Encoding `json:"encoding,omitempty"`
}

MediaType 是由 OpenAPI/Swagger 3.0 标准指定的。

type OAuthFlow

type OAuthFlow struct {
	AuthorizationURL string            `json:"authorizationUrl,omitempty"`
	TokenURL         string            `json:"tokenUrl,omitempty"`
	RefreshURL       string            `json:"refreshUrl,omitempty"`
	Scopes           map[string]string `json:"scopes"`
}

type OAuthFlows

type OAuthFlows struct {
	Implicit          *OAuthFlow `json:"implicit,omitempty"`
	Password          *OAuthFlow `json:"password,omitempty"`
	ClientCredentials *OAuthFlow `json:"clientCredentials,omitempty"`
	AuthorizationCode *OAuthFlow `json:"authorizationCode,omitempty"`
}

type OpenApiV3

type OpenApiV3 struct {
	Config       Config                `json:"-"`
	OpenAPI      string                `json:"openapi"`
	Components   Components            `json:"components,omitempty"`
	Info         Info                  `json:"info"`
	Paths        Paths                 `json:"paths"`
	Security     *SecurityRequirements `json:"security,omitempty"`
	Servers      *Servers              `json:"servers,omitempty"`
	Tags         *Tags                 `json:"tags,omitempty"`
	ExternalDocs *ExternalDocs         `json:"externalDocs,omitempty"`
}

OpenApiV3 是从以下网址定义的结构体: https://swagger.io/specification/ https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md (译:OpenApiV3 结构体是根据以下链接中定义的 OpenAPI 3.0 规范实现的:) (https://swagger.io/specification/,以及 OpenAPI 3.0 的具体版本规范:) (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md

func New

func New() *OpenApiV3

New 创建并返回一个实现了 OpenApiV3 的对象。

func (*OpenApiV3) Add

func (oai *OpenApiV3) Add(in AddInput) error

Add 将一个结构体实例或路由函数添加到OpenApiV3定义实现中。

func (OpenApiV3) String

func (oai OpenApiV3) String() string

type Operation

type Operation struct {
	Tags         []string              `json:"tags,omitempty"`
	Summary      string                `json:"summary,omitempty"`
	Description  string                `json:"description,omitempty"`
	OperationID  string                `json:"operationId,omitempty"`
	Parameters   Parameters            `json:"parameters,omitempty"`
	RequestBody  *RequestBodyRef       `json:"requestBody,omitempty"`
	Responses    Responses             `json:"responses"`
	Deprecated   bool                  `json:"deprecated,omitempty"`
	Callbacks    *Callbacks            `json:"callbacks,omitempty"`
	Security     *SecurityRequirements `json:"security,omitempty"`
	Servers      *Servers              `json:"servers,omitempty"`
	ExternalDocs *ExternalDocs         `json:"externalDocs,omitempty"`
	XExtensions  XExtensions           `json:"-"`
}

Operation 表示由 OpenAPI/Swagger 3.0 标准定义的 "操作"。

func (Operation) MarshalJSON

func (o Operation) MarshalJSON() ([]byte, error)

type Parameter

type Parameter struct {
	Name            string      `json:"name,omitempty"`
	In              string      `json:"in,omitempty"`
	Description     string      `json:"description,omitempty"`
	Style           string      `json:"style,omitempty"`
	Explode         *bool       `json:"explode,omitempty"`
	AllowEmptyValue bool        `json:"allowEmptyValue,omitempty"`
	AllowReserved   bool        `json:"allowReserved,omitempty"`
	Deprecated      bool        `json:"deprecated,omitempty"`
	Required        bool        `json:"required,omitempty"`
	Schema          *SchemaRef  `json:"schema,omitempty"`
	Example         interface{} `json:"example,omitempty"`
	Examples        *Examples   `json:"examples,omitempty"`
	Content         *Content    `json:"content,omitempty"`
	XExtensions     XExtensions `json:"-"`
}

参数遵循OpenAPI/Swagger 3.0标准指定。 详情请参阅:https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#parameterObject

func (Parameter) MarshalJSON

func (p Parameter) MarshalJSON() ([]byte, error)

type ParameterRef

type ParameterRef struct {
	Ref   string
	Value *Parameter
}

func (ParameterRef) MarshalJSON

func (r ParameterRef) MarshalJSON() ([]byte, error)

type Parameters

type Parameters []ParameterRef

参数遵循OpenAPI/Swagger 3.0标准进行指定。

type ParametersMap

type ParametersMap map[string]*ParameterRef

type Path

type Path struct {
	Ref         string      `json:"$ref,omitempty"`
	Summary     string      `json:"summary,omitempty"`
	Description string      `json:"description,omitempty"`
	Connect     *Operation  `json:"connect,omitempty"`
	Delete      *Operation  `json:"delete,omitempty"`
	Get         *Operation  `json:"get,omitempty"`
	Head        *Operation  `json:"head,omitempty"`
	Options     *Operation  `json:"options,omitempty"`
	Patch       *Operation  `json:"patch,omitempty"`
	Post        *Operation  `json:"post,omitempty"`
	Put         *Operation  `json:"put,omitempty"`
	Trace       *Operation  `json:"trace,omitempty"`
	Servers     Servers     `json:"servers,omitempty"`
	Parameters  Parameters  `json:"parameters,omitempty"`
	XExtensions XExtensions `json:"-"`
}

Path 是由 OpenAPI/Swagger 标准版本 3.0 规定的。

func (Path) MarshalJSON

func (p Path) MarshalJSON() ([]byte, error)

MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。

type Paths

type Paths map[string]Path

路径由OpenAPI/Swagger标准版本3.0指定。

type RequestBodies

type RequestBodies map[string]*RequestBodyRef

type RequestBody

type RequestBody struct {
	Description string  `json:"description,omitempty"`
	Required    bool    `json:"required,omitempty"`
	Content     Content `json:"content,omitempty"`
}

RequestBody 由 OpenAPI/Swagger 3.0 标准指定。

type RequestBodyRef

type RequestBodyRef struct {
	Ref   string
	Value *RequestBody
}

func (RequestBodyRef) MarshalJSON

func (r RequestBodyRef) MarshalJSON() ([]byte, error)

type Response

type Response struct {
	Description string      `json:"description"`
	Headers     Headers     `json:"headers,omitempty"`
	Content     Content     `json:"content,omitempty"`
	Links       Links       `json:"links,omitempty"`
	XExtensions XExtensions `json:"-"`
}

Response 由 OpenAPI/Swagger 3.0 标准指定。

func (Response) MarshalJSON

func (r Response) MarshalJSON() ([]byte, error)

type ResponseRef

type ResponseRef struct {
	Ref   string
	Value *Response
}

func (ResponseRef) MarshalJSON

func (r ResponseRef) MarshalJSON() ([]byte, error)

type Responses

type Responses map[string]ResponseRef

Responses 按照 OpenAPI/Swagger 3.0 标准进行指定。

type Schema

type Schema struct {
	OneOf                SchemaRefs     `json:"oneOf,omitempty"`
	AnyOf                SchemaRefs     `json:"anyOf,omitempty"`
	AllOf                SchemaRefs     `json:"allOf,omitempty"`
	Not                  *SchemaRef     `json:"not,omitempty"`
	Type                 string         `json:"type,omitempty"`
	Title                string         `json:"title,omitempty"`
	Format               string         `json:"format,omitempty"`
	Description          string         `json:"description,omitempty"`
	Enum                 []interface{}  `json:"enum,omitempty"`
	Default              interface{}    `json:"default,omitempty"`
	Example              interface{}    `json:"example,omitempty"`
	ExternalDocs         *ExternalDocs  `json:"externalDocs,omitempty"`
	UniqueItems          bool           `json:"uniqueItems,omitempty"`
	ExclusiveMin         bool           `json:"exclusiveMinimum,omitempty"`
	ExclusiveMax         bool           `json:"exclusiveMaximum,omitempty"`
	Nullable             bool           `json:"nullable,omitempty"`
	ReadOnly             bool           `json:"readOnly,omitempty"`
	WriteOnly            bool           `json:"writeOnly,omitempty"`
	AllowEmptyValue      bool           `json:"allowEmptyValue,omitempty"`
	XML                  interface{}    `json:"xml,omitempty"`
	Deprecated           bool           `json:"deprecated,omitempty"`
	Min                  *float64       `json:"minimum,omitempty"`
	Max                  *float64       `json:"maximum,omitempty"`
	MultipleOf           *float64       `json:"multipleOf,omitempty"`
	MinLength            uint64         `json:"minLength,omitempty"`
	MaxLength            *uint64        `json:"maxLength,omitempty"`
	Pattern              string         `json:"pattern,omitempty"`
	MinItems             uint64         `json:"minItems,omitempty"`
	MaxItems             *uint64        `json:"maxItems,omitempty"`
	Items                *SchemaRef     `json:"items,omitempty"`
	Required             []string       `json:"required,omitempty"`
	Properties           Schemas        `json:"properties,omitempty"`
	MinProps             uint64         `json:"minProperties,omitempty"`
	MaxProps             *uint64        `json:"maxProperties,omitempty"`
	AdditionalProperties *SchemaRef     `json:"additionalProperties,omitempty"`
	Discriminator        *Discriminator `json:"discriminator,omitempty"`
	XExtensions          XExtensions    `json:"-"`
	ValidationRules      string         `json:"-"`
}

Schema遵循OpenAPI/Swagger 3.0标准进行定义。

func (*Schema) Clone

func (s *Schema) Clone() *Schema

Clone仅克隆必要的属性。 TODO:克隆所有属性,或改进包deepcopy。

func (Schema) MarshalJSON

func (s Schema) MarshalJSON() ([]byte, error)

type SchemaRef

type SchemaRef struct {
	Ref   string
	Value *Schema
}

func (SchemaRef) MarshalJSON

func (r SchemaRef) MarshalJSON() ([]byte, error)

type SchemaRefs

type SchemaRefs []SchemaRef

type Schemas

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

func (*Schemas) Clone

func (s *Schemas) Clone() Schemas

func (*Schemas) Get

func (s *Schemas) Get(name string) *SchemaRef

func (*Schemas) Map

func (s *Schemas) Map() map[string]SchemaRef

func (Schemas) MarshalJSON

func (s Schemas) MarshalJSON() ([]byte, error)

func (*Schemas) Removes

func (s *Schemas) Removes(names []interface{})

func (*Schemas) X设置值

func (s *Schemas) X设置值(name string, ref SchemaRef)

func (*Schemas) X遍历

func (s *Schemas) X遍历(f func(key string, ref SchemaRef) bool)

type SecurityRequirement

type SecurityRequirement map[string][]string

type SecurityRequirements

type SecurityRequirements []SecurityRequirement

type SecurityScheme

type SecurityScheme struct {
	Type             string      `json:"type,omitempty"`
	Description      string      `json:"description,omitempty"`
	Name             string      `json:"name,omitempty"`
	In               string      `json:"in,omitempty"`
	Scheme           string      `json:"scheme,omitempty"`
	BearerFormat     string      `json:"bearerFormat,omitempty"`
	Flows            *OAuthFlows `json:"flows,omitempty"`
	OpenIdConnectUrl string      `json:"openIdConnectUrl,omitempty"`
}

type SecuritySchemeRef

type SecuritySchemeRef struct {
	Ref   string
	Value *SecurityScheme
}

func (SecuritySchemeRef) MarshalJSON

func (r SecuritySchemeRef) MarshalJSON() ([]byte, error)

type SecuritySchemes

type SecuritySchemes map[string]SecuritySchemeRef

type Server

type Server struct {
	URL         string                     `json:"url"`
	Description string                     `json:"description,omitempty"`
	Variables   map[string]*ServerVariable `json:"variables,omitempty"`
}

Server 按照 OpenAPI/Swagger 3.0 标准定义。

type ServerVariable

type ServerVariable struct {
	Enum        []string `json:"enum,omitempty"`
	Default     string   `json:"default,omitempty"`
	Description string   `json:"description,omitempty"`
}

ServerVariable 是由 OpenAPI/Swagger 3.0 标准指定的。

type Servers

type Servers []Server

Servers 是由 OpenAPI/Swagger 标准版本 3.0 规定的。

type Tag

type Tag struct {
	Name         string        `json:"name,omitempty"`
	Description  string        `json:"description,omitempty"`
	ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"`
}

Tag 是由 OpenAPI/Swagger 3.0 标准指定的。

type Tags

type Tags []Tag

Tags 是由 OpenAPI/Swagger 3.0 标准指定的。

type XExtensions

type XExtensions map[string]string

XExtensions 用于存储以 `x-` 开头的自定义扩展。

Jump to

Keyboard shortcuts

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