-
Notifications
You must be signed in to change notification settings - Fork 19
/
spec_definition.go
243 lines (213 loc) · 6.33 KB
/
spec_definition.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
package vega
import (
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
)
const (
DataSourceDefinitionTypeUspec = 0
DataSourceDefinitionTypeInt = 1
DataSourceDefinitionTypeExt = 2
)
func (d DataSourceDefinition) DeepClone() *DataSourceDefinition {
cpy := &DataSourceDefinition{}
if d.SourceType != nil {
switch t := d.SourceType.(type) {
case *DataSourceDefinition_External:
cpy.SourceType = t.DeepClone()
case *DataSourceDefinition_Internal:
cpy.SourceType = t.DeepClone()
}
}
return cpy
}
// GetSigners tries to get the Signers from the DataSourceDefinition object.
func (d DataSourceDefinition) GetSigners() []*datapb.Signer {
signers := []*datapb.Signer{}
if d.SourceType != nil {
switch tp := d.SourceType.(type) {
case *DataSourceDefinition_External:
if tp.External != nil {
if tp.External.SourceType != nil {
switch etp := tp.External.SourceType.(type) {
case *DataSourceDefinitionExternal_Oracle:
if etp.Oracle != nil {
signers = etp.Oracle.Signers
}
}
}
}
}
}
return signers
}
func (d DataSourceDefinition) GetFilters() []*datapb.Filter {
filters := []*datapb.Filter{}
if d.SourceType != nil {
switch tp := d.SourceType.(type) {
case *DataSourceDefinition_External:
if tp.External != nil {
if tp.External.SourceType != nil {
switch etp := tp.External.SourceType.(type) {
case *DataSourceDefinitionExternal_Oracle:
if etp.Oracle != nil {
filters = etp.Oracle.Filters
}
}
}
}
case *DataSourceDefinition_Internal:
if tp.Internal != nil {
if tp.Internal.SourceType != nil {
switch itp := tp.Internal.SourceType.(type) {
case *DataSourceDefinitionInternal_Time:
if itp.Time != nil {
if len(itp.Time.Conditions) > 0 {
filters = append(filters,
&datapb.Filter{
Key: &datapb.PropertyKey{
Name: "vegaprotocol.builtin.timestamp",
Type: datapb.PropertyKey_TYPE_TIMESTAMP,
},
Conditions: []*datapb.Condition{
itp.Time.Conditions[0],
},
},
)
}
}
}
}
}
}
}
return filters
}
func NewDataSourceDefinition(tp int) *DataSourceDefinition {
ds := &DataSourceDefinition{}
switch tp {
case DataSourceDefinitionTypeInt:
ds.SourceType = &DataSourceDefinition_Internal{
Internal: &DataSourceDefinitionInternal{
SourceType: &DataSourceDefinitionInternal_Time{
Time: &DataSourceSpecConfigurationTime{
Conditions: []*datapb.Condition{},
},
},
},
}
case DataSourceDefinitionTypeExt:
ds.SourceType = &DataSourceDefinition_External{
External: &DataSourceDefinitionExternal{
SourceType: &DataSourceDefinitionExternal_Oracle{
Oracle: &DataSourceSpecConfiguration{
Signers: []*datapb.Signer{},
Filters: []*datapb.Filter{},
},
},
},
}
}
return ds
}
// SetOracleConfig sets a given oracle config in the receiver.
// If the receiver is not external oracle type of data source - it is not changed.
// This method does not care about object previous contents - use with caution (currenty needed only for testing purposes).
func (s *DataSourceDefinition) SetOracleConfig(oc *DataSourceSpecConfiguration) *DataSourceDefinition {
if s.SourceType != nil {
switch def := s.SourceType.(type) {
// For the case the definition source type is vegapb.DataSourceDefinition_External
case *DataSourceDefinition_External:
if def.External != nil {
if def.External.SourceType != nil {
switch def.External.SourceType.(type) {
// For the case the vegapb.DataSourceDefinitionExternal is not nill
// and its embedded object is of type vegapb.DataSourceDefinitionExternal_Oracle
case *DataSourceDefinitionExternal_Oracle:
// Set the new config only in this case
ds := &DataSourceDefinition{
SourceType: &DataSourceDefinition_External{
External: &DataSourceDefinitionExternal{
SourceType: &DataSourceDefinitionExternal_Oracle{
Oracle: oc,
},
},
},
}
*s = *ds
}
}
}
}
}
return s
}
// SetTimeTriggerConditionConfig sets a condition to the time triggered receiver.
// If the receiver is not a time triggered data source - it does not set anything to it.
// This method does not care about object previous contents - use with caution (currenty needed only for testing purposes).
func (s *DataSourceDefinition) SetTimeTriggerConditionConfig(c []*datapb.Condition) *DataSourceDefinition {
if s.SourceType != nil {
switch def := s.SourceType.(type) {
// For the case the definition source type is vegapb.DataSourceDefinition_Internal
case *DataSourceDefinition_Internal:
if def.Internal != nil {
if def.Internal.SourceType != nil {
switch def.Internal.SourceType.(type) {
// For the case the vegapb.DataSourceDefinitionInternal is not nill
// and its embedded object is of type vegapb.DataSourceDefinitionInternal_Time
case *DataSourceDefinitionInternal_Time:
// Set the new condition only in this case
cond := []*datapb.Condition{}
if len(c) > 0 {
cond = append(cond, c[0])
}
ds := &DataSourceDefinition{
SourceType: &DataSourceDefinition_Internal{
Internal: &DataSourceDefinitionInternal{
SourceType: &DataSourceDefinitionInternal_Time{
Time: &DataSourceSpecConfigurationTime{
Conditions: cond,
},
},
},
},
}
*s = *ds
}
}
}
}
}
return s
}
func (s *DataSourceDefinition) Content() interface{} {
if s != nil {
if s.SourceType != nil {
switch tp := s.SourceType.(type) {
case *DataSourceDefinition_External:
if tp.External != nil {
if tp.External.SourceType != nil {
switch extTp := tp.External.SourceType.(type) {
case *DataSourceDefinitionExternal_Oracle:
if extTp.Oracle != nil {
return extTp.Oracle
}
// The Ethereum type case will go here later.
}
}
}
case *DataSourceDefinition_Internal:
if tp.Internal != nil {
if tp.Internal.SourceType != nil {
switch intTp := tp.Internal.SourceType.(type) {
case *DataSourceDefinitionInternal_Time:
if intTp.Time != nil {
return intTp.Time
}
// The rest of the internal type sources will go here.
}
}
}
}
}
}
return nil
}