openapi

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AnnotateSplitChar = "|"
	AuthStart         = "auth:"

	ResponseTypeJSON  = "json"
	ResponseTypeXML   = "xml"
	ResponseTypeBytes = "bytes"

	AuthTypeJWT     = "JWT"
	AuthTypeOAuth2  = "OAuth2"
	AuthTypeSession = "Session"
	AuthTypeCookie  = "Cookie"

	ApiParamsBindMethod    = "Bind"
	ApiParamsSuccessMethod = "Success"
)
View Source
const (
	ExprStruct = "struct"
	ExprFunc   = "func"
)
View Source
const (
	PropertyTypeObject = "object"
	PropertyTypeArray  = "array"
)
View Source
const (
	TagParamPath = "uri"
	TagParamFrom = "form"
	TagParamJson = "json"
	TagParamXml  = "xml"
	TagParamYaml = "yaml"

	OpenApiInPath   = "path"
	OpenApiInQuery  = "query"
	OpenApiInHeader = "header"
	OpenApiInCookie = "cookie"

	TagValidate = "validate"

	OpenApiTypeArray   = "array"
	OpenApiTypeBoolean = "boolean"
	OpenApiTypeInteger = "integer"
	OpenApiTypeNull    = "null"
	OpenApiTypeNumber  = "number"
	OpenApiTypeObject  = "object"
	OpenApiTypeString  = "string"

	OpenApiSchemasPrefix = "#/components/schemas/"
)
View Source
const ConfigFileName = "config_origin.yaml"

Variables

View Source
var DefaultInfo = OpenAPI{
	Openapi: "3.0.0",
	Components: Components{
		Schemas:         make(map[string]Property),
		Responses:       make(map[string]Response),
		Headers:         make(map[string]Header),
		RequestBodies:   make(map[string]RequestBody),
		SecuritySchemes: make(map[string]*SecurityScheme),
	},
	Paths: make(map[string]map[string]Method),
}

Functions

func FindImports

func FindImports(file *ast.File) map[string]string

func FindReturnInBlockStmt

func FindReturnInBlockStmt(stmt ast.Stmt) []*ast.ReturnStmt

func FindRspIndex

func FindRspIndex(rspName string, lhs []ast.Expr) int

func FindSelectorPath

func FindSelectorPath(s *ast.SelectorExpr) []string

func FormatComment

func FormatComment(comments *ast.CommentGroup) string

func GenFromPath

func GenFromPath(name, des, version, path, out string)

GenFromPath 通过目录生成

func GetTagInfo

func GetTagInfo(fieldTag string) (tag, name, validate string)

Types

type Api

type Api struct {
	Name         string  `json:"name"`         //接口地址
	Summary      string  `json:"summary"`      //名称
	Description  string  `json:"description"`  //描述
	RequestPath  string  `json:"request_path"` //自定义路径
	Point        string  `json:"point"`        //接口结构体对象名称
	Group        string  `json:"group"`        //接口结构体名称
	HttpMethod   string  `json:"method"`       //接口http方法
	Annotate     string  `json:"annotate"`     //注释
	Path         string  `json:"path"`         //文件地址
	Auth         string  `json:"auth"`         //授权方式
	AuthTo       string  `json:"authTo"`       //授权方式
	ResponseType string  `json:"responseType"` //返回类型
	Params       *Struct `json:"params"`       //参数 string为路径 Parameter为对象
	Response     *Struct `json:"response"`     //返回结构体
	// contains filtered or unexported fields
}

func (*Api) AnalysisAnnotate

func (a *Api) AnalysisAnnotate()

func (*Api) FillRequestParams

func (a *Api) FillRequestParams(method *Method)

FillRequestParams 填充请求参数

func (*Api) FillResponse

func (a *Api) FillResponse(method *Method)

FillResponse 填充返回参数

func (*Api) FillSecurity

func (a *Api) FillSecurity(method *Method)

FillSecurity 填充Security

func (*Api) FindApiParameter

func (a *Api) FindApiParameter(body *ast.BlockStmt)

FindApiParameter 构建API基础参数

func (*Api) FindRspMethodMap

func (a *Api) FindRspMethodMap(rhs []ast.Expr) []string

