openapi格式的文档基础生成 #3
@ -140,11 +140,11 @@ type Schema struct {
|
|||||||
//
|
//
|
||||||
// Date : 17:05 2024/7/19
|
// Date : 17:05 2024/7/19
|
||||||
type Property struct {
|
type Property struct {
|
||||||
Type string `json:"type"` // 数据类型, swagger本身的定义
|
Type string `json:"type,omitempty"` // 数据类型, swagger本身的定义
|
||||||
Format string `json:"format"` // 对应编程语言中的数据类型描述
|
Format string `json:"format,omitempty"` // 对应编程语言中的数据类型描述
|
||||||
Enum []any `json:"enum,omitempty"` // 枚举值列表
|
Enum []string `json:"enum,omitempty"` // 枚举值列表
|
||||||
Default any `json:"default"` // 默认值 : 不同于 JSON Schema,这个值必须符合定义与相同级别的 Schema 对象 中定义的类型,比如 type 是 string,那么 default 可以是 "foo" 但不能是 1。
|
Default string `json:"default,omitempty"` // 默认值 : 不同于 JSON Schema,这个值必须符合定义与相同级别的 Schema 对象 中定义的类型,比如 type 是 string,那么 default 可以是 "foo" 但不能是 1。
|
||||||
Description string `json:"description"` // 数据描述, CommonMark syntax可以被用来呈现富文本格式.
|
Description string `json:"description,omitempty"` // 数据描述, CommonMark syntax可以被用来呈现富文本格式.
|
||||||
AllOf []*PropertyXOf `json:"allOf,omitempty"` // type 是一个对象, allOf 指向对象描述
|
AllOf []*PropertyXOf `json:"allOf,omitempty"` // type 是一个对象, allOf 指向对象描述
|
||||||
OneOf []*PropertyXOf `json:"oneOf,omitempty"` // type 是一个对象, allOf 指向对象描述
|
OneOf []*PropertyXOf `json:"oneOf,omitempty"` // type 是一个对象, allOf 指向对象描述
|
||||||
AnyOf []*PropertyXOf `json:"anyOf,omitempty"` // type 是一个对象, allOf 指向对象描述
|
AnyOf []*PropertyXOf `json:"anyOf,omitempty"` // type 是一个对象, allOf 指向对象描述
|
||||||
|
24
define/tag.go
Normal file
24
define/tag.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Package define ...
|
||||||
|
//
|
||||||
|
// Description : define ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-02-11 21:51
|
||||||
|
package define
|
||||||
|
|
||||||
|
const (
|
||||||
|
TagJson = "json"
|
||||||
|
TagXml = "xml"
|
||||||
|
TagYaml = "yaml"
|
||||||
|
TagYml = "yml"
|
||||||
|
TagForm = "form"
|
||||||
|
TagBinding = "binding"
|
||||||
|
TagValidate = "validate"
|
||||||
|
TagErr = "err"
|
||||||
|
TagMsg = "msg"
|
||||||
|
TagDesc = "desc"
|
||||||
|
TagDescription = "description"
|
||||||
|
TagD = "d"
|
||||||
|
TagDefault = "default"
|
||||||
|
)
|
57
generate.go
57
generate.go
@ -274,7 +274,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
Type: g.realType2SwaggerType(inputType.String()),
|
Type: g.realType2SwaggerType(inputType.String()),
|
||||||
Format: inputType.String(),
|
Format: inputType.String(),
|
||||||
Enum: nil,
|
Enum: nil,
|
||||||
Default: nil,
|
Default: "",
|
||||||
Description: "",
|
Description: "",
|
||||||
AllOf: nil,
|
AllOf: nil,
|
||||||
OneOf: nil,
|
OneOf: nil,
|
||||||
@ -401,7 +401,7 @@ func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type)
|
|||||||
Type: "",
|
Type: "",
|
||||||
Format: inputType.String(),
|
Format: inputType.String(),
|
||||||
Enum: nil,
|
Enum: nil,
|
||||||
Default: nil,
|
Default: "",
|
||||||
Description: "",
|
Description: "",
|
||||||
AllOf: nil,
|
AllOf: nil,
|
||||||
OneOf: nil,
|
OneOf: nil,
|
||||||
@ -465,3 +465,56 @@ func (g *Generate) realType2SwaggerType(realType string) string {
|
|||||||
}
|
}
|
||||||
return g.realBaseType2SwaggerType(realType)
|
return g.realBaseType2SwaggerType(realType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getParamName 获取参数名称
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 21:58 2025/2/11
|
||||||
|
func (g *Generate) getParamName(structField reflect.StructField) string {
|
||||||
|
paramNameTagList := []string{
|
||||||
|
define.TagJson, define.TagForm,
|
||||||
|
define.TagXml, define.TagYaml,
|
||||||
|
define.TagYml,
|
||||||
|
}
|
||||||
|
for _, tag := range paramNameTagList {
|
||||||
|
tagVal := structField.Tag.Get(tag)
|
||||||
|
if tagVal != "" {
|
||||||
|
return tagVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 未设置相关字段, 则字段名即为参数名
|
||||||
|
return structField.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// getParamDesc ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 22:01 2025/2/11
|
||||||
|
func (g *Generate) getParamDesc(structField reflect.StructField) string {
|
||||||
|
descTagList := []string{define.TagDesc, define.TagDescription}
|
||||||
|
for _, tag := range descTagList {
|
||||||
|
tagVal := structField.Tag.Get(tag)
|
||||||
|
if tagVal != "" {
|
||||||
|
return tagVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 没有显示的设置参数描述, 则使用参数名作为参数描述
|
||||||
|
return g.getParamName(structField)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getDefaultValue 获取默认值
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 22:05 2025/2/11
|
||||||
|
func (g *Generate) getDefaultValue(structField reflect.StructField) string {
|
||||||
|
defaultTagList := []string{define.TagD, define.TagDefault}
|
||||||
|
for _, tag := range defaultTagList {
|
||||||
|
if tagVal, exist := structField.Tag.Lookup(tag); exist {
|
||||||
|
return tagVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user