openapi格式的文档基础生成 #3

Merged
zhangdeman merged 32 commits from feature/upgrade_swagger into master 2025-02-14 21:13:00 +08:00
4 changed files with 68 additions and 10 deletions
Showing only changes of commit 45a25e0018 - Show all commits

View File

@ -143,8 +143,8 @@ type Schema struct {
type Property struct {
Type string `json:"type,omitempty"` // 数据类型, swagger本身的定义
Format string `json:"format,omitempty"` // 对应编程语言中的数据类型描述
Enum []string `json:"enum,omitempty"` // 枚举值列表
Default string `json:"default,omitempty"` // 默认值 : 不同于 JSON Schema这个值必须符合定义与相同级别的 Schema 对象 中定义的类型,比如 type 是 string那么 default 可以是 "foo" 但不能是 1。
Enum []any `json:"enum,omitempty"` // 枚举值列表
Default any `json:"default,omitempty"` // 默认值 : 不同于 JSON Schema这个值必须符合定义与相同级别的 Schema 对象 中定义的类型,比如 type 是 string那么 default 可以是 "foo" 但不能是 1。
Description string `json:"description,omitempty"` // 数据描述, CommonMark syntax可以被用来呈现富文本格式.
AllOf []*PropertyXOf `json:"allOf,omitempty"` // type 是一个对象, allOf 指向对象描述
OneOf []*PropertyXOf `json:"oneOf,omitempty"` // type 是一个对象, allOf 指向对象描述

View File

@ -25,7 +25,7 @@ import (
func Test_parser_Openapi3(t *testing.T) {
type User struct {
Name string `json:"name" d:"zhang" desc:"用户姓名" binding:"required"`
Age int `json:"age" d:"18" desc:"年龄" binding:"required,oneof:12 13 18 90"`
Age string `json:"age" d:"18" desc:"年龄" binding:"required,oneof=12 13 18 90"`
}
type List struct {
Total int64 `json:"total" binding:"required"`

View File

@ -10,6 +10,8 @@ package api_doc
import (
"git.zhangdeman.cn/gateway/api-doc/define"
"reflect"
"strconv"
"strings"
)
var (
@ -62,14 +64,37 @@ func (psf parseStructField) GetParamDesc(structField reflect.StructField) string
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:05 2025/2/11
func (psf parseStructField) GetDefaultValue(structField reflect.StructField) string {
func (psf parseStructField) GetDefaultValue(structField reflect.StructField) any {
defaultTagList := []string{define.TagD, define.TagDefault}
fieldType := structField.Type.Kind().String()
for _, tag := range defaultTagList {
if tagVal, exist := structField.Tag.Lookup(tag); exist {
return tagVal
if val, exist := structField.Tag.Lookup(tag); exist && val != "" {
if strings.HasPrefix(fieldType, "int") {
i, _ := strconv.Atoi(val)
return i
}
if strings.HasPrefix(fieldType, "uint") {
uintVal, _ := strconv.ParseUint(val, 10, 64)
return uintVal
}
if strings.HasPrefix(fieldType, "float") {
floatVal, _ := strconv.ParseFloat(val, 64)
return floatVal
}
if strings.HasPrefix(fieldType, "string") {
return val
}
if strings.HasPrefix(fieldType, "bool") {
if val == "true" {
return true
} else {
return false
}
}
return val
}
}
return ""
return nil
}
// GetValidateRule 获取验证规则

View File

@ -10,6 +10,7 @@ package api_doc
import (
"git.zhangdeman.cn/zhangdeman/consts"
"reflect"
"strconv"
"strings"
)
@ -36,13 +37,45 @@ func (r validateRule) IsRequired(structField reflect.StructField) bool {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:23 2025/2/13
func (r validateRule) Enum(structField reflect.StructField) []string {
func (r validateRule) Enum(structField reflect.StructField) []any {
ruleTable := r.getValidateRuleTable(structField)
oneOfValue, _ := ruleTable[consts.ValidatorRuleCommonOneOf.String()]
if len(oneOfValue) == 0 {
return []string{}
return []any{}
}
return strings.Split(oneOfValue, " ")
fieldType := structField.Type.Kind().String()
valStrArr := strings.Split(oneOfValue, " ")
anySlice := make([]any, 0)
for _, val := range valStrArr {
if strings.HasPrefix(fieldType, "int") {
i, _ := strconv.Atoi(val)
anySlice = append(anySlice, i)
continue
}
if strings.HasPrefix(fieldType, "uint") {
uintVal, _ := strconv.ParseUint(val, 10, 64)
anySlice = append(anySlice, uintVal)
continue
}
if strings.HasPrefix(fieldType, "float") {
floatVal, _ := strconv.ParseFloat(val, 64)
anySlice = append(anySlice, floatVal)
continue
}
if strings.HasPrefix(fieldType, "string") {
anySlice = append(anySlice, val)
continue
}
if strings.HasPrefix(fieldType, "bool") {
if val == "true" {
anySlice = append(anySlice, true)
} else {
anySlice = append(anySlice, false)
}
continue
}
}
return anySlice
}
// getValidateRuleTable 解析验证规则表