-
Notifications
You must be signed in to change notification settings - Fork 19
/
spec.go
257 lines (216 loc) · 6.14 KB
/
spec.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
package common
import (
"fmt"
"strconv"
"strings"
vegapb "code.vegaprotocol.io/vega/protos/vega"
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
)
type SpecConditionOperator = datapb.Condition_Operator
// SpecCondition mirrors datapb.Condition type.
type SpecCondition struct {
Operator SpecConditionOperator
Value string
}
func (c SpecCondition) String() string {
return fmt.Sprintf(
"value(%s) operator(%s)",
c.Value,
c.Operator.String(),
)
}
func (c SpecCondition) IntoProto() *datapb.Condition {
return &datapb.Condition{
Operator: c.Operator,
Value: c.Value,
}
}
func (c *SpecCondition) DeepClone() *SpecCondition {
return &SpecCondition{
Operator: c.Operator,
Value: c.Value,
}
}
func SpecConditionFromProto(protoCondition *datapb.Condition) *SpecCondition {
return &SpecCondition{
Operator: protoCondition.Operator,
Value: protoCondition.Value,
}
}
type SpecConditions []*SpecCondition
func (sc SpecConditions) IntoProto() []*datapb.Condition {
protoConditions := []*datapb.Condition{}
for _, condition := range sc {
protoConditions = append(protoConditions, condition.IntoProto())
}
return protoConditions
}
func (sc SpecConditions) String() string {
if sc == nil {
return "[]"
}
strs := []string{}
for _, c := range sc {
strs = append(strs, c.String())
}
return "[" + strings.Join(strs, ", ") + "]"
}
func SpecConditionsFromProto(protoConditions []*datapb.Condition) []*SpecCondition {
conditions := []*SpecCondition{}
for _, protoCondition := range protoConditions {
conditions = append(conditions, SpecConditionFromProto(protoCondition))
}
return conditions
}
func DeepCloneSpecConditions(conditions []*SpecCondition) []*SpecCondition {
othConditions := make([]*SpecCondition, 0, len(conditions))
for _, condition := range conditions {
othConditions = append(othConditions, condition.DeepClone())
}
return othConditions
}
type SpecFilter struct {
Key *SpecPropertyKey
Conditions []*SpecCondition
}
func SpecFilterFromProto(protoFilter *datapb.Filter) *SpecFilter {
filter := &SpecFilter{
Key: &SpecPropertyKey{},
Conditions: []*SpecCondition{},
}
if protoFilter.Key != nil {
*filter.Key = *SpecPropertyKeyFromProto(protoFilter.Key)
}
filter.Conditions = SpecConditionsFromProto(protoFilter.Conditions)
return filter
}
func (f SpecFilter) String() string {
return fmt.Sprintf(
"key(%s) conditions(%v)",
f.Key.String(),
SpecConditions(f.Conditions).String(),
)
}
// IntoProto return proto version of the filter receiver
// taking into account if its fields are empty.
func (f *SpecFilter) 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 = SpecConditions(f.Conditions).IntoProto()
}
return filter
}
// DeepClone clones the filter receiver taking into account if its fields are empty.
func (f *SpecFilter) DeepClone() *SpecFilter {
filter := &SpecFilter{
Key: &SpecPropertyKey{},
Conditions: []*SpecCondition{},
}
if f.Key != nil {
filter.Key = f.Key.DeepClone()
}
if len(f.Conditions) > 0 {
filter.Conditions = DeepCloneSpecConditions(f.Conditions)
}
return filter
}
type SpecFilters []*SpecFilter
func (df SpecFilters) IntoProto() []*datapb.Filter {
protoFilters := make([]*datapb.Filter, 0, len(df))
for _, filter := range df {
protoFilters = append(protoFilters, filter.IntoProto())
}
return protoFilters
}
func (df SpecFilters) 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 SpecFiltersFromProto(protoFilters []*datapb.Filter) []*SpecFilter {
dsf := make([]*SpecFilter, len(protoFilters))
for i, protoFilter := range protoFilters {
dsf[i] = SpecFilterFromProto(protoFilter)
}
return dsf
}
func DeepCloneSpecFilters(filters []*SpecFilter) []*SpecFilter {
clonedFilters := make([]*SpecFilter, 0, len(filters))
for _, filter := range filters {
clonedFilters = append(clonedFilters, filter.DeepClone())
}
return clonedFilters
}
type SpecPropertyKeyType = datapb.PropertyKey_Type
type SpecStatus = vegapb.DataSourceSpec_Status
type SpecPropertyKey struct {
Name string
Type SpecPropertyKeyType
NumberDecimalPlaces *uint64
}
func (k SpecPropertyKey) String() string {
var dp string
if k.NumberDecimalPlaces != nil {
dp = strconv.FormatUint(*k.NumberDecimalPlaces, 10)
}
return fmt.Sprintf(
"name(%s) type(%s) decimals(%s)",
k.Name,
k.Type.String(),
dp,
)
}
func (k SpecPropertyKey) IntoProto() *datapb.PropertyKey {
pk := &datapb.PropertyKey{
Name: k.Name,
Type: k.Type,
NumberDecimalPlaces: k.NumberDecimalPlaces,
}
return pk
}
func (k *SpecPropertyKey) DeepClone() *SpecPropertyKey {
c := k
return c
}
func SpecPropertyKeyFromProto(protoKey *datapb.PropertyKey) *SpecPropertyKey {
return &SpecPropertyKey{
Name: protoKey.Name,
Type: protoKey.Type,
NumberDecimalPlaces: protoKey.NumberDecimalPlaces,
}
}
func SpecPropertyKeyIsEmpty(key *SpecPropertyKey) bool {
if key == nil {
return true
}
if key.Name == "" && key.Type == 0 {
return true
}
return false
}