-
Notifications
You must be signed in to change notification settings - Fork 705
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88ca73f
commit b1c904f
Showing
7 changed files
with
1,229 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package fieldmask | ||
|
||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
// Copyright 2017 David Ackroyd. All Rights Reserved. | ||
// See LICENSE for licensing terms. | ||
|
||
package fieldmask | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
"google.golang.org/grpc" | ||
"google.golang.org/protobuf/types/known/fieldmaskpb" | ||
) | ||
|
||
func TestFieldMaskSuite(t *testing.T) { | ||
s := &FieldMaskSuite{ | ||
InterceptorTestSuite: &testpb.InterceptorTestSuite{ | ||
TestService: &testpb.TestPingService{}, | ||
ServerOpts: []grpc.ServerOption{ | ||
grpc.UnaryInterceptor( | ||
UnaryServerInterceptor(DefaultFilterFunc), | ||
), | ||
}, | ||
}, | ||
} | ||
suite.Run(t, s) | ||
} | ||
|
||
type FieldMaskSuite struct { | ||
*testpb.InterceptorTestSuite | ||
} | ||
|
||
func (s *FieldMaskSuite) TestUnary_ReturnAllResponse() { | ||
resp, err := s.Client.Ping(s.SimpleCtx(), &testpb.PingRequest{Value: "1"}) | ||
assert.Equal(s.T(), nil, err) | ||
expected := &testpb.PingResponse{ | ||
Value: "1", | ||
} | ||
assert.Equal(s.T(), expected.Counter, resp.Counter) | ||
assert.Equal(s.T(), expected.Value, resp.Value) | ||
} | ||
|
||
func (s *FieldMaskSuite) TestUnary_NoReturnValueResponse() { | ||
resp, err := s.Client.Ping(s.SimpleCtx(), &testpb.PingRequest{Value: "1", FieldMask: &fieldmaskpb.FieldMask{ | ||
Paths: []string{"counter"}, | ||
}}) | ||
assert.Equal(s.T(), nil, err) | ||
expected := &testpb.PingResponse{ | ||
Value: "", | ||
} | ||
assert.Equal(s.T(), expected.Counter, resp.Counter) | ||
assert.Equal(s.T(), expected.Value, resp.Value) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters