googleapis / googleapis/google-cloud-go
pubsub/pstest: Add Reactor adapter func type
- Dominant language
- Go
- Stars
- 4.5k
- Forks
- 1.6k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 109
Description
It's currently a little cumbersome to construct custom server reactors for `pstest`, you have to make a type that implements the interface which isn't a problem on it's own but can result in quite a few types needed to be defined you want to react on quite a few functions on server.
I would like to implement a simple `ReactorFunc` type, similar to the `http.HandlerFunc` type. This would be a simple adapter function that implements the `Reactor` interface.
```go
type ReactorFunc func(in any) (handled bool, ret any, err error)
func (f ReactorFunc) React(in any) (handled bool, ret any, err error) {
return f(in)
}
```
This makes it a little easier to define and use reactors:
```go
reactors := []pstest.ServerReactorOption{
{
FuncName: "Acknowledge",
Reactor: ReactorFunc(func(_ any) (handled bool, ret any, err error) {
// Do Something
return false, &emptypb.Empty{}, nil
}),
},
}
srv := pstest.Server(t, reactors...)
```
And as a bonus maybe a constructor helper:
```go
func NewServerReactorOption(fn string, reactor Reactor) pstest.ServerReactorOption {
return pstest.ServerReactorOption{
FuncName: fn,
Reactor: reactor
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.