-
Notifications
You must be signed in to change notification settings - Fork 19
/
data.go
312 lines (262 loc) · 8.01 KB
/
data.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
//lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
package types
import (
"fmt"
"strconv"
"strings"
vegapb "code.vegaprotocol.io/vega/protos/vega"
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
)
type ExternalDataSourceSpecConfiguration struct {
DataSourceSpec *DataSourceSpecConfiguration
}
type DataSourceSpecFilter struct {
Key *DataSourceSpecPropertyKey
Conditions []*DataSourceSpecCondition
}
func DataSourceSpecFilterFromProto(protoFilter *datapb.Filter) *DataSourceSpecFilter {
filter := &DataSourceSpecFilter{
Key: &DataSourceSpecPropertyKey{},
Conditions: []*DataSourceSpecCondition{},
}
if protoFilter.Key != nil {
filter.Key = DataSourceSpecPropertyKeyFromProto(protoFilter.Key)
}
filter.Conditions = DataSourceSpecConditionsFromProto(protoFilter.Conditions)
return filter
}
func (f DataSourceSpecFilter) String() string {
return fmt.Sprintf(
"key(%s) conditions(%v)",
f.Key.String(),
DataSourceSpecConditions(f.Conditions).String(),
)
}
// IntoProto return proto version of the filter receiver
// taking into account if its fields are empty.
func (f *DataSourceSpecFilter) IntoProto() *datapb.Filter {
filter := &datapb.Filter{
Key: &datapb.PropertyKey{},
Conditions: []*datapb.Condition{},
}
if f.Key != nil {
filter.Key = f.Key.IntoProto()
}
if len(f.Conditions) > 0 {
filter.Conditions = DataSourceSpecConditions(f.Conditions).IntoProto()
}
return filter
}
// DeepClone clones the filter receiver taking into account if its fields are empty.
func (f *DataSourceSpecFilter) DeepClone() *DataSourceSpecFilter {
filter := &DataSourceSpecFilter{
Key: &DataSourceSpecPropertyKey{},
Conditions: []*DataSourceSpecCondition{},
}
if f.Key != nil {
filter.Key = f.Key.DeepClone()
}
if len(f.Conditions) > 0 {
filter.Conditions = DeepCloneDataSourceSpecConditions(f.Conditions)
}
return filter
}
type DataSourceSpecFilters []*DataSourceSpecFilter
func (df DataSourceSpecFilters) IntoProto() []*datapb.Filter {
protoFilters := make([]*datapb.Filter, 0, len(df))
for _, filter := range df {
protoFilters = append(protoFilters, filter.IntoProto())
}
return protoFilters
}
func (df DataSourceSpecFilters) String() string {
if df == nil {
return "[]"
}
strs := make([]string, 0, len(df))
for _, f := range df {
strs = append(strs, f.String())
}
return "[" + strings.Join(strs, ", ") + "]"
}
func DataSourceSpecFiltersFromProto(protoFilters []*datapb.Filter) []*DataSourceSpecFilter {
if len(protoFilters) > 0 {
dsf := make([]*DataSourceSpecFilter, len(protoFilters))
if len(protoFilters) > 0 {
for i, protoFilter := range protoFilters {
dsf[i] = DataSourceSpecFilterFromProto(protoFilter)
}
}
return dsf
}
return []*DataSourceSpecFilter{}
}
func DeepCloneDataSourceSpecFilters(filters []*DataSourceSpecFilter) []*DataSourceSpecFilter {
clonedFilters := make([]*DataSourceSpecFilter, 0, len(filters))
for _, filter := range filters {
clonedFilters = append(clonedFilters, filter.DeepClone())
}
return clonedFilters
}
type DataSourceSpecPropertyKeyType = datapb.PropertyKey_Type
type DataSourceSpecToFutureBinding struct{}
type DataSourceSpecBindingForFuture struct {
SettlementDataProperty string
TradingTerminationProperty string
}
func (b DataSourceSpecBindingForFuture) String() string {
return fmt.Sprintf(
"settlementData(%s) tradingTermination(%s)",
b.SettlementDataProperty,
b.TradingTerminationProperty,
)
}
func (b DataSourceSpecBindingForFuture) IntoProto() *vegapb.DataSourceSpecToFutureBinding {
return &vegapb.DataSourceSpecToFutureBinding{
SettlementDataProperty: b.SettlementDataProperty,
TradingTerminationProperty: b.TradingTerminationProperty,
}
}
func (b DataSourceSpecBindingForFuture) DeepClone() *DataSourceSpecBindingForFuture {
return &DataSourceSpecBindingForFuture{
SettlementDataProperty: b.SettlementDataProperty,
TradingTerminationProperty: b.TradingTerminationProperty,
}
}
func DataSourceSpecBindingForFutureFromProto(o *vegapb.DataSourceSpecToFutureBinding) *DataSourceSpecBindingForFuture {
return &DataSourceSpecBindingForFuture{
SettlementDataProperty: o.SettlementDataProperty,
TradingTerminationProperty: o.TradingTerminationProperty,
}
}
type DataSourceSpecStatus = vegapb.DataSourceSpec_Status
type DataSourceSpec struct {
ID string
CreatedAt int64
UpdatedAt int64
Data *DataSourceDefinition
Status DataSourceSpecStatus
}
func (s *DataSourceSpec) IntoProto() *vegapb.DataSourceSpec {
config := &vegapb.DataSourceDefinition{}
if s.Data != nil {
config = s.Data.IntoProto()
}
return &vegapb.DataSourceSpec{
Id: s.ID,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
Data: config,
Status: s.Status,
}
}
func (s *DataSourceSpec) String() string {
configAsString := ""
if s.Data != nil {
configAsString = s.Data.String()
}
return fmt.Sprintf(
"ID(%s) createdAt(%v) updatedAt(%v) data(%s) status(%s)",
s.ID,
s.CreatedAt,
s.UpdatedAt,
configAsString,
s.Status.String(),
)
}
// /
// ToExternalDataSourceSpec wraps the DataSourceSpec receiver into ExternalDataSourceSpec.
// Used for aligning with required types in the code.
func (s *DataSourceSpec) ToExternalDataSourceSpec() *ExternalDataSourceSpec {
return &ExternalDataSourceSpec{
Spec: s,
}
}
func DataSourceSpecFromProto(specProto *vegapb.DataSourceSpec) *DataSourceSpec {
return &DataSourceSpec{
ID: specProto.Id,
CreatedAt: specProto.CreatedAt,
UpdatedAt: specProto.UpdatedAt,
Data: DataSourceDefinitionFromProto(specProto.Data),
Status: specProto.Status,
}
}
type DataSourceSpecSigners []*Signer
func (s DataSourceSpecSigners) String() string {
allSigners := []string{}
for _, signer := range s {
allSigners = append(allSigners, signer.String())
}
return "[" + strings.Join(allSigners, ", ") + "]"
}
type DataSourceSpecPropertyKey struct {
Name string
Type DataSourceSpecPropertyKeyType
NumberDecimalPlaces *uint64
}
func (k DataSourceSpecPropertyKey) String() string {
return fmt.Sprintf(
"name(%s) type(%s) decimals(%s)",
k.Name,
k.Type.String(),
strconv.FormatUint(*k.NumberDecimalPlaces, 10),
)
}
func (k DataSourceSpecPropertyKey) IntoProto() *datapb.PropertyKey {
pk := &datapb.PropertyKey{
Name: k.Name,
Type: k.Type,
NumberDecimalPlaces: k.NumberDecimalPlaces,
}
return pk
}
func (k *DataSourceSpecPropertyKey) DeepClone() *DataSourceSpecPropertyKey {
c := k
return c
}
func DataSourceSpecPropertyKeyFromProto(protoKey *datapb.PropertyKey) *DataSourceSpecPropertyKey {
return &DataSourceSpecPropertyKey{
Name: protoKey.Name,
Type: protoKey.Type,
NumberDecimalPlaces: protoKey.NumberDecimalPlaces,
}
}
func DataSourceSpecPropertyKeyIsEmpty(key *DataSourceSpecPropertyKey) bool {
if key.Name == "" && key.Type == 0 {
return true
}
return false
}
type ExternalDataSourceSpec struct {
Spec *DataSourceSpec
}
func (s *ExternalDataSourceSpec) IntoProto() *vegapb.ExternalDataSourceSpec {
return &vegapb.ExternalDataSourceSpec{
Spec: s.Spec.IntoProto(),
}
}
func (s *ExternalDataSourceSpec) String() string {
return s.Spec.String()
}
func ExternalDataSourceSpecFromProto(specProto *vegapb.ExternalDataSourceSpec) *ExternalDataSourceSpec {
if specProto.Spec != nil {
r := DataSourceSpecFromProto(specProto.Spec)
return &ExternalDataSourceSpec{
Spec: r,
}
}
return &ExternalDataSourceSpec{
Spec: &DataSourceSpec{},
}
}