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

refactor(generic): refactor existing generic to have new ServiceInfo which has the generic's reader and writer info directly #1365

Merged
merged 26 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
be37e5e
json thrift generic refactor
Marina-Sakai May 28, 2024
31f1276
json pb refactor
Marina-Sakai May 28, 2024
52fa792
map and http thrift generic
Marina-Sakai May 28, 2024
77c660a
http pb thrift generic refactor
Marina-Sakai May 29, 2024
cd1ec84
add err handling
Marina-Sakai May 29, 2024
93507aa
test: add and fix tests
Marina-Sakai May 30, 2024
a53e276
fix: nit things
Marina-Sakai May 30, 2024
7696efa
fix: jsonthrift codec test
Marina-Sakai May 30, 2024
1a11169
fix: lint
Marina-Sakai May 30, 2024
5374913
fix: import
Marina-Sakai May 30, 2024
5ce39d3
fix: revert unnecessary files
Marina-Sakai May 30, 2024
95764d1
fix: import
Marina-Sakai May 30, 2024
3671fe0
fix: import lint
Marina-Sakai May 30, 2024
4548064
fix: pass method and isClient to writer and reader
Marina-Sakai Jun 4, 2024
7515622
fix: typo
Marina-Sakai Jun 4, 2024
2e3ea5b
fix: make a TODO for binary generic multi-service
Marina-Sakai Jun 4, 2024
6d927e2
revert: reserve the old APIs and leave comments
Marina-Sakai Jun 4, 2024
d2df168
fix: arguments
Marina-Sakai Jun 4, 2024
a569add
fix: review dog
Marina-Sakai Jun 4, 2024
cc0c758
fix: no need to check cause ttheader doesn't pass idlSvcName when it'…
Marina-Sakai Jun 5, 2024
0f12dd5
fix: lint
Marina-Sakai Jun 7, 2024
a8efdb0
fix: better use method
Marina-Sakai Jun 11, 2024
d80b2ab
chore: todo -> note
Marina-Sakai Jun 13, 2024
7921251
refactor: remove codecInfo from serviceInfo
Marina-Sakai Jun 17, 2024
5978061
refactor: no need isClient
Marina-Sakai Jun 17, 2024
00251a9
refactor: no need isClient
Marina-Sakai Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions client/genericclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ Client = &genericServiceClient{}

// NewClient create a generic client
func NewClient(destService string, g generic.Generic, opts ...client.Option) (Client, error) {
svcInfo := generic.ServiceInfo(g.PayloadCodecType())
svcInfo := generic.ServiceInfoWithCodec(g)
return NewClientWithServiceInfo(destService, g, svcInfo, opts...)
}

Expand All @@ -47,6 +47,7 @@ func NewClientWithServiceInfo(destService string, g generic.Generic, svcInfo *se
return nil, err
}
cli := &genericServiceClient{
svcInfo: svcInfo,
kClient: kc,
g: g,
}
Expand Down Expand Up @@ -86,24 +87,27 @@ type Client interface {
}

type genericServiceClient struct {
svcInfo *serviceinfo.ServiceInfo
kClient client.Client
g generic.Generic
}

