github.com/annwntech/go-micro/v2@v2.9.5/client/grpc/request.go (about)

     1  package grpc
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/annwntech/go-micro/v2/client"
     8  	"github.com/annwntech/go-micro/v2/codec"
     9  )
    10  
    11  type grpcRequest struct {
    12  	service     string
    13  	method      string
    14  	contentType string
    15  	request     interface{}
    16  	opts        client.RequestOptions
    17  	codec       codec.Codec
    18  }
    19  
    20  // service Struct.Method /service.Struct/Method
    21  func methodToGRPC(service, method string) string {
    22  	// no method or already grpc method
    23  	if len(method) == 0 || method[0] == '/' {
    24  		return method
    25  	}
    26  
    27  	// assume method is Foo.Bar
    28  	mParts := strings.Split(method, ".")
    29  	if len(mParts) != 2 {
    30  		return method
    31  	}
    32  
    33  	if len(service) == 0 {
    34  		return fmt.Sprintf("/%s/%s", mParts[0], mParts[1])
    35  	}
    36  
    37  	// return /pkg.Foo/Bar
    38  	return fmt.Sprintf("/%s.%s/%s", service, mParts[0], mParts[1])
    39  }
    40  
    41  func newGRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request {
    42  	var opts client.RequestOptions
    43  	for _, o := range reqOpts {
    44  		o(&opts)
    45  	}
    46  
    47  	// set the content-type specified
    48  	if len(opts.ContentType) > 0 {
    49  		contentType = opts.ContentType
    50  	}
    51  
    52  	return &grpcRequest{
    53  		service:     service,
    54  		method:      method,
    55  		request:     request,
    56  		contentType: contentType,
    57  		opts:        opts,
    58  	}
    59  }
    60  
    61  func (g *grpcRequest) ContentType() string {
    62  	return g.contentType
    63  }
    64  
    65  func (g *grpcRequest) Service() string {
    66  	return g.service
    67  }
    68  
    69  func (g *grpcRequest) Method() string {
    70  	return g.method
    71  }
    72  
    73  func (g *grpcRequest) Endpoint() string {
    74  	return g.method
    75  }
    76  
    77  func (g *grpcRequest) Codec() codec.Writer {
    78  	return g.codec
    79  }
    80  
    81  func (g *grpcRequest) Body() interface{} {
    82  	return g.request
    83  }
    84  
    85  func (g *grpcRequest) Stream() bool {
    86  	return g.opts.Stream
    87  }