接口入口函数首个参数支持custom context
This commit is contained in:
parent
a7fed88714
commit
e0d5e9c94b
@ -20,7 +20,7 @@ import (
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Gin *gin.Context // 继承 gin context
|
||||
Context *gin.Context // 继承 gin context
|
||||
Trace *trace.Runtime // trace 实例
|
||||
TraceID string
|
||||
RequestID string
|
||||
@ -53,7 +53,7 @@ func NewContext(ginCtx *gin.Context) *Context {
|
||||
return traceID
|
||||
}
|
||||
ctx := &Context{
|
||||
Gin: ginCtx,
|
||||
Context: ginCtx,
|
||||
Trace: trace.NewRuntime(traceID, 1),
|
||||
TraceID: traceID,
|
||||
RequestID: getRequestID(ginCtx, traceID),
|
||||
|
@ -69,8 +69,9 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
// 第一个参数必须是 *gin.Context
|
||||
if methodType.In(1).String() != GinContextType {
|
||||
// 第一个参数必须是 *gin.Context 或者 *define.Context
|
||||
paramOne := methodType.In(1).String()
|
||||
if paramOne != GinContextType && paramOne != CustomContextType {
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
@ -106,6 +107,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
}
|
||||
}
|
||||
// 解析meta信息
|
||||
cfg.CtxType = paramOne
|
||||
cfg.Path = metaField.Tag.Get(TagNamePath)
|
||||
cfg.RequestMethod = metaField.Tag.Get(TagNameMethod)
|
||||
cfg.Desc = metaField.Tag.Get(TagNameDesc)
|
||||
|
@ -15,6 +15,7 @@ const (
|
||||
PrefixFuncName = "RouterPrefix" // 路由前缀函数名称
|
||||
MiddlewareFuncName = "RouterMiddleware" // 路由中间件函数名称
|
||||
GinContextType = "*gin.Context" // gin context 类型名称
|
||||
CustomContextType = "*define.Context" // custom context 类型名称
|
||||
ErrorType = "error" // error类型
|
||||
ErrorInterfaceFuncName = "Error" // error接口需要实现的方法名称
|
||||
)
|
||||
@ -41,6 +42,7 @@ type UriConfig struct {
|
||||
TagList []string `json:"tag_list"` // 接口分组
|
||||
Desc string `json:"desc"` // 接口描述
|
||||
OutputStrict bool `json:"output_strict"` // 接口是否为严格模式 : 不配置,可返回任意类型, 配置, 必须返回结构体或者map
|
||||
CtxType string `json:"ctx_type"` // ctx参数类型
|
||||
FormDataType reflect.Type `json:"-"` // 表单数据类型
|
||||
ResultDataType reflect.Type `json:"-"` // 返回值数据类型
|
||||
ApiStructValue reflect.Value `json:"-"` // 逻辑函数所属结构体取值
|
||||
|
@ -86,7 +86,13 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
if uriCfg.FormDataType.Kind() != reflect.Ptr {
|
||||
inputValue = inputValue.Elem()
|
||||
}
|
||||
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, reflect.ValueOf(ctx), inputValue})
|
||||
var firstParam reflect.Value
|
||||
if uriCfg.CtxType == define.CustomContextKey {
|
||||
firstParam = reflect.ValueOf(ctx.MustGet(define.CustomContextKey))
|
||||
} else {
|
||||
firstParam = reflect.ValueOf(ctx)
|
||||
}
|
||||
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, firstParam, inputValue})
|
||||
if resList[1].IsNil() {
|
||||
// 请求成功
|
||||
isSuccess = true
|
||||
|
Loading…
x
Reference in New Issue
Block a user