-
Notifications
You must be signed in to change notification settings - Fork 703
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
b1c904f
commit 4ad25b3
Showing
7 changed files
with
118 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# fieldmask-middlware | ||
Go grpc middleware for field mask | ||
This package provides an interceptor that will `filter` server response by client intention. | ||
|
||
# How to use | ||
`Pre-condition` | ||
|
||
You have to add `field_mask` into proto's request like | ||
``` Protobuf | ||
message Request{ | ||
// main fields | ||
google.protobuf.FieldMask field_mask = 100; | ||
} | ||
``` | ||
1. install via go get | ||
|
||
`go get github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/fieldmask` | ||
` | ||
|
||
2. Import and inject into grpc interceptor | ||
The code in your application should be like that: | ||
``` Go | ||
import( | ||
// ... | ||
"google.golang.org/grpc" | ||
fieldmaskpkg "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/fieldmask" | ||
|
||
|
||
) | ||
// ... | ||
|
||
func main(){ | ||
var unaryOpts []grpc.UnaryServerInterceptor{ | ||
fieldmaskpkg.UnaryServerInterceptor(fieldmaskpkg.DefaultFilterFunc), | ||
} | ||
// Should append others interceptors | ||
} | ||
``` | ||
3. Client usage | ||
|
||
`Protobuf definition` | ||
```Protobuf | ||
message GetProductRequest{ | ||
string id = 1; | ||
google.protobuf.FieldMask field_mask = 2; | ||
} | ||
message Response{ | ||
message Result{ | ||
repeated Product products = 1; | ||
} | ||
Result result = 1; | ||
} | ||
message Product{ | ||
string id = 1; | ||
string name = 2; | ||
string img = 3; | ||
decimal price = 4; | ||
} | ||
``` | ||
|
||
Client interaction | ||
|
||
``` Go | ||
func GetProduct(id string) *Product{ | ||
request := pb.GetProductRequest{ | ||
Id: "axaxaxx", | ||
FieldMask: &fieldmaskpb.FieldMask{ | ||
Paths: []string{ | ||
"result.products.id", "result.products.name" | ||
}, | ||
} | ||
} | ||
// ... | ||
} | ||
``` |
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,8 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
/* | ||
Package fieldmask is a middleware that filter response base on client's request intent before return to clients. | ||
Please see examples for simple examples of use. | ||
*/ | ||
package fieldmask |
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,24 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package fieldmask | ||
|
||
import ( | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// Simple example of server initialization code. | ||
func Example_serverConfig() { | ||
_ = grpc.NewServer( | ||
grpc.UnaryInterceptor(UnaryServerInterceptor(DefaultFilterFunc)), | ||
) | ||
} | ||
|
||
// Simple example of server initialization code with fieldmask interceptor. | ||
func Example_serverConfigWithAuthOverride() { | ||
server := grpc.NewServer( | ||
grpc.UnaryInterceptor(UnaryServerInterceptor(DefaultFilterFunc)), | ||
) | ||
testpb.RegisterTestServiceServer(server, &testpb.TestPingService{}) | ||
} |
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
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