openapi

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractAllModuleNames

func ExtractAllModuleNames(openapi *OpenAPI3) ([]string, error)

ExtractAllModuleNames 从OpenAPI文档中提取所有模块名

func GenerateFieldName

func GenerateFieldName(name string) string

GenerateFieldName 生成字段名

func GenerateGoType

func GenerateGoType(schema *Schema, openapi *OpenAPI3) string

GenerateGoType 生成Go类型

func GenerateGoTypeFromParam

func GenerateGoTypeFromParam(param Parameter) string

GenerateGoTypeFromParam 从参数生成Go类型

func GenerateGoTypeFromSchema

func GenerateGoTypeFromSchema(schema *Schema) string

GenerateGoTypeFromSchema 从Schema生成Go类型

func GenerateHandlerName

func GenerateHandlerName(op APIOperation) string

GenerateHandlerName 生成处理器名

func GenerateJSONTag

func GenerateJSONTag(fieldName string) string

GenerateJSONTag 生成JSON标签

func GenerateMethodName

func GenerateMethodName(op APIOperation) string

GenerateMethodName 生成方法名

func GenerateRoute

func GenerateRoute(op APIOperation, baseRoute string) string

GenerateRoute 生成路由

func GenerateValidationRules

func GenerateValidationRules(schema *Schema) string

GenerateValidationRules 生成验证规则

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase 转换为蛇形命名

Types

type APIModule

type APIModule struct {
	Name          string
	Tag           string
	Operations    []APIOperation
	Schemas       map[string]*Schema
	Parameters    map[string]*Parameter
	RequestBodies map[string]*RequestBody
}

API模块信息

func GenerateFromOpenAPI

func GenerateFromOpenAPI(openapi *OpenAPI3, moduleName string) (*APIModule, error)

GenerateFromOpenAPI 从OpenAPI3文档生成API模块

type APIOperation

type APIOperation struct {
	Method      string
	Path        string
	Summary     string
	Description string
	OperationID string
	Parameters  []Parameter
	RequestBody *RequestBody
	Responses   map[string]Response
	Tag         string
	XErrorCodes []ErrorCode
	// 生成相关字段
	MethodName            string
	ResponseData          *ResponseData
	HasRequestBodyOrQuery bool
	HasPathParams         bool
	QueryParameters       []Parameter
}

type Components

type Components struct {
	Schemas       map[string]*Schema      `yaml:"schemas,omitempty" json:"schemas,omitempty"`
	Parameters    map[string]*Parameter   `yaml:"parameters,omitempty" json:"parameters,omitempty"`
	Responses     map[string]*Response    `yaml:"responses,omitempty" json:"responses,omitempty"`
	RequestBodies map[string]*RequestBody `yaml:"requestBodies,omitempty" json:"requestBodies,omitempty"`
}

type ErrorCategory

type ErrorCategory struct {
	Key        string `yaml:"key" json:"key"`
	Name       string `yaml:"name" json:"name"`
	HTTPStatus int    `yaml:"http_status" json:"http_status"`
}

ErrorCategory 错误类别

type ErrorCode

type ErrorCode struct {
	Code       int    `yaml:"code" json:"code"`
	Message    string `yaml:"message" json:"message"`
	HTTPStatus int    `yaml:"http_status,omitempty" json:"http_status,omitempty"`
}

ErrorCode 错误码

type ErrorCodeMeta

type ErrorCodeMeta struct {
	Name             string `yaml:"name" json:"name"`
	Version          string `yaml:"version" json:"version"`
	UpdatedAt        string `yaml:"updated_at" json:"updated_at"`
	Pattern          string `yaml:"pattern" json:"pattern"`
	PatternDesc      string `yaml:"pattern_desc" json:"pattern_desc"`
	HTTPStatusPolicy string `yaml:"http_status_policy" json:"http_status_policy"`
}

ErrorCodeMeta 错误码元信息

type ErrorService

type ErrorService struct {
	Key  string `yaml:"key" json:"key"`
	Name string `yaml:"name" json:"name"`
}

ErrorService 错误服务

type Field

type Field struct {
	Name            string
	GoType          string
	FieldName       string
	Description     string
	Required        bool
	ValidationRules string
}

func GenerateField

func GenerateField(name string, schema *Schema, required bool) Field

GenerateField 生成字段信息

type Info

type Info struct {
	Title       string `yaml:"title" json:"title"`
	Description string `yaml:"description,omitempty" json:"description,omitempty"`
	Version     string `yaml:"version" json:"version"`
}

type MediaType

type MediaType struct {
	Schema *Schema `yaml:"schema,omitempty" json:"schema,omitempty"`
}

type OpenAPI3

type OpenAPI3 struct {
	OpenAPI     string              `yaml:"openapi" json:"openapi"`
	Info        Info                `yaml:"info" json:"info"`
	Servers     []Server            `yaml:"servers,omitempty" json:"servers,omitempty"`
	Paths       map[string]PathItem `yaml:"paths" json:"paths"`
	Components  Components          `yaml:"components,omitempty" json:"components,omitempty"`
	Tags        []Tag               `yaml:"tags,omitempty" json:"tags,omitempty"`
	XErrorCodes *XErrorCodes        `yaml:"x-error-codes,omitempty" json:"x-error-codes,omitempty"`
}

OpenAPI3 结构定义

func ParseOpenAPI3

func ParseOpenAPI3(filePath string) (*OpenAPI3, error)

ParseOpenAPI3 解析OpenAPI3文档

type Operation

