找到反射获取数组每一项数据类型的方法

This commit is contained in:
白茶清欢 2025-02-08 21:27:40 +08:00
parent fce39068ca
commit 81975350c1
4 changed files with 24 additions and 11 deletions

View File

@ -197,10 +197,10 @@ func (g *Generate) AddComponentsSchema(pkgPath string, inputType reflect.Type) {
return
}
// 数组
if inputType.Kind() == reflect.Slice {
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
inputType.Comparable()
}
// 结构体
if inputType.Kind() == reflect.Struct {
}
}

2
go.sum
View File

@ -1,5 +1,3 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250207132005-330777d80591 h1:P58+JwVhycrAFqE2Eq25N9y5lDokYBUz+oLxCKk44BE=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250207132005-330777d80591/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250208020330-a50062af46a1 h1:vv4X72I6s6XcTi0ykj2v/cgMZyseFyE2LkS4WloICCs=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250208020330-a50062af46a1/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=

View File

@ -14,6 +14,7 @@ import (
"git.zhangdeman.cn/zhangdeman/serialize"
"os"
"os/user"
"reflect"
"testing"
)
@ -23,6 +24,20 @@ import (
//
// Date : 17:55 2024/7/19
func Test_parser_Openapi3(t *testing.T) {
type A struct {
Name string `json:"name"`
}
type B struct {
List []A `json:"list"`
}
var b *B
tt := reflect.TypeOf(b)
fmt.Println(tt)
var bArr []*B
v := reflect.MakeSlice(reflect.TypeOf(bArr), 1, 1)
fmt.Println(v)
index9 := v.Index(0)
fmt.Println(index9, index9.Type())
current, _ := user.Current()
byteData, _ := os.ReadFile(current.HomeDir + "/Downloads/test-openapi-doc.json")
var data define.OpenapiDoc

View File

@ -54,7 +54,7 @@ func buildSwagger2GlobalSecurityParamList(swaggerDoc *define.Swagger) []*define.
result = append(result, &define.ParamConfig{
Location: GetParamLocation(paramConfig.In).String(),
Path: paramName,
Type: GetDataType(paramConfig.Type, "").String(),
Type: GetDataType(paramConfig.Type, ""),
Title: paramName,
Description: paramConfig.Description,
Required: false,
@ -72,8 +72,8 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) {
uriResult := &define.UriBaseConfig{
Uri: swaggerDoc.BasePath + "/" + strings.TrimLeft(itemUri, "/"),
Method: strings.ToUpper(requestMethod),
ContentType: methodConfig.Consumes[0],
OutputContentType: methodConfig.Produces[0],
ContentType: methodConfig.Consumes,
OutputContentType: methodConfig.Produces,
TagList: methodConfig.Tags,
Summary: methodConfig.Summary,
Description: methodConfig.Description,
@ -120,7 +120,7 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
paramConfigBuildConfig := &define.ParamConfig{
Location: GetParamLocation(paramConfig.In).String(),
Path: paramConfig.Name,
Type: GetDataType(paramConfig.Type, paramConfig.Format).String(),
Type: GetDataType(paramConfig.Type, paramConfig.Format),
Title: paramConfig.Name,
Description: paramConfig.Description,
Required: paramConfig.Required,
@ -143,7 +143,7 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
paramConfigBuildConfig := &define.ParamConfig{
Location: GetParamLocation(paramConfig.In).String(),
Path: paramName,
Type: GetDataType(paramMoreConfig.Type, "").String(),
Type: GetDataType(paramMoreConfig.Type, ""),
Title: paramName,
Description: paramMoreConfig.Description,
Required: requiredTable[paramName],
@ -204,7 +204,7 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, resultConfig map[stri
responseType = consts.DataTypeAny.String()
} else {
schemaType = responseTypeDefine.Schema.Type
responseType = GetDataType(responseTypeDefine.Schema.Type, "").String()
responseType = GetDataType(responseTypeDefine.Schema.Type, "")
}
resCfg := &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
@ -223,7 +223,7 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, resultConfig map[stri
res = append(res, &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
Path: responseKey,
Type: GetDataType(responseKeyConfig.Type, "").String(),
Type: GetDataType(responseKeyConfig.Type, ""),
Title: responseKey,
Description: responseKey,
})