godantic

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RefName              = "$ref"
	RefPrefix            = "#/components/schemas/"
	ArrayTypePrefix      = "ArrayOf" // 对于数组类型,关联到一个新模型
	InnerModelNamePrefix = "godantic."
)
View Source
const (
	ValidationErrorName     string = "ValidationError"
	HttpValidationErrorName string = "HTTPValidationError"
)

Variables

View Source
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 IsArray

func IsArray(object any) bool

IsArray 判断一个对象是否是数组类型

func IsFieldRequired

func IsFieldRequired(tag reflect.StructTag) bool

IsFieldRequired 从tag中判断此字段是否是必须的

func QueryFieldTag

func QueryFieldTag(tag reflect.StructTag, label string, undefined string) string

QueryFieldTag 查找struct字段的Tag

@param	tag			reflect.StructTag	字段的Tag
@param	label		string				要查找的标签
@param	undefined	string				当查找的标签不存在时返回的默认值
@return	string 查找到的标签值, 不存在则返回提供的默认值

func QueryJsonName

func QueryJsonName(tag reflect.StructTag, undefined string) string

QueryJsonName 查询字段定义的json名称

func ReflectObjectType

func ReflectObjectType(obj any) reflect.Type

ReflectObjectType 获取任意对象的类型,若为指针,则反射具体的类型

func StringsToFloats

func StringsToFloats(strs []string) []float64

StringsToFloats 将字符串数组转换成float64数组, 简单实现

@param	strs	[]string	输入字符串数组
@return	[]float64 输出float64数组

func StringsToInts

func StringsToInts(strs []string) []int

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) Copy

func (b *BaseModel) Copy() any

Copy 拷贝一个新的空实例对象

func (*BaseModel) Dict

func (b *BaseModel) Dict(exclude []string, include map[string]any) (m map[string]any)

Dict 将结构体转换为字典视图,并允许过滤一些字段或添加一些键值对到字典中

func (*BaseModel) Exclude

func (b *BaseModel) Exclude(exclude ...string) (m map[string]any)

Exclude 将结构体转换为字典视图,并过滤一些字段

func (*BaseModel) Include

func (b *BaseModel) Include(include map[string]any) (m map[string]any)

Include 将结构体转换为字典视图,并允许添加一些键值对到字典中

func (*BaseModel) IsRequired

func (b *BaseModel) IsRequired() bool

func (*BaseModel) Map

func (b *BaseModel) Map() (m map[string]any)

Map 将结构体转换为字典视图

func (*BaseModel) Metadata

func (b *BaseModel) Metadata() (*Metadata, error)

Metadata 获取反射后的字段元信息, 此字段应慎重使用

func (*BaseModel) ParseRaw

func (b *BaseModel) ParseRaw(stc []byte) []*ValidationError

ParseRaw 从原始字节流中解析结构体对象

func (*BaseModel) Schema

func (b *BaseModel) Schema() (m map[string]any)

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) SchemaDesc

func (b *BaseModel) SchemaDesc() string

SchemaDesc 结构体文档注释

func (*BaseModel) SchemaName

func (b *BaseModel) SchemaName(exclude ...bool) string

SchemaName 获取结构体的名称,默认包含包名

@param	exclude	[]bool	是否排除包名LL

func (*BaseModel) SchemaType

func (b *BaseModel) SchemaType() OpenApiDataType

SchemaType 模型类型

func (*BaseModel) SetId

func (b *BaseModel) SetId(id string)

SetId 设置结构体的唯一标识

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

func (f *Field) InnerSchema() (m map[string]map[string]any)

InnerSchema 内部字段模型文档, 全名:文档

func (*Field) IsArray

func (f *Field) IsArray() bool

IsArray 字段是否是数组类型

func (*Field) IsRequired

func (f *Field) IsRequired() bool

IsRequired 字段是否必须

func (*Field) Metadata

func (f *Field) Metadata() (*Metadata, error)

func (*Field) Schema

func (f *Field) Schema() (m map[string]any)

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) SchemaDesc

