/
encoded.go
112 lines (96 loc) · 4.09 KB
/
encoded.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package internal
import (
"reflect"
"go.uber.org/cadence/internal/common"
"go.uber.org/cadence/internal/common/util"
)
type (
// Value is used to encapsulate/extract encoded value from workflow/activity.
Value interface {
// HasValue return whether there is value encoded.
HasValue() bool
// Get extract the encoded value into strong typed value pointer.
Get(valuePtr interface{}) error
}
// Values is used to encapsulate/extract encoded one or more values from workflow/activity.
Values interface {
// HasValues return whether there are values encoded.
HasValues() bool
// Get extract the encoded values into strong typed value pointers.
Get(valuePtr ...interface{}) error
}
// DataConverter is used by the framework to serialize/deserialize input and output of activity/workflow
// that need to be sent over the wire.
// To encode/decode workflow arguments, one should set DataConverter in two places:
// 1. Workflow worker, through worker.Options
// 2. Client, through client.Options
// To encode/decode Activity/ChildWorkflow arguments, one should set DataConverter in two places:
// 1. Inside workflow code, use workflow.WithDataConverter to create new Context,
// and pass that context to ExecuteActivity/ExecuteChildWorkflow calls.
// Cadence support using different DataConverters for different activity/childWorkflow in same workflow.
// 2. Activity/Workflow worker that run these activity/childWorkflow, through worker.Options.
DataConverter interface {
// ToData implements conversion of a list of values.
ToData(value ...interface{}) ([]byte, error)
// FromData implements conversion of an array of values of different types.
// Useful for deserializing arguments of function invocations.
FromData(input []byte, valuePtr ...interface{}) error
}
// defaultDataConverter uses thrift encoder/decoder when possible, for everything else use json.
defaultDataConverter struct{}
)
var defaultJSONDataConverter = &defaultDataConverter{}
// DefaultDataConverter is default data converter used by Cadence worker
var DefaultDataConverter = getDefaultDataConverter()
// getDefaultDataConverter return default data converter used by Cadence worker
func getDefaultDataConverter() DataConverter {
return defaultJSONDataConverter
}
func (dc *defaultDataConverter) ToData(r ...interface{}) ([]byte, error) {
if len(r) == 1 && util.IsTypeByteSlice(reflect.TypeOf(r[0])) {
return r[0].([]byte), nil
}
var encoder encoding
if common.IsUseThriftEncoding(r) {
encoder = &thriftEncoding{}
} else {
encoder = &jsonEncoding{}
}
data, err := encoder.Marshal(r)
if err != nil {
return nil, err
}
return data, nil
}
func (dc *defaultDataConverter) FromData(data []byte, to ...interface{}) error {
if len(to) == 1 && util.IsTypeByteSlice(reflect.TypeOf(to[0])) {
reflect.ValueOf(to[0]).Elem().SetBytes(data)
return nil
}
var encoder encoding
if common.IsUseThriftDecoding(to) {
encoder = &thriftEncoding{}
} else {
encoder = &jsonEncoding{}
}
return encoder.Unmarshal(data, to)
}