interceptor

package module
v0.0.0-...-e9246d1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 5 Imported by: 0

README

go-grpc-middleware-field-mask

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

message Request{
  // main fields
  google.protobuf.FieldMask field_mask = 100;
}

  1. install via go get

go get github.com/linhbkhn95/go-grpc-middleware-field-mask `

  1. Import and inject into grpc interceptor The code in your application should be like that:
import(
        // ...
        "google.golang.org/grpc"
    	fieldmaskpkg "github.com/linhbkhn95/go-grpc-middleware-field-mask"


)
// ...

func main(){
    var unaryOpts []grpc.UnaryServerInterceptor{
		fieldmaskpkg.UnaryServerInterceptor(fieldmaskpkg.DefaultFilterFunc),
    }
    // Should append others interceptors
}
  1. Client usage

    Protobuf definition

      message ListProductsRequest{
        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 GetProducts() *Product{
         request := pb.GetProductRequest{
       	Id: "axaxaxx",
       	FieldMask: &fieldmaskpb.FieldMask{
       		Paths: []string{
       			"result.products.id", "result.products.name"
       		},
       	}
          }
          // ...
        }
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnaryServerInterceptor

func UnaryServerInterceptor(filterFunc FilterFunc) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor that will decide whether to which fields should return to clients.

Types

type FilterFunc

type FilterFunc func(msg proto.Message, paths []string)
var DefaultFilterFunc FilterFunc = func(msg proto.Message, paths []string) {
	fmutils.Filter(msg, paths)
}

Directories

Path Synopsis
example
pb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL