Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add(*): Add filedmask interceptor #726

Closed
wants to merge 4 commits into from

Conversation

linhbkhn95
Copy link

@linhbkhn95 linhbkhn95 commented Oct 1, 2024

Changes

  • Add fieldmask interceptor that allow filter response base on client's request intent.

How to use

Pre-condition

You have to add field_mask into proto's request like

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
`

  1. Import and inject into grpc interceptor
    The code in your application should be like that:
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
}
  1. Client usage

    Protobuf definition

      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

        func GetProduct(id string) *Product{
         request := pb.GetProductRequest{
       	Id: "axaxaxx",
       	FieldMask: &fieldmaskpb.FieldMask{
       		Paths: []string{
       			"result.products.id", "result.products.name"
       		},
       	}
          }
          // ...
        }

Verification

  • I wrote tests

@linhbkhn95 linhbkhn95 force-pushed the master branch 3 times, most recently from 938cb37 to 49b100f Compare October 1, 2024 04:15
@linhbkhn95
Copy link
Author

@jpkrohling @bwplotka can you guys give me some suggestions in this PR?

@johanbrandhorst
Copy link
Collaborator

Hi, this is cool, but I'm not sure it's generically useful enough to be merged into the library. Field masks can be used for a number of different things, not just for filtering fields in a response message. I don't think we want this functionality in the library. Thank you for your PR though!

@linhbkhn95
Copy link
Author

I know that field masks are not only for filtering responses(partial update). Can I change the package name to fit with this functionality?
@johanbrandhorst

@johanbrandhorst
Copy link
Collaborator

I just think the functionality is too niche to be generally useful. It's a cool example of how users can implement their own interceptors using the framework in this repo, but I don't know that it's something users will find useful otherwise. You could submit a documentation PR if you want?

@linhbkhn95
Copy link
Author

You could submit a documentation PR if you want?. What is your mean? where is the documentation repo?

@johanbrandhorst
Copy link
Collaborator

I mean that we could add an example to https://github.com/grpc-ecosystem/go-grpc-middleware/tree/main/examples, something about implementing your own interceptors and use this as an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants