What parameters do Golang decorator functions have?

WBOY
Release: 2024-02-08 23:09:39
forward
1414 people have browsed it

Golang 装饰器函数的哪些参数

php editor Baicao will introduce to you the parameters of the Golang decorator function. In Golang, a decorator function is a special function that can be used to wrap other functions and add extra functionality to them. Decorator functions usually have three parameters: the original function, the parameters of the decorator function, and the return value. The original function is the function that needs to be decorated. The parameters of the decorator function can be of any type and can be used to pass additional parameters to the decorator function. The return value is usually a function that replaces the execution of the original function. Through these parameters, we can implement various flexible decorator modes, add different functions to functions, and improve the reusability and scalability of the code.

Question content

I want to use setters on several methods of adminapi, such as update. To do this, I created a method type that can be matched with other methods.

Should I use an interface instead of 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) {}
Copy after login
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)
Copy after login

Solution

I think the error you are encountering is self-explanatory:

a.adminapi.adminm2msetter(s.adminapi.update)
Copy after login

Calling

func (a adminapi) adminm2msetter(s toadminctx) toadminctx {
Copy after login

Pass in s.adminapi.update as a parameter, the expected type is toadminctx. Your type definition is:

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

But the second parameter of your update function is *reqtype, and its first return value is the resptype value, so update is not toadminctx. toadminctx A function type is a function that can be called using a context and literally any type . Your update function is not guaranteed to work in all cases where the toadminctx function does.

What you are looking for is a way to "wrap" any function, add some work on the ctx parameters (maybe set some values), and then pass the call. Prior to go 1.19 we did this by adding some kind of wrapper type like this:

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

Change all related functions, such as the update function to take a wrapper parameter type:

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

Response types will be similarly wrapped and/or combined.

Now that go supports generics, they are very useful in this case, let's change the adminm2msetter function to look like this:

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)
    }
}
Copy after login

This way, we only need to define this function once, but rely on the compiler to generate a tailor-made function for all types we need. For the update function, we do the following:

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

Essentially replaces the generic t and r types with the specific types used by the update function. Since I don't really know what function you are trying to wrap this way, I used t any, r any, but since it seems to me that you are trying to wrap some kind of request handler, So you can create your own constraints:

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

Just replace [t any, r any] with [t requests, r responses]

The above is the detailed content of What parameters do Golang decorator functions have?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!