func (*Api) GetApiPath

func (a *Api) GetApiPath() string

func (*Api) GetOperationId

func (a *Api) GetOperationId() string

func (*Api) GetParameterStruct

func (a *Api) GetParameterStruct(expr ast.Expr) *Struct

func (*Api) GetRequestPath

func (a *Api) GetRequestPath() string

func (*Api) GetRequestPathNoGroup

func (a *Api) GetRequestPathNoGroup() string

func (*Api) GetResponseData

func (a *Api) GetResponseData() *Struct

func (*Api) GetResponseStruct

func (a *Api) GetResponseStruct(expr ast.Expr)

func (*Api) NewStruct

func (a *Api) NewStruct() *Struct

func (*Api) SetResponseDataStruct

func (a *Api) SetResponseDataStruct(array bool, s *Struct)

SetResponseDataStruct 返回为结构体时

func (*Api) SetResponseDataType

func (a *Api) SetResponseDataType(array bool, typ string)

SetResponseDataType 设置返回为基础类型时

type ApiPathsMap

type ApiPathsMap map[string]map[string]Method

ApiPathsMap api解决乱序问题

func (ApiPathsMap) MarshalJSON

func (m ApiPathsMap) MarshalJSON() ([]byte, error)

type BaseType

type BaseType []string

func (BaseType) CheckIn

func (bt BaseType) CheckIn(typ string) bool

type Components

type Components struct {
	Schemas map[string]Property `json:"schemas,omitempty"`

	Responses map[string]Response `json:"responses,omitempty"`

	Parameters []Parameter `json:"parameters,omitempty"`

	RequestBodies map[string]RequestBody `json:"requestBodies,omitempty"`

	Headers map[string]Header `json:"headers,omitempty"`

	SecuritySchemes map[string]*SecurityScheme `json:"securitySchemes,omitempty"`
}

func (Components) GetHeadersName

func (c Components) GetHeadersName() string

func (Components) GetParametersName

func (c Components) GetParametersName() string

func (Components) GetRequestBodiesName

func (c Components) GetRequestBodiesName() string

func (Components) GetResponsesName

func (c Components) GetResponsesName() string

func (Components) GetSchemasName

func (c Components) GetSchemasName() string

func (Components) GetSecuritySchemesName

func (c Components) GetSecuritySchemesName() string

func (*Components) SetSecurityScheme

func (c *Components) SetSecurityScheme(name string, scheme SecurityScheme)

type Config

type Config struct {
	Web struct {
		Prefix string `yaml:"prefix"`
	} `yaml:"web"`
}

Config 配置参数

type Contact

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

type Content

type Content struct {
	Type   string   `json:"type,omitempty"`
	Schema Property `json:"schema,omitempty"`
}

type CreatePackageOpt

type CreatePackageOpt struct {
	NeedApi    bool
	NeedMethod bool
}

type ExternalDocs

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

type Field

type Field struct {
	Name      string  `json:"name"`      //名称
	Type      string  `json:"type"`      //类型
	Tag       string  `json:"tag"`       //tag
	In        string  `json:"in"`        //存在类型 path 路径 json json对象内
	ParamName string  `json:"paramName"` //参数名
	Validate  string  `json:"validate"`  //验证
	Pkg       string  `json:"Pkg"`       //包名 类型为结构体时
	PkgPath   string  `json:"pkgPath"`   //包路径 类型为结构体时
	Comment   string  `json:"comment"`   //注释
	Array     bool    `json:"array"`     //是否数组
	Ptr       bool    `json:"ptr"`       //是否指针
	MapInfo   MapInfo `json:"mapInfo"`   //map类型键值信息
	Struct    *Struct `json:"struct"`    //是结构体时
}

func GetExprInfo

func GetExprInfo(expr ast.Expr) Field

func GetFieldInfo

func GetFieldInfo(field *ast.Field) Field

func (*Field) GetOpenApiIn

func (f *Field) GetOpenApiIn() string

func (*Field) GetOpenApiType

func (f *Field) GetOpenApiType() string

func (*Field) GetType

func (f *Field) GetType() string

func (Field) IsRequired

func (f Field) IsRequired() bool

func (Field) ToParameter

