golang 类型断言 断到接口类型 实例

// 【出发点】
// 装饰器 封装通用逻辑
func merCtrlProcessor(c *gin.Context, req generic.CommonReqInterface, resp *generic.CommonRespInterface, f func()) {
	merAddCommonInfo(c, req)
	if err := c.ShouldBindJSON(req); err != nil {
		errs := validate.ReadableError(err)
		c.AbortWithStatusJSON(http.StatusNotAcceptable, errs)
		return
	}
	transLog.FrontIn(time.Now(), req.(model.Trace), req)	// ###
	f()
	baseResp := (*resp).Get()
	// baseResp.Base = baseReq.Base
	transLog.FrontOut(time.Now(), (*resp).(model.Trace), resp)	// ###
	c.Set(common.HTTP_CODE, baseResp.HttpStatus)
	c.Set(common.RESP_ITF, resp)
}

//-------------------------------

type CommonReqInterface interface {
	Get() *CommonRequest
}

func (r *CommonRequest) Get() *CommonRequest {
	return r
}

type CommonRequest struct {
	MsgID     string `json:"-" bson:"-"`
	TransType string `json:"-" bson:"-"`
	ApiVer    string `json:"-" bson:"-"`
	ApiMode   string `json:"-" bson:"-"`
	GwID      string `json:"-" bson:"-"`
	CID       string `json:"-" bson:"-"`
	OID       string `json:"-" bson:"-"`
	SID       string `json:"-" bson:"-"`
	TID       string `json:"-" bson:"-"`
	InsCode   string `json:"-" bson:"-"`
}

// -------

type CommonRespInterface interface {
	Get() *CommonResponse
}

func (r *CommonResponse) Get() *CommonResponse {
	return r
}

type CommonResponse struct {
	MsgID      string `json:"-" bson:"-"`
	TransType  string `json:"-" bson:"-"`
	HttpStatus int    `json:"-" bson:"-"`
	OrderNum   string `json:"-" bson:"-"`
}

//-------------------------------

type Trace interface {
	TraceInfo() TraceInfo
}

func (r *ApplePayReq) TraceInfo() model.TraceInfo {
	return model.TraceInfo{
		TransId:     r.OrderNum,
		OrigTransId: "",
		TransType:   r.TransType,
	}
}

// 附注Trace接口不同的实现类,发现其中字段的赋值不一样,这也体现了“transLog.FrontIn(time.Now(), req.(model.Trace), req)”这样做的意义
// func (r *AddPaymentMethodReq) TraceInfo() model.TraceInfo {
// 	return model.TraceInfo{
// 		TransId:     r.MsgID,
// 		OrigTransId: "",
// 		TransType:   r.TransType,
// 	}
// }

type TraceInfo struct {
	TransId     string
	OrigTransId string
	TransType   string
}

//-------------------------------

func FrontIn(transTime time.Time, trace model.Trace, data interface{}) {
	insert(transTime, _FRONT, _IN, trace, data)
}

func insert(transTime time.Time, msgType int, direction string, trace model.Trace, data interface{}) {
	info := trace.TraceInfo()

	l := &model.TransLog{
		TransTime:   transTime,
		MsgType:     msgType,
		Direction:   direction,
		TransId:     info.TransId,
		OrigTransId: info.OrigTransId,
		TransType:   info.TransType,
		Msg:         data,
	}

	logChan <- l
}

//-------------------------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值