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