func (f Field) ToParameter() Parameter

func (Field) ToProperty

func (f Field) ToProperty(storey, deep int) []Property

ToProperty 找到属性 deep 递归层数 max递归深度

type Fields

type Fields []Field

func (*Fields) ToProperty

func (f *Fields) ToProperty(storey, deep int) []Property

ToProperty 找到属性 deep 递归层数 max递归深度

type Flow

type Flow struct {
	AuthorizationUrl string            `json:"authorizationUrl,omitempty"`
	TokenUrl         string            `json:"tokenUrl,omitempty"`
	Scope            map[string]string `json:"scope,omitempty"`
}
type Header struct {
	Description string   `json:"description,omitempty"`
	Schema      Property `json:"schema,omitempty"`
}

type Info

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

type License

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

type MapField

type MapField struct {
	Type    string  `json:"type"`    //类型
	Pkg     string  `json:"Pkg"`     //包名 类型为结构体时
	PkgPath string  `json:"pkgPath"` //包路径 类型为结构体时
	Struct  *Struct `json:"struct"`  //是结构体时
}

type MapInfo

type MapInfo struct {
	Key   MapField `json:"key"`
	Value MapField `json:"value"`
}

type Method

type Method struct {
	Tags        []string              `json:"tags,omitempty"`
	Summary     string                `json:"summary,omitempty"`
	Description string                `json:"description,omitempty"`
	OperationId string                `json:"operationId,omitempty"`
	Parameters  []Parameter           `json:"parameters,omitempty"`
	RequestBody RequestBody           `json:"requestBody,omitempty"`
	Responses   map[string]Response   `json:"responses,omitempty"`
	Security    []map[string][]string `json:"security,omitempty"`
	// contains filtered or unexported fields
}

type MethodMap

type MethodMap struct {
	Idx   int
	Paths []string
}

type OpenAPI

type OpenAPI struct {
	Openapi    string      `json:"openapi,omitempty"`
	Info       Info        `json:"info,omitempty"`
	Servers    []Server    `json:"servers,omitempty"`
	Tags       []Tag       `json:"tags,omitempty"`
	Paths      ApiPathsMap `json:"paths,omitempty"`
	Components Components  `json:"components,omitempty"`
}

type Package

type Package struct {
	Name    string             //包名
	Path    string             //包路径
	ModPath string             //模块路径
	Structs map[string]*Struct //所有结构体信息
	// contains filtered or unexported fields
}

func (*Package) FindConfig

func (pkg *Package) FindConfig()

func (*Package) FindPkgApi

func (pkg *Package) FindPkgApi()

func (*Package) FindPkgStruct

func (pkg *Package) FindPkgStruct()

FindPkgStruct 设置包内的结构体

func (*Package) GetAstPkg

func (pkg *Package) GetAstPkg() *ast.Package

type Packages

type Packages []Package

func (*Packages) DeepFindParams

func (pkgs *Packages) DeepFindParams(idx int, paths []string, field Field) *Struct

DeepFindParams 深度查找参数

func (*Packages) FillPkgRelationStruct

func (pkgs *Packages) FillPkgRelationStruct()

FillPkgRelationStruct 设置包内的关联结构体

func (*Packages) FindInMethodMapParams

func (pkgs *Packages) FindInMethodMapParams(sct *Struct)

FindInMethodMapParams 从调用链获取返回参数

func (*Packages) FindStruct

func (pkgs *Packages) FindStruct(path, pkg, name string) Struct

func (*Packages) FindStructPtr

func (pkgs *Packages) FindStructPtr(path, pkg, name string) *Struct

func (*Packages) GenPackage

func (pkgs *Packages) GenPackage(pkgPath, path string) []*Package

func (*Packages) GetApi

func (pkgs *Packages) GetApi() OpenAPI

GetApi 获取所有API定义

func (*Packages) Init

func (pkgs *Packages) Init(base string)

func (*Packages) InitPackages

func (pkgs *Packages) InitPackages(base string)

type Parameter

