卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34500本站已运行393

Golang 装饰器函数的哪些参数

golang 装饰器函数的哪些参数

php小编百草为大家介绍一下Golang装饰器函数的参数。在Golang中,装饰器函数是一种特殊的函数,可以用来包装其他函数,为其添加额外的功能。装饰器函数通常有三个参数:原始函数、装饰器函数的参数和返回值。原始函数是需要被装饰的函数,装饰器函数的参数可以是任意类型,可以用来传递额外的参数给装饰器函数,返回值通常是一个函数,该函数会替代原始函数的执行。通过这些参数,我们可以实现各种灵活的装饰器模式,为函数添加不同的功能,提高代码的可复用性和可扩展性。

问题内容

我想在 adminapi 的几个方法上使用设置器,例如 update。为此,我创建了一个可以与其他方法匹配的方法类型。

我应该使用接口而不是 func type 吗?

type adminapi struct {
}

type toadminctx func(ctx context.context, req interface{}) (interface{}, error)

func (a adminapi) adminm2msetter(s toadminctx) toadminctx {
    return func(ctx context.context, arg interface{}) (interface{}, error) {
        m2mprincipal, _ := a.getm2mprincipal(ctx)
        ctxm2m := extlib.setprincipal(ctx, m2mprincipal)
        return s(ctxm2m, arg)
    }
}

func (a adminapi) update(ctx context.context, req *reqtype) (resptype, error) {}
updateWithAdminCtx := a.adminAPI.AdminM2MSetter(s.adminAPI.Update)
// ERROR => cannot use s.adminAPI.Update (value of type func(ctx 
// context.Context, req *ReqType) (RespType, error)) as grpcAdmin.ToGetAdminCtx value in 
// argument to s.adminAPI.AdminM2MSetter

_, err := updateWithAdminCtx(ctx context.Context, req *ReqType)

解决方法

我认为您遇到的错误是不言自明的:

a.adminapi.adminm2msetter(s.adminapi.update)

正在通话

func (a adminapi) adminm2msetter(s toadminctx) toadminctx {

传入 s.adminapi.update 作为参数,预计类型为 toadminctx。您的该类型定义为:

type toadminctx func(ctx context.context, req interface{}) (interface{}, error)

但是您的 update 函数的第二个参数是 *reqtype,其第一个返回值是 resptype 值,因此 update 不是 toadminctxtoadminctx 函数类型是可以使用上下文和字面上的任何类型调用的函数。您的 update 函数不能保证在 toadminctx 函数可以的所有情况下都能工作。

您正在寻找的是一种“包装”任何函数的方法,添加对 ctx 参数做一些工作(可能设置一些值),然后传递调用。在 go 1.19 之前,我们通过添加某种包装类型来做到这一点,如下所示:

type wrapper struct {
    updatereqtype *reqtype
    anothertype *reqtype2 // for some other call you want to wrap
}

更改所有相关函数,例如 update 函数以采用包装器参数类型:

func (a adminapi) update(ctx context.context, req wrapper) (resp, error) {
    realreq := req.updatereqtype // get the actual request used here
}

响应类型将被类似地包装和/或组合。

现在,go 支持泛型,在这种情况下它们非常有用,让我们将 adminm2msetter 函数更改为如下所示:

func adminm2msetter[t any, r any](s func(context.context, t) (r, error)) func(context.context, t) (r, error) {
    return func (ctx context.context, arg t) (r, error) {
        m2mprincipal, _ := a.getm2mprincipal(ctx)
        ctxm2m := extlib.setprincipal(ctx, m2mprincipal)
        return s(ctxm2m, arg)
    }
}

这样,我们只需要定义这个函数一次,而是依靠编译器为我们需要的所有类型生成一个量身定制的函数。对于 update 函数,我们会执行以下操作:

a.adminapi.adminm2msetter[*reqtype, resptype](s.adminapi.update)

本质上是用 update 函数使用的特定类型替换通用 tr 类型。因为我真的不知道你想以这种方式包装什么函数,所以我使用了 t any, r any,但是因为在我看来你正在尝试包装某种类型的请求处理程序,所以你可以创建自己的约束:

type Requests interface {
    *ReqType1 | *ReqType2 | *ReqType3 // and so on
}
type Responses interface {
    Resp1 | Resp2 | Resp3
}

只需将 [t any, r any] 替换为 [t requests, r responses]

卓越飞翔博客
上一篇: Golang 中 `array` 和 `slice{array,array,...}` 有什么区别?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