github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/client/call_option.go (about)

     1  package client
     2  
     3  import (
     4  	msgwriter2 "github.com/nyan233/littlerpc/core/common/msgwriter"
     5  	"github.com/nyan233/littlerpc/core/middle/codec"
     6  	"github.com/nyan233/littlerpc/core/middle/packer"
     7  )
     8  
     9  type callConfig struct {
    10  	Writer msgwriter2.Writer
    11  	Codec  codec.Codec
    12  	Packer packer.Packer
    13  }
    14  
    15  type CallOption func(cc *callConfig)
    16  
    17  func WithCallCodec(scheme string) CallOption {
    18  	return func(cc *callConfig) {
    19  		cc.Codec = codec.Get(scheme)
    20  	}
    21  }
    22  
    23  func WithCallWriter(writer msgwriter2.Writer) CallOption {
    24  	return func(cc *callConfig) {
    25  		cc.Writer = writer
    26  	}
    27  }
    28  
    29  func WithCallLRPCNoMuxWriter() CallOption {
    30  	return func(cc *callConfig) {
    31  		cc.Writer = msgwriter2.NewLRPCNoMux()
    32  	}
    33  }
    34  
    35  func WithCallLRPCMuxWriter() CallOption {
    36  	return func(cc *callConfig) {
    37  		cc.Writer = msgwriter2.NewLRPCMux()
    38  	}
    39  }
    40  
    41  func WithCallJsonRpc2Writer() CallOption {
    42  	return func(cc *callConfig) {
    43  		cc.Writer = msgwriter2.NewJsonRPC2()
    44  	}
    45  }
    46  
    47  func WithCallPacker(scheme string) CallOption {
    48  	return func(cc *callConfig) {
    49  		cc.Packer = packer.Get(scheme)
    50  	}
    51  }
    52  
    53  func WithCallJsonRpc2() CallOption {
    54  	return func(cc *callConfig) {
    55  		var c *Config
    56  		WithJsonRpc2()(c)
    57  		cc.Codec = c.Codec
    58  		cc.Writer = c.Writer
    59  		cc.Packer = c.Packer
    60  	}
    61  }