-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
40 lines (35 loc) · 1.06 KB
/
interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package interceptor
import (
"context"
"github.com/mennanov/fmutils"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
var DefaultFilterFunc FilterFunc = func(msg proto.Message, paths []string) {
fmutils.Filter(msg, paths)
}
type FilterFunc func(msg proto.Message, paths []string)
// UnaryServerInterceptor returns a new unary server interceptor that will decide whether to which fields should return to clients.
func UnaryServerInterceptor(filterFunc FilterFunc) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
resp, err = handler(ctx, req)
if err != nil {
return
}
reqWithFieldMask, ok := req.(interface {
GetFieldMask() *fieldmaskpb.FieldMask
})
if !ok {
return
}
if len(reqWithFieldMask.GetFieldMask().GetPaths()) > 0 {
protoResp, ok := resp.(proto.Message)
if !ok {
return
}
filterFunc(protoResp, reqWithFieldMask.GetFieldMask().GetPaths())
}
return
}
}