func (f *Field) SchemaDesc() string

SchemaDesc 字段注释说明

func (*Field) SchemaName

func (f *Field) SchemaName(exclude ...bool) string

SchemaName swagger文档字段名

func (*Field) SchemaType

func (f *Field) SchemaType() OpenApiDataType

SchemaType 模型类型

func (*Field) SetId

func (f *Field) SetId(id string)

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 缓存

func (*MetaCache) Get

func (m *MetaCache) Get(pkg string) *Metadata

func (*MetaCache) Set

func (m *MetaCache) Set(meta *Metadata)

Set 保存一个元信息,存在则更新

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) InnerModel

func (m *MetaField) InnerModel() *Metadata

InnerModel 获取内部模型

func (*MetaField) IsInnerModel

func (m *MetaField) IsInnerModel() bool

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 GetMetadata

func GetMetadata(pkg string) *Metadata

GetMetadata 获取结构体的元信息

func List

func List(model SchemaIface) *Metadata

func StructReflect

func StructReflect(rt reflect.Type) *Metadata

func (*Metadata) AddField

func (m *Metadata) AddField(field *MetaField)

AddField 添加字段记录

func (*Metadata) AddInnerField

func (m *Metadata) AddInnerField(field *MetaField)

func (*Metadata) Fields

func (m *Metadata) Fields() []*MetaField

Fields 结构体字段

func (*Metadata) Id

func (m *Metadata) Id() string

Id 获取结构体的唯一标识

func (*Metadata) InnerFields

func (m *Metadata) InnerFields() []*MetaField

InnerFields 内部字段

func (*Metadata) IsRequired

func (m *Metadata) IsRequired() bool

IsRequired 字段是否必须

func (*Metadata) Metadata

func (m *Metadata) Metadata() (*Metadata, error)

func (*Metadata) Name

func (m *Metadata) Name() string

Name 获取结构体名称

func (*Metadata) ReflectType

func (m *Metadata) ReflectType() reflect.Type

func (*Metadata) Schema

func (m *Metadata) Schema() map[string]any

Schema 输出为OpenAPI文档模型,字典格式 数组类型: 需要单独处理, 取其 fields 的第一个元素作为子资源素的实际类型 基本数据类型:取其 fields 的第一个元素, description同样取fields 的第一个元素 结构体类型: 需处理全部的 fields 和 innerFields

func (*Metadata) SchemaDesc

func (m *Metadata) SchemaDesc() string

SchemaDesc 结构体文档注释

func (*Metadata) SchemaName

func (m *Metadata) SchemaName(exclude ...bool) string

SchemaName 获取结构体的名称,默认包含包名

func (*Metadata) SchemaType

func (m *Metadata) SchemaType() OpenApiDataType

SchemaType 模型类型

func (*Metadata) String

func (m *Metadata) String() string

String 结构体唯一标识:包名+结构体名称

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

func (q *QModel) InnerSchema() (m map[string]map[string]any)

InnerSchema 内部字段模型文档, 全名:文档

func (*QModel) IsRequired

func (q *QModel) IsRequired() bool

IsRequired 是否必须

func (*QModel) Metadata

func (q *QModel) Metadata() (*Metadata, error)

func (*QModel) Schema

func (q *QModel) Schema() (m map[string]any)

Schema 输出为OpenAPI文档模型,字典格式

{
	"required": true,
	"schema": {
		"title": "names",
		"type": "string",
		"default": "jack"
	},
	"name": "names",
	"in": "query"/"path"
}

func (*QModel) SchemaDesc

func (q *QModel) SchemaDesc() string

SchemaDesc 结构体文档注释

func (*QModel) SchemaName

func (q *QModel) SchemaName(exclude ...bool) string

SchemaName 获取名称,以json字段为准

func (*QModel) SchemaType

func (q *QModel) SchemaType() OpenApiDataType

SchemaType 模型类型

func (*QModel) SetId

func (q *QModel) SetId(id string)

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

Jump to

Keyboard shortcuts

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