-
Notifications
You must be signed in to change notification settings - Fork 795
/
mapthrift_codec.go
134 lines (121 loc) · 3.7 KB
/
mapthrift_codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package generic
import (
"context"
"errors"
"fmt"
"sync/atomic"
"github.com/cloudwego/kitex/pkg/generic/descriptor"
"github.com/cloudwego/kitex/pkg/generic/thrift"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/remote/codec"
"github.com/cloudwego/kitex/pkg/serviceinfo"
)
var (
_ remote.PayloadCodec = &mapThriftCodec{}
_ Closer = &mapThriftCodec{}
)
type mapThriftCodec struct {
svcDsc atomic.Value // *idl
provider DescriptorProvider
codec remote.PayloadCodec
forJSON bool
binaryWithBase64 bool
binaryWithByteSlice bool
setFieldsForEmptyStruct uint8
}
func newMapThriftCodec(p DescriptorProvider, codec remote.PayloadCodec) (*mapThriftCodec, error) {
svc := <-p.Provide()
c := &mapThriftCodec{
codec: codec,
provider: p,
binaryWithBase64: false,
binaryWithByteSlice: false,
}
c.svcDsc.Store(svc)
go c.update()
return c, nil
}
func newMapThriftCodecForJSON(p DescriptorProvider, codec remote.PayloadCodec) (*mapThriftCodec, error) {
c, err := newMapThriftCodec(p, codec)
if err != nil {
return nil, err
}
c.forJSON = true
return c, nil
}
func (c *mapThriftCodec) update() {
for {
svc, ok := <-c.provider.Provide()
if !ok {
return
}
c.svcDsc.Store(svc)
}
}
func (c *mapThriftCodec) Marshal(ctx context.Context, msg remote.Message, out remote.ByteBuffer) error {
method := msg.RPCInfo().Invocation().MethodName()
if method == "" {
return errors.New("empty methodName in thrift Marshal")
}
if msg.MessageType() == remote.Exception {
return c.codec.Marshal(ctx, msg, out)
}
svcDsc, ok := c.svcDsc.Load().(*descriptor.ServiceDescriptor)
if !ok {
return fmt.Errorf("get parser ServiceDescriptor failed")
}
wm, err := thrift.NewWriteStruct(svcDsc, method, msg.RPCRole() == remote.Client)
if err != nil {
return err
}
wm.SetBinaryWithBase64(c.binaryWithBase64)
msg.Data().(WithCodec).SetCodec(wm)
return c.codec.Marshal(ctx, msg, out)
}
func (c *mapThriftCodec) Unmarshal(ctx context.Context, msg remote.Message, in remote.ByteBuffer) error {
if err := codec.NewDataIfNeeded(serviceinfo.GenericMethod, msg); err != nil {
return err
}
svcDsc, ok := c.svcDsc.Load().(*descriptor.ServiceDescriptor)
if !ok {
return fmt.Errorf("get parser ServiceDescriptor failed")
}
var rm *thrift.ReadStruct
if c.forJSON {
rm = thrift.NewReadStructForJSON(svcDsc, msg.RPCRole() == remote.Client)
} else {
rm = thrift.NewReadStruct(svcDsc, msg.RPCRole() == remote.Client)
}
rm.SetBinaryOption(c.binaryWithBase64, c.binaryWithByteSlice)
rm.SetSetFieldsForEmptyStruct(c.setFieldsForEmptyStruct)
msg.Data().(WithCodec).SetCodec(rm)
return c.codec.Unmarshal(ctx, msg, in)
}
func (c *mapThriftCodec) getMethod(req interface{}, method string) (*Method, error) {
fnSvc, err := c.svcDsc.Load().(*descriptor.ServiceDescriptor).LookupFunctionByMethod(method)
if err != nil {
return nil, err
}
return &Method{method, fnSvc.Oneway}, nil
}
func (c *mapThriftCodec) Name() string {
return "MapThrift"
}
func (c *mapThriftCodec) Close() error {
return c.provider.Close()
}