-
Notifications
You must be signed in to change notification settings - Fork 12
/
payloads.go
316 lines (249 loc) · 7.07 KB
/
payloads.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
313
314
315
316
package transactions
import (
"encoding"
"github.com/kwilteam/kwil-db/pkg/serialize"
)
// PayloadType is the type of payload
type PayloadType string
func (p PayloadType) String() string {
return string(p)
}
func (p PayloadType) Valid() bool {
switch p {
case PayloadTypeDeploySchema,
PayloadTypeDropSchema,
PayloadTypeExecuteAction,
PayloadTypeCallAction:
return true
default:
return false
}
}
const (
PayloadTypeDeploySchema PayloadType = "deploy_schema"
PayloadTypeDropSchema PayloadType = "drop_schema"
PayloadTypeExecuteAction PayloadType = "execute_action"
PayloadTypeCallAction PayloadType = "call_action"
PayloadTypeValidatorJoin PayloadType = "validator_join"
PayloadTypeValidatorLeave PayloadType = "validator_leave"
PayloadTypeValidatorApprove PayloadType = "validator_approve"
)
// Payload is the interface that all payloads must implement
// Implementations should use Kwil's serialization package to encode and decode themselves
type Payload interface {
MarshalBinary() (serialize.SerializedData, error)
UnmarshalBinary(serialize.SerializedData) error
Type() PayloadType
}
// Schema is the payload that is used to deploy a schema
type Schema struct {
Owner string
Name string
Tables []*Table
Actions []*Action
Extensions []*Extension
}
var _ Payload = (*Schema)(nil)
func (s *Schema) MarshalBinary() (serialize.SerializedData, error) {
return serialize.Encode(s)
}
func (s *Schema) UnmarshalBinary(b serialize.SerializedData) error {
result, err := serialize.Decode[Schema](b)
if err != nil {
return err
}
*s = *result
return nil
}
func (s *Schema) Type() PayloadType {
return PayloadTypeDeploySchema
}
type Extension struct {
Name string
Config []*ExtensionConfig
Alias string
}
type ExtensionConfig struct {
Argument string
Value string
}
type Table struct {
Name string
Columns []*Column
Indexes []*Index
ForeignKeys []*ForeignKey
}
type Column struct {
Name string
Type string
Attributes []*Attribute
}
type Attribute struct {
Type string
Value string
}
type Action struct {
Name string
Inputs []string
// Mutability could be empty if the abi is generated by legacy version of kuneiform,
// default to "update" for backward compatibility
Mutability string
// Auxiliaries are the auxiliary types that are required for the action, specifying extra characteristics of the action
Auxiliaries []string
Public bool
Statements []string
}
type Index struct {
Name string
Columns []string
Type string
}
type ForeignKey struct {
// ChildKeys are the columns that are referencing another.
// For example, in FOREIGN KEY (a) REFERENCES tbl2(b), "a" is the child key
ChildKeys []string
// ParentKeys are the columns that are being referred to.
// For example, in FOREIGN KEY (a) REFERENCES tbl2(b), "tbl2.b" is the parent key
ParentKeys []string
// ParentTable is the table that holds the parent columns.
// For example, in FOREIGN KEY (a) REFERENCES tbl2(b), "tbl2.b" is the parent table
ParentTable string
// Action refers to what the foreign key should do when the parent is altered.
// This is NOT the same as a database action;
// however sqlite's docs refer to these as actions,
// so we should be consistent with that.
// For example, ON DELETE CASCADE is a foreign key action
Actions []*ForeignKeyAction
}
// ForeignKeyAction is used to specify what should occur
// if a parent key is updated or deleted
type ForeignKeyAction struct {
// On can be either "UPDATE" or "DELETE"
On string
// Do specifies what a foreign key action should do
Do string
}
// MutabilityType is the type of mutability
type MutabilityType string
func (t MutabilityType) String() string {
return string(t)
}
const (
MutabilityUpdate MutabilityType = "update"
MutabilityView MutabilityType = "view"
)
// AuxiliaryType is the type of auxiliary
type AuxiliaryType string
func (t AuxiliaryType) String() string {
return string(t)
}
const (
// AuxiliaryTypeMustSign is used to specify that an action need signature, it is used for `view` action.
AuxiliaryTypeMustSign AuxiliaryType = "mustsign"
)
// DropSchema is the payload that is used to drop a schema
type DropSchema struct {
DBID string
}
var _ Payload = (*DropSchema)(nil)
func (s *DropSchema) MarshalBinary() (serialize.SerializedData, error) {
return serialize.Encode(s)
}
func (s *DropSchema) UnmarshalBinary(b serialize.SerializedData) error {
res, err := serialize.Decode[DropSchema](b)
if err != nil {
return err
}
*s = *res
return nil
}
func (s *DropSchema) Type() PayloadType {
return PayloadTypeDropSchema
}
// ActionExecution is the payload that is used to execute an action
type ActionExecution struct {
DBID string
Action string
Arguments [][]string
}
var _ Payload = (*ActionExecution)(nil)
func (a *ActionExecution) MarshalBinary() (serialize.SerializedData, error) {
return serialize.Encode(a)
}
func (a *ActionExecution) UnmarshalBinary(b serialize.SerializedData) error {
res, err := serialize.Decode[ActionExecution](b)
if err != nil {
return err
}
*a = *res
return nil
}
func (a *ActionExecution) Type() PayloadType {
return PayloadTypeExecuteAction
}
// ActionCall is the payload that is used to call an action
type ActionCall struct {
DBID string
Action string
Arguments []string
}
var _ Payload = (*ActionCall)(nil)
func (a *ActionCall) MarshalBinary() (serialize.SerializedData, error) {
return serialize.Encode(a)
}
func (a *ActionCall) UnmarshalBinary(b serialize.SerializedData) error {
res, err := serialize.Decode[ActionCall](b)
if err != nil {
return err
}
*a = *res
return nil
}
var _ encoding.BinaryUnmarshaler = (*ActionCall)(nil)
var _ encoding.BinaryMarshaler = (*ActionCall)(nil)
func (a *ActionCall) Type() PayloadType {
return PayloadTypeCallAction
}
type ValidatorJoin struct {
Candidate []byte
Power uint64
}
func (v *ValidatorJoin) Type() PayloadType {
return PayloadTypeValidatorJoin
}
var _ encoding.BinaryUnmarshaler = (*ValidatorJoin)(nil)
var _ encoding.BinaryMarshaler = (*ValidatorJoin)(nil)
func (v *ValidatorJoin) UnmarshalBinary(b []byte) error {
return serialize.DecodeInto(b, v)
}
func (v *ValidatorJoin) MarshalBinary() ([]byte, error) {
return serialize.Encode(v)
}
type ValidatorApprove struct {
Candidate []byte
}
func (v *ValidatorApprove) Type() PayloadType {
return PayloadTypeValidatorApprove
}
var _ encoding.BinaryUnmarshaler = (*ValidatorApprove)(nil)
var _ encoding.BinaryMarshaler = (*ValidatorApprove)(nil)
func (v *ValidatorApprove) UnmarshalBinary(b []byte) error {
return serialize.DecodeInto(b, v)
}
func (v *ValidatorApprove) MarshalBinary() ([]byte, error) {
return serialize.Encode(v)
}
type ValidatorLeave struct {
Validator []byte
}
func (v *ValidatorLeave) Type() PayloadType {
return PayloadTypeValidatorLeave
}
var _ encoding.BinaryUnmarshaler = (*ValidatorLeave)(nil)
var _ encoding.BinaryMarshaler = (*ValidatorLeave)(nil)
func (v *ValidatorLeave) UnmarshalBinary(b []byte) error {
return serialize.DecodeInto(b, v)
}
func (v *ValidatorLeave) MarshalBinary() ([]byte, error) {
return serialize.Encode(v)
}