Documentation ¶
Index ¶
- Constants
- Variables
- func GetDefaultV(tag reflect.StructTag, otype OpenApiDataType) (v any)
- func IsArray(object any) bool
- func IsFieldRequired(tag reflect.StructTag) bool
- func QueryFieldTag(tag reflect.StructTag, label string, undefined string) string
- func QueryJsonName(tag reflect.StructTag, undefined string) string
- func ReflectObjectType(obj any) reflect.Type
- func StringsToFloats(strs []string) []float64
- func StringsToInts(strs []string) []int
- type BaseModel
- func (b *BaseModel) Copy() any
- func (b *BaseModel) Dict(exclude []string, include map[string]any) (m map[string]any)
- func (b *BaseModel) Exclude(exclude ...string) (m map[string]any)
- func (b *BaseModel) Include(include map[string]any) (m map[string]any)
- func (b *BaseModel) IsRequired() bool
- func (b *BaseModel) Map() (m map[string]any)
- func (b *BaseModel) Metadata() (*Metadata, error)
- func (b *BaseModel) ParseRaw(stc []byte) []*ValidationError
- func (b *BaseModel) Schema() (m map[string]any)
- func (b *BaseModel) SchemaDesc() string
- func (b *BaseModel) SchemaName(exclude ...bool) string
- func (b *BaseModel) SchemaType() OpenApiDataType
- func (b *BaseModel) SetId(id string)
- func (b *BaseModel) Validate(stc any) []*ValidationError
- type DictIface
- type Field
- func (f *Field) InnerSchema() (m map[string]map[string]any)
- func (f *Field) IsArray() bool
- func (f *Field) IsRequired() bool
- func (f *Field) Metadata() (*Metadata, error)
- func (f *Field) Schema() (m map[string]any)
- func (f *Field) SchemaDesc() string
- func (f *Field) SchemaName(exclude ...bool) string
- func (f *Field) SchemaType() OpenApiDataType
- func (f *Field) SetId(id string)
- type HTTPValidationError
- func (v *HTTPValidationError) Error() string
- func (v *HTTPValidationError) Schema() map[string]any
- func (v *HTTPValidationError) SchemaDesc() string
- func (v *HTTPValidationError) SchemaName(exclude ...bool) string
- func (v *HTTPValidationError) SchemaType() OpenApiDataType
- func (v *HTTPValidationError) String() string
- type Iface
- type MetaCache
- type MetaField
- type Metadata
- func (m *Metadata) AddField(field *MetaField)
- func (m *Metadata) AddInnerField(field *MetaField)
- func (m *Metadata) Fields() []*MetaField
- func (m *Metadata) Id() string
- func (m *Metadata) InnerFields() []*MetaField
- func (m *Metadata) IsRequired() bool
- func (m *Metadata) Metadata() (*Metadata, error)
- func (m *Metadata) Name() string
- func (m *Metadata) ReflectType() reflect.Type
- func (m *Metadata) Schema() map[string]any
- func (m *Metadata) SchemaDesc() string
- func (m *Metadata) SchemaName(exclude ...bool) string
- func (m *Metadata) SchemaType() OpenApiDataType
- func (m *Metadata) String() string
- type ModelReflect
- type OpenApiDataType
- type QModel
- func (q *QModel) InnerSchema() (m map[string]map[string]any)
- func (q *QModel) IsRequired() bool
- func (q *QModel) Metadata() (*Metadata, error)
- func (q *QModel) Schema() (m map[string]any)
- func (q *QModel) SchemaDesc() string
- func (q *QModel) SchemaName(exclude ...bool) string
- func (q *QModel) SchemaType() OpenApiDataType
- func (q *QModel) SetId(id string)
- type QueryModel
- type QueryParameter
- type SchemaIface
- type ValidationError
Constants ¶
const ( RefName = "$ref" RefPrefix = "#/components/schemas/" ArrayTypePrefix = "ArrayOf" // 对于数组类型,关联到一个新模型 InnerModelNamePrefix = "godantic." )
const ( ValidationErrorName string = "ValidationError" HttpValidationErrorName string = "HTTPValidationError" )
Variables ¶
var ( Int8 = makeMetadata( "Int8", "8位有符号的数字类型", `json:"int8" gte:"-128" lte:"127"`, IntegerType, ) Int16 = makeMetadata( "Int16", "16位有符号的数字类型", `json:"int16" gte:"-32768" lte:"32767"`, IntegerType, ) Int32 = makeMetadata( "Int32", "32位有符号的数字类型", `json:"int32" gte:"-2147483648" lte:"2147483647"`, IntegerType, ) Int64 = makeMetadata( "Int64", "64位有符号的数字类型", `json:"int64" gte:"-9223372036854775808" lte:"9223372036854775807"`, IntegerType, ) Int = makeMetadata( "Int", "有符号的数字类型", `json:"int" gte:"-9223372036854775808" lte:"9223372036854775807"`, IntegerType, ) Uint8 = makeMetadata( "Uint8", "8位无符号的数字类型", `json:"uint8" gte:"0" lte:"255"`, IntegerType, ) Uint16 = makeMetadata( "Uint16", "16位无符号的数字类型", `json:"uint16" gte:"0" lte:"65535"`, IntegerType, ) Uint32 = makeMetadata( "Uint32", "32位无符号的数字类型", `json:"uint32" gte:"0" lte:"4294967295"`, IntegerType, ) Uint64 = makeMetadata( "Uint64", "64位无符号的数字类型", `json:"uint64" gte:"0" lte:"18446744073709551615"`, IntegerType, ) Float32 = makeMetadata( "Float32", "32位的浮点类型", `json:"float32"`, NumberType, ) Float64 = makeMetadata( "Float64", "64位的浮点类型", `json:"float64"`, NumberType, ) Float = makeMetadata( "Float", "64位的浮点类型", `json:"float"`, NumberType, ) String = makeMetadata( "String", "字符串类型", `json:"string" min:"0" max:"255"`, StringType, ) Bool = makeMetadata( "Bool", "布尔类型", `json:"boolean" oneof:"true false"`, BoolType, ) )
Functions ¶
func GetDefaultV ¶
func GetDefaultV(tag reflect.StructTag, otype OpenApiDataType) (v any)
GetDefaultV 从Tag中提取字段默认值
func IsFieldRequired ¶
IsFieldRequired 从tag中判断此字段是否是必须的
func QueryFieldTag ¶
QueryFieldTag 查找struct字段的Tag
@param tag reflect.StructTag 字段的Tag @param label string 要查找的标签 @param undefined string 当查找的标签不存在时返回的默认值 @return string 查找到的标签值, 不存在则返回提供的默认值
func QueryJsonName ¶
QueryJsonName 查询字段定义的json名称
func ReflectObjectType ¶
ReflectObjectType 获取任意对象的类型,若为指针,则反射具体的类型
func StringsToFloats ¶
StringsToFloats 将字符串数组转换成float64数组, 简单实现
@param strs []string 输入字符串数组 @return []float64 输出float64数组
func StringsToInts ¶
StringsToInts 将字符串数组转换成int数组, 简单实现
@param strs []string 输入字符串数组 @return []int 输出int数组
Types ¶
type BaseModel ¶
type BaseModel struct {
// contains filtered or unexported fields
}
BaseModel 基本数据模型, 对于上层的 app.Route 其请求和相应体都应为继承此结构体的结构体 在 OpenApi 文档模型中,此模型的类型始终为 "object"; 对于 BaseModel 其字段仍然可能会 BaseModel
func (*BaseModel) IsRequired ¶
func (*BaseModel) ParseRaw ¶
func (b *BaseModel) ParseRaw(stc []byte) []*ValidationError
ParseRaw 从原始字节流中解析结构体对象
func (*BaseModel) Schema ¶
Schema 输出为OpenAPI文档模型,字典格式
{ "title": "examle.MyTimeslot", "type": "object" "description": "examle.mytimeslot", "required": [], "properties": { "control_timeslot": { "title": "control_timeslot", "type": "array" "description": "控制时隙编号数组", "required": false, "items": { "type": "integer" }, }, "superframe_count": { "title": "superframe_count", "type": "integer" "description": "超帧计数", "required": false, }, }, },
func (*BaseModel) Validate ¶
func (b *BaseModel) Validate(stc any) []*ValidationError
Validate 检验实例是否符合tag要求
type DictIface ¶
type DictIface interface { // Map 将结构体转换为字典视图 Map() (m map[string]any) // Dict 将结构体转换为字典视图,并允许过滤一些字段或添加一些键值对到字典中 Dict(exclude []string, include map[string]any) (m map[string]any) // Exclude 将结构体转换为字典视图,并过滤一些字段 Exclude(exclude ...string) (m map[string]any) // Include 将结构体转换为字典视图,并允许添加一些键值对到字典中 Include(include map[string]any) (m map[string]any) }
type Field ¶
type Field struct { Title string `json:"title,omitempty" description:"字段名称"` Tag reflect.StructTag `json:"tag" description:"字段标签"` Description string `json:"description,omitempty" description:"说明"` ItemRef string `description:"子元素类型, 仅Type=array/object时有效"` OType OpenApiDataType `json:"otype,omitempty" description:"openapi 数据类型"` // contains filtered or unexported fields }
Field 基本数据模型, 此模型不可再分, 同时也是 BaseModel 的字段类型 但此类型不再递归记录,仅记录一个关联模型为基本
func (*Field) InnerSchema ¶
InnerSchema 内部字段模型文档, 全名:文档
func (*Field) Schema ¶
Schema 生成字段的详细描述信息
// 字段为结构体类型 "position_sat": { "title": "position_sat", "type": "object" "description": "position_sat", "required": false, "$ref": "#/comonents/schemas/example.PositionGeo", } // 字段为数组类型, 数组元素为基本类型 "traffic_timeslot": { "title": "traffic_timeslot", "type": "array" "description": "业务时隙编号数组", "required": false, "items": { "type": "integer" }, } // 字段为数组类型, 数组元素为自定义结构体类型 "Detail": { "title": "Detail", "type": "array" "description": "Detail", "required": true, "items": { "$ref": "#/comonents/schemas/ValidationError" }, }
func (*Field) SchemaName ¶
SchemaName swagger文档字段名
type HTTPValidationError ¶
type HTTPValidationError struct { BaseModel Detail []*ValidationError `json:"detail" description:"Detail" binding:"required"` }
func (*HTTPValidationError) Error ¶
func (v *HTTPValidationError) Error() string
func (*HTTPValidationError) Schema ¶
func (v *HTTPValidationError) Schema() map[string]any
func (*HTTPValidationError) SchemaDesc ¶
func (v *HTTPValidationError) SchemaDesc() string
func (*HTTPValidationError) SchemaName ¶
func (v *HTTPValidationError) SchemaName(exclude ...bool) string
func (*HTTPValidationError) SchemaType ¶
func (v *HTTPValidationError) SchemaType() OpenApiDataType
func (*HTTPValidationError) String ¶
func (v *HTTPValidationError) String() string
type Iface ¶
type Iface interface { SchemaIface DictIface // Validate 检验实例是否符合tag要求 Validate(stc any) []*ValidationError // ParseRaw 从原始字节流中解析结构体对象 ParseRaw(stc []byte) []*ValidationError // Copy 拷贝一个新的空实例对象 Copy() any }
type MetaCache ¶
type MetaCache struct {
// contains filtered or unexported fields
}
MetaCache Metadata 缓存
type MetaField ¶
type MetaField struct { RType reflect.Type `description:"反射字段类型"` Field Index int `json:"index" description:"当前字段所处的序号"` Exported bool `json:"exported" description:"是否是导出字段"` Anonymous bool `json:"anonymous" description:"是否是嵌入字段"` }
func (*MetaField) IsInnerModel ¶
IsInnerModel 是否是内部模型,如果是则需要重新生成其文档
type Metadata ¶
type Metadata struct {
// contains filtered or unexported fields
}
Metadata 数据模型 BaseModel 的元信息
func BaseModelToMetadata ¶
func BaseModelToMetadata(model SchemaIface) *Metadata
BaseModelToMetadata 提取基本数据模型的元信息
@param model SchemaIface 基本数据模型 @return *Metadata 基本数据模型的元信息
func List ¶
func List(model SchemaIface) *Metadata
func StructReflect ¶
func (*Metadata) AddInnerField ¶
func (*Metadata) ReflectType ¶
func (*Metadata) Schema ¶
Schema 输出为OpenAPI文档模型,字典格式 数组类型: 需要单独处理, 取其 fields 的第一个元素作为子资源素的实际类型 基本数据类型:取其 fields 的第一个元素, description同样取fields 的第一个元素 结构体类型: 需处理全部的 fields 和 innerFields
func (*Metadata) SchemaName ¶
SchemaName 获取结构体的名称,默认包含包名
type ModelReflect ¶
type ModelReflect struct {
// contains filtered or unexported fields
}
func (*ModelReflect) Metadata ¶
func (m *ModelReflect) Metadata() *Metadata
type OpenApiDataType ¶
type OpenApiDataType string
const ( IntegerType OpenApiDataType = "integer" NumberType OpenApiDataType = "number" StringType OpenApiDataType = "string" BoolType OpenApiDataType = "boolean" ObjectType OpenApiDataType = "object" ArrayType OpenApiDataType = "array" )
func ReflectKindToOType ¶
func ReflectKindToOType(kind reflect.Kind) (name OpenApiDataType)
ReflectKindToOType 转换reflect.Kind为swagger类型说明
@param ReflectKind reflect.Kind 反射类型
type QModel ¶
type QModel struct { Title string `json:"title,omitempty" description:"字段标题"` Name string `json:"name,omitempty" description:"字段名称"` Tag reflect.StructTag `json:"tag,omitempty" description:"TAG"` OType OpenApiDataType `json:"otype,omitempty" description:"openaapi 数据类型"` InPath bool `json:"in_path,omitempty" description:"是否是路径参数"` }
QModel 查询参数或路径参数模型, 此类型会进一步转换为 openapi.Parameter
func ParseToQueryModels ¶
func ParseToQueryModels(q QueryParameter) []*QModel
ParseToQueryModels 将一个结构体转换为 QueryModel
func (*QModel) InnerSchema ¶
InnerSchema 内部字段模型文档, 全名:文档
func (*QModel) Schema ¶
Schema 输出为OpenAPI文档模型,字典格式
{ "required": true, "schema": { "title": "names", "type": "string", "default": "jack" }, "name": "names", "in": "query"/"path" }
func (*QModel) SchemaName ¶
SchemaName 获取名称,以json字段为准
type QueryModel ¶
type QueryModel struct{}
QueryModel 查询参数基类
func (*QueryModel) Fields ¶
func (q *QueryModel) Fields() []*QModel
type QueryParameter ¶
type QueryParameter interface { }
type SchemaIface ¶
type SchemaIface interface { // Schema 输出为OpenAPI文档模型,字典格式 Schema() (m map[string]any) // SchemaName 获取结构体的名称,默认包含包名 SchemaName(exclude ...bool) string // SchemaDesc 结构体文档注释 SchemaDesc() string // SchemaType 模型类型 SchemaType() OpenApiDataType // IsRequired 字段是否必须 IsRequired() bool // Metadata 获取反射后的字段元信息, 允许上层处理 Metadata() (*Metadata, error) }
type ValidationError ¶
type ValidationError struct { BaseModel Ctx map[string]any `json:"service" description:"Service"` Msg string `json:"msg" description:"Message" binding:"required"` Type string `json:"type" description:"Error RType" binding:"required"` Loc []string `json:"loc" description:"Location" binding:"required"` }
ValidationError 参数校验错误
func (*ValidationError) Error ¶
func (v *ValidationError) Error() string
func (*ValidationError) Schema ¶
func (v *ValidationError) Schema() (m map[string]any)
func (*ValidationError) SchemaDesc ¶
func (v *ValidationError) SchemaDesc() string
func (*ValidationError) SchemaName ¶
func (v *ValidationError) SchemaName(exclude ...bool) string
func (*ValidationError) SchemaType ¶
func (v *ValidationError) SchemaType() OpenApiDataType