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 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用