type Operation struct {
	Tags        []string            `yaml:"tags,omitempty" json:"tags,omitempty"`
	Summary     string              `yaml:"summary,omitempty" json:"summary,omitempty"`
	Description string              `yaml:"description,omitempty" json:"description,omitempty"`
	OperationID string              `yaml:"operationId,omitempty" json:"operationId,omitempty"`
	Parameters  []Parameter         `yaml:"parameters,omitempty" json:"parameters,omitempty"`
	RequestBody *RequestBody        `yaml:"requestBody,omitempty" json:"requestBody,omitempty"`
	Responses   map[string]Response `yaml:"responses" json:"responses"`
	XErrorCodes []ErrorCode         `yaml:"x-error-codes,omitempty" json:"x-error-codes,omitempty"`
}

type Parameter

type Parameter struct {
	Name        string  `yaml:"name" json:"name"`
	In          string  `yaml:"in" json:"in"`
	Description string  `yaml:"description,omitempty" json:"description,omitempty"`
	Required    bool    `yaml:"required,omitempty" json:"required,omitempty"`
	Schema      *Schema `yaml:"schema,omitempty" json:"schema,omitempty"`
	Ref         string  `yaml:"$ref,omitempty" json:"$ref,omitempty"`
	// 生成相关字段
	GoType          string `yaml:"-" json:"-"`
	FieldName       string `yaml:"-" json:"-"`
	ValidationRules string `yaml:"-" json:"-"`
}

type PathItem

type PathItem struct {
	Get    *Operation `yaml:"get,omitempty" json:"get,omitempty"`
	Post   *Operation `yaml:"post,omitempty" json:"post,omitempty"`
	Put    *Operation `yaml:"put,omitempty" json:"put,omitempty"`
	Delete *Operation `yaml:"delete,omitempty" json:"delete,omitempty"`
	Patch  *Operation `yaml:"patch,omitempty" json:"patch,omitempty"`
}

type RequestBody

type RequestBody struct {
	Description string               `yaml:"description,omitempty" json:"description,omitempty"`
	Required    bool                 `yaml:"required,omitempty" json:"required,omitempty"`
	Content     map[string]MediaType `yaml:"content" json:"content"`
	Ref         string               `yaml:"$ref,omitempty" json:"$ref,omitempty"`
	// 生成相关字段
	Fields []Field `yaml:"-" json:"-"`
}

type Response

type Response struct {
	Description string               `yaml:"description" json:"description"`
	Content     map[string]MediaType `yaml:"content,omitempty" json:"content,omitempty"`
	Ref         string               `yaml:"$ref,omitempty" json:"$ref,omitempty"`
}

type ResponseData

type ResponseData struct {
	GoType      string
	Description string
	Fields      []Field
}

type Schema

type Schema struct {
	Type        string             `yaml:"type,omitempty" json:"type,omitempty"`
	Format      string             `yaml:"format,omitempty" json:"format,omitempty"`
	Description string             `yaml:"description,omitempty" json:"description,omitempty"`
	Properties  map[string]*Schema `yaml:"properties,omitempty" json:"properties,omitempty"`
	Items       *Schema            `yaml:"items,omitempty" json:"items,omitempty"`
	Required    []string           `yaml:"required,omitempty" json:"required,omitempty"`
	Ref         string             `yaml:"$ref,omitempty" json:"$ref,omitempty"`
	Default     interface{}        `yaml:"default,omitempty" json:"default,omitempty"`
	// OpenAPI 3.1 组合模式支持
	AllOf []*Schema `yaml:"allOf,omitempty" json:"allOf,omitempty"`
	OneOf []*Schema `yaml:"oneOf,omitempty" json:"oneOf,omitempty"`
	AnyOf []*Schema `yaml:"anyOf,omitempty" json:"anyOf,omitempty"`
	Not   *Schema   `yaml:"not,omitempty" json:"not,omitempty"`
	// OpenAPI 3.1 新增字段
	Nullable *bool    `yaml:"nullable,omitempty" json:"nullable,omitempty"`
	Enum     []string `yaml:"enum,omitempty" json:"enum,omitempty"`
	Const    string   `yaml:"const,omitempty" json:"const,omitempty"`
	Examples []string `yaml:"examples,omitempty" json:"examples,omitempty"`
	// 验证相关字段
	MinLength *int     `yaml:"minLength,omitempty" json:"minLength,omitempty"`
	MaxLength *int     `yaml:"maxLength,omitempty" json:"maxLength,omitempty"`
	Pattern   string   `yaml:"pattern,omitempty" json:"pattern,omitempty"`
	Minimum   *float64 `yaml:"minimum,omitempty" json:"minimum,omitempty"`
	Maximum   *float64 `yaml:"maximum,omitempty" json:"maximum,omitempty"`
	MinItems  *int     `yaml:"minItems,omitempty" json:"minItems,omitempty"`
	MaxItems  *int     `yaml:"maxItems,omitempty" json:"maxItems,omitempty"`
}

func ResolveSchemaRef

func ResolveSchemaRef(ref string, openapi *OpenAPI3) *Schema

ResolveSchemaRef 解析Schema引用

type Server

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

type Tag

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

type XErrorCodes

type XErrorCodes struct {
	Meta       ErrorCodeMeta            `yaml:"meta" json:"meta"`
	Categories map[string]ErrorCategory `yaml:"categories" json:"categories"`
	Services   map[string]ErrorService  `yaml:"services" json:"services"`
}

XErrorCodes 错误码注册表

Jump to

Keyboard shortcuts

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