dsyrdwdbo47282676 2017-07-26 18:36
浏览 47
已采纳

Golang微服务中间件允许任何类型,但对端点严格

New to golang but what I'm trying to do is make my logging middleware generic e.g. allow any type and then call the method for the next layer.

So below us the loggingmiddleware package, where I want to be able to accept any type and print it out.

package loggingmiddleware

import (
    "context"
    "time"

    gokitlogger "github.com/go-kit/kit/log"
)

type layer interface {
    Run(context.Context, interface{}) (interface{}, error)
}

type LoggingMiddleware struct {
    Logger gokitlogger.Logger
    Layer  layer
}

func (mw LoggingMiddleware) Run(ctx context.Context, i interface{}) (output interface{}, err error) {

    defer func(begin time.Time) {

        mw.Logger.Log(
            "method", "name of method",
            "input", i,
            "output", output,
            "err", err,
            "took", time.Since(begin),
        )

    }(time.Now())

    output, err = mw.Layer.Run(ctx, i)
    return

}

However I want to be strict when calling the next method, if it needs to be string I want to set the type to be string rather than interface{}

In my example I want to make sure only a float64 type will be used as an argument

type mathServiceInterface interface {
    Run(context.Context, float64) (float64, error)
}

type mathService struct{}

func (mathService) Run(_ context.Context, f float64) (float64, error) {
    return f * f, nil
}

However with my current implementation I'm getting this error...

# github.com/jakelacey2012/blankit/blankit-ms/sqaure

./main.go:92: cannot use ms (type mathServiceInterface) as type loggingmiddleware.layer in field value:
        mathServiceInterface does not implement loggingmiddleware.layer (wrong type for Run method)
                have Run(context.Context, float64) (float64, error)
                want Run(context.Context, interface {}) (interface {}, error)

./main.go:92: cannot use loggingmiddleware.LoggingMiddleware literal (type loggingmiddleware.LoggingMiddleware) as type mathServiceInterface in assignment:
        loggingmiddleware.LoggingMiddleware does not implement mathServiceInterface (wrong type for Run method)
                have Run(context.Context, interface {}) (interface {}, error)
                want Run(context.Context, float64) (float64, error)

I understand the error, however I don't know whether my implementation is over complicating things because I don't know go.

I hope what I'm saying makes sense, I was unsure what to title this as so please feel free to edit it.

Also if you need more code to better explain please do let me know.

  • 写回答

2条回答 默认 最新

  • dr2898 2017-07-26 19:09
    关注

    What's going to be calling these? At some point there is an actual consumer, and that consumer will (presumably, based on your code) be using an interface (either layer or an identical interface). If there's middleware, that interface will necessarily be as generic as the middleware - i.e., taking a interface{} as a parameter to Run. So making something downstream more specific (besides not compiling as you've seen) doesn't make any sense: the actual consumer won't see the more-specific interface, it will see Run(Context,interface{}) (interface{},error).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序