func (gc *genericServiceClient) GenericCall(ctx context.Context, method string, request interface{}, callOptions ...callopt.Option) (response interface{}, err error) {
ctx = client.NewCtxWithCallOptions(ctx, callOptions)
var _args generic.Args
_args := gc.svcInfo.MethodInfo(method).NewArgs().(*generic.Args)
_args.Method = method
_args.Request = request

mt, err := gc.g.GetMethod(request, method)
if err != nil {
return nil, err
}
if mt.Oneway {
return nil, gc.kClient.Call(ctx, mt.Name, &_args, nil)
return nil, gc.kClient.Call(ctx, mt.Name, _args, nil)
}
var _result generic.Result
if err = gc.kClient.Call(ctx, mt.Name, &_args, &_result); err != nil {

_result := gc.svcInfo.MethodInfo(method).NewResult().(*generic.Result)
if err = gc.kClient.Call(ctx, mt.Name, _args, _result); err != nil {
return
}
return _result.GetSuccess(), nil
Expand Down
4 changes: 2 additions & 2 deletions internal/mocks/generic/thrift.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 66 additions & 37 deletions pkg/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ import (
type Generic interface {
Closer
// PayloadCodec return codec implement
// this is used for generic which does not need IDL
PayloadCodec() remote.PayloadCodec
// PayloadCodecType return the type of codec
PayloadCodecType() serviceinfo.PayloadCodec
// RawThriftBinaryGeneric must be framed
Framed() bool
// GetMethod to get method name if need
// GetMethod is to get method name if needed
GetMethod(req interface{}, method string) (*Method, error)
// IDLServiceName returns idl service name
IDLServiceName() string
// MessageReaderWriter returns reader and writer
// this is used for generic which needs IDL
MessageReaderWriter() interface{}
}

// Method information
Expand All @@ -64,22 +70,14 @@ func BinaryThriftGeneric() Generic {
//
// SetBinaryWithByteSlice(g, true)
func MapThriftGeneric(p DescriptorProvider) (Generic, error) {
codec, err := newMapThriftCodec(p, thriftCodec)
if err != nil {
return nil, err
}
return &mapThriftGeneric{
codec: codec,
codec: newMapThriftCodec(p),
}, nil
}

func MapThriftGenericForJSON(p DescriptorProvider) (Generic, error) {
codec, err := newMapThriftCodecForJSON(p, thriftCodec)
if err != nil {
return nil, err
}
return &mapThriftGeneric{
codec: codec,
codec: newMapThriftCodecForJSON(p),
}, nil
}

Expand All @@ -92,20 +90,12 @@ func MapThriftGenericForJSON(p DescriptorProvider) (Generic, error) {
func HTTPThriftGeneric(p DescriptorProvider, opts ...Option) (Generic, error) {
gOpts := &Options{dynamicgoConvOpts: DefaultHTTPDynamicGoConvOpts}
gOpts.apply(opts)
codec, err := newHTTPThriftCodec(p, thriftCodec, gOpts)
if err != nil {
return nil, err
}
return &httpThriftGeneric{codec: codec}, nil
return &httpThriftGeneric{codec: newHTTPThriftCodec(p, gOpts)}, nil
}

func HTTPPbThriftGeneric(p DescriptorProvider, pbp PbDescriptorProvider) (Generic, error) {
codec, err := newHTTPPbThriftCodec(p, pbp, thriftCodec)
if err != nil {
return nil, err
}
return &httpPbThriftGeneric{
codec: codec,
codec: newHTTPPbThriftCodec(p, pbp),
}, nil
}

Expand All @@ -118,24 +108,15 @@ func HTTPPbThriftGeneric(p DescriptorProvider, pbp PbDescriptorProvider) (Generi
func JSONThriftGeneric(p DescriptorProvider, opts ...Option) (Generic, error) {
gOpts := &Options{dynamicgoConvOpts: DefaultJSONDynamicGoConvOpts}
gOpts.apply(opts)
codec, err := newJsonThriftCodec(p, thriftCodec, gOpts)
if err != nil {
return nil, err
}
return &jsonThriftGeneric{codec: codec}, nil
return &jsonThriftGeneric{codec: newJsonThriftCodec(p, gOpts)}, nil
}

// JSONPbGeneric json mapping generic.
// Uses dynamicgo for json to protobufs conversion, so DynamicGo field is true by default.
func JSONPbGeneric(p PbDescriptorProviderDynamicGo, opts ...Option) (Generic, error) {
gOpts := &Options{dynamicgoConvOpts: conv.Options{}}
gOpts.apply(opts)

codec, err := newJsonPbCodec(p, pbCodec, gOpts)
if err != nil {
return nil, err
}
return &jsonPbGeneric{codec: codec}, nil
return &jsonPbGeneric{codec: newJsonPbCodec(p, gOpts)}, nil
}

// SetBinaryWithBase64 enable/disable Base64 codec for binary field.
Expand Down Expand Up @@ -243,6 +224,14 @@ func (g *binaryThriftGeneric) Close() error {
return nil
}

func (g *binaryThriftGeneric) IDLServiceName() string {
return ""
}

func (g *binaryThriftGeneric) MessageReaderWriter() interface{} {
return nil
}

type mapThriftGeneric struct {
codec *mapThriftCodec
}
Expand All @@ -256,7 +245,7 @@ func (g *mapThriftGeneric) PayloadCodecType() serviceinfo.PayloadCodec {
}

func (g *mapThriftGeneric) PayloadCodec() remote.PayloadCodec {
return g.codec
return nil
}

func (g *mapThriftGeneric) GetMethod(req interface{}, method string) (*Method, error) {
Expand All @@ -267,6 +256,14 @@ func (g *mapThriftGeneric) Close() error {
return g.codec.Close()
}

func (g *mapThriftGeneric) IDLServiceName() string {
return g.codec.svcName
}

func (g *mapThriftGeneric) MessageReaderWriter() interface{} {
return g.codec.getMessageReaderWriter()
}

type jsonThriftGeneric struct {
codec *jsonThriftCodec
}
Expand All @@ -280,7 +277,7 @@ func (g *jsonThriftGeneric) PayloadCodecType() serviceinfo.PayloadCodec {
}

func (g *jsonThriftGeneric) PayloadCodec() remote.PayloadCodec {
return g.codec
return nil
}

func (g *jsonThriftGeneric) GetMethod(req interface{}, method string) (*Method, error) {
Expand All @@ -291,6 +288,14 @@ func (g *jsonThriftGeneric) Close() error {
return g.codec.Close()
}

func (g *jsonThriftGeneric) IDLServiceName() string {
return g.codec.svcName
}

func (g *jsonThriftGeneric) MessageReaderWriter() interface{} {
return g.codec.getMessageReaderWriter()
}

type jsonPbGeneric struct {
codec *jsonPbCodec
}
Expand All @@ -304,7 +309,7 @@ func (g *jsonPbGeneric) PayloadCodecType() serviceinfo.PayloadCodec {
}

func (g *jsonPbGeneric) PayloadCodec() remote.PayloadCodec {
return g.codec
return nil
}

func (g *jsonPbGeneric) GetMethod(req interface{}, method string) (*Method, error) {
Expand All @@ -315,6 +320,14 @@ func (g *jsonPbGeneric) Close() error {
return g.codec.Close()
}

func (g *jsonPbGeneric) IDLServiceName() string {
return g.codec.svcName
}

func (g *jsonPbGeneric) MessageReaderWriter() interface{} {
return g.codec.getMessageReaderWriter()
}

type httpThriftGeneric struct {
codec *httpThriftCodec
}
Expand All @@ -328,7 +341,7 @@ func (g *httpThriftGeneric) PayloadCodecType() serviceinfo.PayloadCodec {
}

func (g *httpThriftGeneric) PayloadCodec() remote.PayloadCodec {
return g.codec
return nil
}

func (g *httpThriftGeneric) GetMethod(req interface{}, method string) (*Method, error) {
Expand All @@ -339,6 +352,14 @@ func (g *httpThriftGeneric) Close() error {
return g.codec.Close()
}

func (g *httpThriftGeneric) IDLServiceName() string {
return g.codec.svcName
}

func (g *httpThriftGeneric) MessageReaderWriter() interface{} {
return g.codec.getMessageReaderWriter()
}

type httpPbThriftGeneric struct {
codec *httpPbThriftCodec
}
Expand All @@ -352,7 +373,7 @@ func (g *httpPbThriftGeneric) PayloadCodecType() serviceinfo.PayloadCodec {
}

func (g *httpPbThriftGeneric) PayloadCodec() remote.PayloadCodec {
return g.codec
return nil
}

func (g *httpPbThriftGeneric) GetMethod(req interface{}, method string) (*Method, error) {
Expand All @@ -362,3 +383,11 @@ func (g *httpPbThriftGeneric) GetMethod(req interface{}, method string) (*Method
func (g *httpPbThriftGeneric) Close() error {
return g.codec.Close()
}

func (g *httpPbThriftGeneric) IDLServiceName() string {
return g.codec.svcName
}

func (g *httpPbThriftGeneric) MessageReaderWriter() interface{} {
return g.codec.getMessageReaderWriter()
}
Loading
Loading