返回数据支持是否严格模式的校验, 严格模式下, 必须返回结构体或者map
This commit is contained in:
parent
e95061a1a8
commit
1b1964881f
2
go.mod
2
go.mod
@ -6,7 +6,7 @@ toolchain go1.23.1
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250122075709-5ecf3edb4a00
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20240930081343-1e7f84ed8465
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250207091724-ca151fbc1f06
|
||||
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20241125083316-eab7bab9d7ad
|
||||
git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd
|
||||
|
4
go.sum
4
go.sum
@ -6,6 +6,10 @@ git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20240930081343-1e7f84ed8465 h1:j5EB0hamTMT5fY+xmjJ51oBvll+vS2inNPi+3/UBj60=
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20240930081343-1e7f84ed8465/go.mod h1:Voc8J4ordx7nuMWpgACXXZULQy7ZIuBzcEIoS8VnDIw=
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250207091353-907c20662792 h1:BehqU7W+FF39xCcrbrlPBggbKVTIguDDXfABn8l5RB4=
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250207091353-907c20662792/go.mod h1:Voc8J4ordx7nuMWpgACXXZULQy7ZIuBzcEIoS8VnDIw=
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250207091724-ca151fbc1f06 h1:XsjGMkBCi93h56oCg5Lrz5zVpUxify/CQVhQU9+qLWM=
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250207091724-ca151fbc1f06/go.mod h1:Voc8J4ordx7nuMWpgACXXZULQy7ZIuBzcEIoS8VnDIw=
|
||||
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20241125083316-eab7bab9d7ad h1:6BI3QiDI64SlER1006UJbTJyOCXxB8KCmCK+Kr7FzQo=
|
||||
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20241125083316-eab7bab9d7ad/go.mod h1:+jPQTyCEQqMWhq4p1LowQWq15emisON+++87ArTgwNA=
|
||||
git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442 h1:1eBf0C0gdpBQOqjTK3UCw/mwzQ/SCodx3iTQtidx9eE=
|
||||
|
@ -8,7 +8,6 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
@ -83,7 +82,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
return
|
||||
}
|
||||
// 第一个参数必须是 *gin.Context
|
||||
if methodType.In(1).String() != "*gin.Context" {
|
||||
if methodType.In(1).String() != GinContextType {
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
@ -100,22 +99,28 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
}
|
||||
|
||||
// 解析第一个返回值, 要求必须是结构体或者是map
|
||||
/*if methodType.Out(0).Kind() != reflect.Struct && methodType.Out(0).Kind() != reflect.Map {
|
||||
outputStrictModel := metaField.Tag.Get(TagNameOutputStrict)
|
||||
// 输出是否严格模式
|
||||
cfg.OutputStrict = outputStrictModel == "1" || outputStrictModel == "true"
|
||||
if cfg.OutputStrict {
|
||||
// 开启输出严格模式校验
|
||||
if methodType.Out(0).Kind() != reflect.Struct && methodType.Out(0).Kind() != reflect.Map {
|
||||
needRegister = false
|
||||
return
|
||||
}*/
|
||||
if methodType.Out(1).Kind().String() != "error" {
|
||||
}
|
||||
}
|
||||
|
||||
if methodType.Out(1).Kind().String() != ErrorType {
|
||||
// 判断是否是实现 error接口的方法
|
||||
outputErrParse := false
|
||||
for j := 0; j < methodType.Out(1).NumMethod(); j++ {
|
||||
if methodType.Out(1).Method(j).Name == "Error" && // 实现Error方法
|
||||
if methodType.Out(1).Method(j).Name == ErrorInterfaceFuncName && // 实现Error方法
|
||||
methodType.Out(1).Method(j).Type.NumIn() == 0 && // 没有任何参数
|
||||
methodType.Out(1).Method(j).Type.NumOut() == 1 && // 一个返回值
|
||||
methodType.Out(1).Method(j).Type.Out(0).Kind().String() == reflect.String.String() {
|
||||
outputErrParse = true
|
||||
break
|
||||
}
|
||||
fmt.Println(methodType.Out(1).Method(j).Name, methodType.Out(1).Method(j).Type.NumIn(), methodType.Out(1).Method(j).Type.NumOut(), methodType.Out(1).Method(j).Type.Out(0).Kind().String())
|
||||
}
|
||||
if !outputErrParse {
|
||||
needRegister = false
|
||||
|
@ -12,6 +12,9 @@ import "reflect"
|
||||
const (
|
||||
PrefixFuncName = "RouterPrefix" // 路由前缀函数名称
|
||||
MiddlewareFuncName = "RouterMiddleware" // 路由中间件函数名称
|
||||
GinContextType = "*gin.Context" // gin context 类型名称
|
||||
ErrorType = "error" // error类型
|
||||
ErrorInterfaceFuncName = "Error" // error接口需要实现的方法名称
|
||||
)
|
||||
|
||||
const (
|
||||
@ -19,7 +22,7 @@ const (
|
||||
TagNameMethod = "method" // 接口的请求方法
|
||||
TagNameUriTag = "tag" // 接口的tag
|
||||
TagNameDesc = "desc" // 接口的描述
|
||||
TagNameStrict = "strict" // 接口是否为严格模式 : 不配置, 则为严格模式.严格模式 : POST 仅解析 BODY , GET 仅解析 QUERY
|
||||
TagNameOutputStrict = "output_strict" // 接口数据是否为严格模式 : 严格模式, 响应数据必须是结构体/map,非严格模式返回任意值
|
||||
TagNameBinding = "binding" // gin 内置的验证规则tag
|
||||
TagNameValidate = "validate" // validator v10 默认的验证规则tag
|
||||
TagNameErrMsg = "err" // 验证失败错误信息tag
|
||||
@ -35,7 +38,7 @@ type UriConfig struct {
|
||||
RequestMethod string `json:"request_method"` // 接口请求方法, 必须配置
|
||||
TagList []string `json:"tag_list"` // 接口分组
|
||||
Desc string `json:"desc"` // 接口描述
|
||||
Strict bool `json:"strict"` // 接口是否为严格模式 : 不配置, 则为严格模式.严格模式 : POST 仅解析 BODY , GET 仅解析 QUERY
|
||||
OutputStrict bool `json:"output_strict"` // 接口是否为严格模式 : 不配置,可返回任意类型, 配置, 必须返回结构体或者map
|
||||
ParamList []UriParam `json:"param_list"` // 参数信息表
|
||||
FormDataType reflect.Type `json:"-"` // 表单数据类型
|
||||
ApiStructValue reflect.Value `json:"-"` // 逻辑函数所属结构体取值
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// RequestHandler 获取请求处理方法 TODO : 待完成
|
||||
// RequestHandler 获取请求处理方法
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
|
@ -127,7 +127,7 @@ func parseUriConfig(methodType reflect.Type, routerPrefix string) (*UriConfig, e
|
||||
RequestMethod: strings.ToUpper(metaField.Tag.Get(TagNameMethod)),
|
||||
TagList: strings.Split(metaField.Tag.Get(TagNameUriTag), "|"),
|
||||
Desc: metaField.Tag.Get(TagNameDesc),
|
||||
Strict: wrapper.ArrayType([]string{"", "true"}).Has(strings.ToLower(metaField.Tag.Get(TagNameStrict))) >= 0,
|
||||
OutputStrict: wrapper.ArrayType([]string{"", "true"}).Has(strings.ToLower(metaField.Tag.Get(TagNameOutputStrict))) >= 0,
|
||||
FormDataType: methodType.In(2).Elem(),
|
||||
}
|
||||
// 校验 FormDataType
|
||||
|
Loading…
x
Reference in New Issue
Block a user