openapi格式的文档基础生成 #3
25
generate.go
25
generate.go
@ -278,19 +278,13 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
schemaName := strings.ReplaceAll(pkgPath+"."+inputName, "/", "-")
|
||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||
s := &define.Schema{
|
||||
Nullable: false,
|
||||
Discriminator: nil,
|
||||
ReadOnly: false,
|
||||
WriteOnly: false,
|
||||
Xml: nil,
|
||||
ExternalDocs: nil,
|
||||
Example: "",
|
||||
Deprecated: false,
|
||||
Properties: make(map[string]*define.Property),
|
||||
Required: make([]string, 0),
|
||||
Enum: make([]any, 0),
|
||||
Type: consts.SwaggerDataTypeObject,
|
||||
Ref: g.getSchemaRef(schemaName),
|
||||
Nullable: false,
|
||||
Deprecated: false,
|
||||
Properties: make(map[string]*define.Property),
|
||||
Required: make([]string, 0),
|
||||
Enum: make([]any, 0),
|
||||
Type: consts.SwaggerDataTypeObject, // TODO : 区分数组
|
||||
Ref: g.getSchemaRef(schemaName),
|
||||
}
|
||||
if len(rootSchemaName) == 0 || inputType.Kind() == reflect.Struct {
|
||||
s.Ref = ""
|
||||
@ -328,6 +322,11 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
}
|
||||
// g.docData.Components.Schemas[schemaName].Ref = consts.SwaggerDataTypeObject
|
||||
for i := 0; i < inputType.NumField(); i++ {
|
||||
if ValidateRule.IsRequired(inputType.Field(i)) {
|
||||
// 必传字段
|
||||
g.docData.Components.Schemas[schemaName].Required = append(g.docData.Components.Schemas[schemaName].Required, ParseStructField.GetParamName(inputType.Field(i)))
|
||||
}
|
||||
|
||||
if inputType.Field(i).Type.Kind() == reflect.Ptr ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Map ||
|
||||
|
@ -24,12 +24,12 @@ import (
|
||||
// Date : 17:55 2024/7/19
|
||||
func Test_parser_Openapi3(t *testing.T) {
|
||||
type User struct {
|
||||
Name string `json:"name" d:"zhang" desc:"用户姓名"`
|
||||
Age int `json:"age" d:"18" desc:"年龄"`
|
||||
Name string `json:"name" d:"zhang" desc:"用户姓名" binding:"required"`
|
||||
Age int `json:"age" d:"18" desc:"年龄" binding:"required"`
|
||||
}
|
||||
type List struct {
|
||||
Total int64 `json:"total"`
|
||||
UserList []User `json:"user_list"`
|
||||
UserList []User `json:"user_list" binding:"required"`
|
||||
}
|
||||
var l List
|
||||
g := NewOpenapiDoc(nil, nil)
|
||||
|
@ -71,3 +71,18 @@ func (psf parseStructField) GetDefaultValue(structField reflect.StructField) str
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetValidateRule 获取验证规则
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:30 2025/2/13
|
||||
func (psf parseStructField) GetValidateRule(structField reflect.StructField) string {
|
||||
defaultTagList := []string{define.TagValidate, define.TagBinding}
|
||||
for _, tag := range defaultTagList {
|
||||
if tagVal, exist := structField.Tag.Lookup(tag); exist && len(tagVal) > 0 {
|
||||
return tagVal
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
55
validateRule.go
Normal file
55
validateRule.go
Normal file
@ -0,0 +1,55 @@
|
||||
// Package api_doc ...
|
||||
//
|
||||
// Description : api_doc ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-02-13 15:26
|
||||
package api_doc
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ValidateRule = validateRule{}
|
||||
)
|
||||
|
||||
type validateRule struct{}
|
||||
|
||||
// IsRequired 判断是否必传
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:32 2025/2/13
|
||||
func (r validateRule) IsRequired(structField reflect.StructField) bool {
|
||||
ruleTable := r.getValidateRuleTable(structField)
|
||||
_, exist := ruleTable[consts.ValidatorRuleCommonRequired.String()]
|
||||
// 存在即为必传
|
||||
return exist
|
||||
}
|
||||
|
||||
// getValidateRuleTable 解析验证规则表
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:29 2025/2/13
|
||||
func (r validateRule) getValidateRuleTable(structField reflect.StructField) map[string]string {
|
||||
res := map[string]string{}
|
||||
ruleStr := ParseStructField.GetValidateRule(structField)
|
||||
if len(ruleStr) == 0 {
|
||||
return res
|
||||
}
|
||||
expressList := strings.Split(ruleStr, ",")
|
||||
for _, item := range expressList {
|
||||
if strings.Contains(item, "=") {
|
||||
arr := strings.Split(item, "=")
|
||||
res[strings.TrimSpace(arr[0])] = strings.Join(arr[1:], "=")
|
||||
} else {
|
||||
res[strings.TrimSpace(item)] = ""
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user