type Parameter struct {
	Name            string   `json:"name,omitempty"`
	In              string   `json:"in,omitempty"`
	Description     string   `json:"description,omitempty"`
	Required        bool     `json:"required,omitempty"`
	AllowEmptyValue bool     `json:"allowEmptyValue,omitempty"`
	Deprecated      bool     `json:"deprecated,omitempty"`
	Schema          Property `json:"schema,omitempty"`
	Style           string   `json:"style,omitempty"`
}

type Property

type Property struct {
	Name        string `json:"-"`
	Type        string `json:"type,omitempty"`
	Description string `json:"description,omitempty"`
	Format      string `json:"format,omitempty"`
	Ref         string `json:"$ref,omitempty"`

	Required   []string            `json:"required,omitempty"`
	Items      *Property           `json:"items,omitempty"` //数组
	File       *Property           `json:"file,omitempty"`
	Properties map[string]Property `json:"properties,omitempty"`
	Enum       []string            `json:"enum,omitempty"`
	// contains filtered or unexported fields
}

func (*Property) FillRequired

func (p *Property) FillRequired()

FillRequired 遍历找出所有必传字段

type RequestBody

type RequestBody struct {
	Description string             `json:"description,omitempty"`
	Required    bool               `json:"required,omitempty"`
	Content     map[string]Content `json:"content,omitempty"`
	Ref         string             `json:"$ref,omitempty"`
}

type Response

type Response struct {
	Description string             `json:"description,omitempty"`
	Content     map[string]Content `json:"content,omitempty"`
	Headers     map[string]Header  `json:"headers,omitempty"`
}

type SecurityScheme

type SecurityScheme struct {
	Type         string            `json:"type,omitempty"`
	Scheme       string            `json:"scheme,omitempty"`
	BearerFormat string            `json:"bearerFormat,omitempty"`
	Description  string            `json:"description,omitempty"`
	In           string            `json:"in,omitempty"`
	Flows        []map[string]Flow `json:"flows,omitempty"`
}

type Server

type Server struct {
	Url         string `json:"url,omitempty"`
	Description string `json:"description,omitempty"`
}

type Struct

type Struct struct {
	Name      string            `json:"name"`      //名称
	Des       string            `json:"des"`       //描述
	Type      string            `json:"type"`      //结构体类型
	Field     string            `json:"field"`     //所需字段 有时可能需要的是结构体中的字段 如:rsp.List
	Fields    Fields            `json:"fields"`    //字段
	MethodMap *MethodMap        `json:"methodMap"` //map[参数位置][]链路 方法的返回值类型查找
	Imports   map[string]string //包下所有导入的包信息
	Methods   []StructMethod    `json:"methods"` //方法列表 包括接口的方法
	Api       []Api             //所有API接口信息
	Pkg       *Package
}

func NewStruct

func NewStruct(pkg *Package) Struct

func (Struct) Copy

func (sct Struct) Copy() Struct

func (*Struct) FieldFromAstField

func (sct *Struct) FieldFromAstField(fd *ast.Field) Field

FieldFromAstField 寻找基础的结构体字段定义

func (*Struct) FieldFromInterfaceAstField

func (sct *Struct) FieldFromInterfaceAstField(fd *ast.Field) Field

func (*Struct) GetField

func (sct *Struct) GetField() *Field

func (*Struct) GetInterfaceMethodField

func (sct *Struct) GetInterfaceMethodField(it *ast.Field) StructMethod

GetInterfaceMethodField 寻找接口字段定义

func (*Struct) GetStructFromAstInterfaceType

func (sct *Struct) GetStructFromAstInterfaceType(s *ast.InterfaceType)

func (*Struct) GetStructFromAstStructType

func (sct *Struct) GetStructFromAstStructType(s *ast.StructType)

func (*Struct) GetStructMethodFuncType

func (sct *Struct) GetStructMethodFuncType(ft *ast.FuncType) StructMethod

GetStructMethodFuncType 寻找方法参数定义

func (Struct) ToProperty

func (sct Struct) ToProperty() Property

ToProperty 找到属性 deep 递归层数 max递归深度

type StructMethod

type StructMethod struct {
	Name    string  //名称
	Pkg     string  //包名
	PkgPath string  //包路径
	Args    []Field //参数
	Returns []Field //返回值
}

type Tag

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

Jump to

Keyboard shortcuts

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