-
Notifications
You must be signed in to change notification settings - Fork 6
/
or.go
523 lines (466 loc) · 10.7 KB
/
or.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package ast
import (
"bytes"
"fmt"
"strings"
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/consts"
"github.com/dipdup-net/go-lib/tools/types"
"github.com/pkg/errors"
)
const (
leftKey = 'L'
rightKey = 'R'
)
// Or -
type Or struct {
Default
LeftType Node
RightType Node
key byte
}
// NewOr -
func NewOr(depth int) *Or {
return &Or{
Default: NewDefault(consts.OR, -1, depth),
}
}
// String -
func (or *Or) String() string {
var s strings.Builder
s.WriteString(or.Default.String())
if or.key == leftKey {
s.WriteString(strings.Repeat(consts.DefaultIndent, or.Depth))
s.WriteString(or.LeftType.String())
}
if or.key == rightKey {
s.WriteString(strings.Repeat(consts.DefaultIndent, or.Depth))
s.WriteString(or.RightType.String())
}
return s.String()
}
// MarshalJSON -
func (or *Or) MarshalJSON() ([]byte, error) {
return marshalJSON(consts.OR, or.Annots, or.LeftType, or.RightType)
}
// ParseType -
func (or *Or) ParseType(node *base.Node, id *int) error {
if err := or.Default.ParseType(node, id); err != nil {
return err
}
if len(node.Args) > 2 || len(node.Args) == 0 {
return errors.Wrap(consts.ErrInvalidArgsCount, "Or.ParseValue")
}
for i := range node.Args {
child, err := typeNode(node.Args[i], or.Depth, id)
if err != nil {
return err
}
if i == 0 {
or.LeftType = child
} else {
or.RightType = child
}
}
return nil
}
// ParseValue -
func (or *Or) ParseValue(node *base.Node) error {
if len(node.Args) > 2 || len(node.Args) == 0 {
return errors.Wrap(consts.ErrInvalidArgsCount, "Or.ParseValue")
}
switch node.Prim {
case consts.Left:
or.key = leftKey
if err := or.LeftType.ParseValue(node.Args[0]); err != nil {
return err
}
case consts.Right:
or.key = rightKey
if err := or.RightType.ParseValue(node.Args[0]); err != nil {
return err
}
default:
return errors.Wrap(consts.ErrInvalidPrim, "Or.ParseValue")
}
return nil
}
// ToMiguel -
func (or *Or) ToMiguel() (*MiguelNode, error) {
node, err := or.Default.ToMiguel()
if err != nil {
return nil, err
}
node.Children = make([]*MiguelNode, 0)
typ := or.LeftType
if or.key == rightKey {
typ = or.RightType
}
child, err := typ.ToMiguel()
if err != nil {
return nil, err
}
if or.Prim == typ.GetPrim() {
node.Children = append(node.Children, child.Children...)
} else {
node.Children = append(node.Children, child)
}
node.Type = consts.TypeNamedEnum
for i := range node.Children {
if node.Children[i].Prim != consts.UNIT {
node.Type = consts.TypeNamedUnion
break
}
}
return node, nil
}
// GetEntrypoints -
func (or *Or) GetEntrypoints() []string {
e := make([]string, 0)
for _, t := range []Node{or.LeftType, or.RightType} {
e = append(e, t.GetEntrypoints()...)
}
return e
}
// ToBaseNode -
func (or *Or) ToBaseNode(optimized bool) (*base.Node, error) {
node := new(base.Node)
switch or.key {
case leftKey:
node.Prim = consts.Left
arg, err := or.LeftType.ToBaseNode(optimized)
if err != nil {
return nil, err
}
node.Args = []*base.Node{arg}
case rightKey:
node.Prim = consts.Right
arg, err := or.RightType.ToBaseNode(optimized)
if err != nil {
return nil, err
}
node.Args = []*base.Node{arg}
default:
return nil, errors.New("OR is not Left or Right")
}
return node, nil
}
// ToJSONSchema -
func (or *Or) ToJSONSchema() (*JSONSchema, error) {
oneOf := make([]*JSONSchema, 0)
for i, arg := range []Node{or.LeftType, or.RightType} {
child, err := arg.ToJSONSchema()
if err != nil {
return nil, err
}
key := leftKey
if i == 1 {
key = rightKey
}
if arg.IsPrim(consts.OR) {
for i := range child.OneOf {
child.OneOf[i].Properties["schemaKey"].Const = string(key) + child.OneOf[i].Properties["schemaKey"].Const
oneOf = append(oneOf, child.OneOf[i])
}
} else {
item := &JSONSchema{
Title: arg.GetName(),
Properties: map[string]*JSONSchema{
"schemaKey": {
Type: JSONSchemaTypeString,
Const: string(key),
},
},
}
switch {
case arg.IsPrim(consts.OPTION) || arg.IsPrim(consts.PAIR):
item.Properties[arg.GetName()] = child
case arg.IsPrim(consts.UNIT):
default:
for key, value := range child.Properties {
item.Properties[key] = value
}
}
oneOf = append(oneOf, item)
}
}
return &JSONSchema{
Type: JSONSchemaTypeObject,
Title: or.GetName(),
Prim: or.Prim,
OneOf: oneOf,
}, nil
}
// FromJSONSchema -
func (or *Or) FromJSONSchema(data map[string]interface{}) error {
var orMap map[string]interface{}
for key, val := range data {
if key != or.GetName() {
continue
}
arrVal, ok := val.(map[string]interface{})
if !ok {
return errors.Wrapf(consts.ErrInvalidType, "Or.FromJSONSchema %T", val)
}
orMap = arrVal
break
}
schemaKey, ok := orMap["schemaKey"]
if !ok {
return errors.Wrap(consts.ErrJSONDataIsAbsent, "Or.FromJSONSchema")
}
sk, ok := schemaKey.(string)
if !ok {
return errors.Wrap(consts.ErrInvalidType, "Or.FromJSONSchema")
}
chr := sk[0]
sk = sk[1:]
orMap["schemaKey"] = sk
switch chr {
case leftKey:
if or.LeftType.IsPrim(consts.OR) {
orMap[or.LeftType.GetName()] = orMap
}
or.key = leftKey
return or.LeftType.FromJSONSchema(orMap)
case rightKey:
if or.RightType.IsPrim(consts.OR) {
orMap[or.RightType.GetName()] = orMap
}
or.key = rightKey
return or.RightType.FromJSONSchema(orMap)
default:
return errors.Wrap(consts.ErrJSONDataIsAbsent, "Or.FromJSONSchema")
}
}
// EnrichBigMap -
func (or *Or) EnrichBigMap(bmd []*types.BigMapDiff) error {
switch or.key {
case leftKey:
return or.LeftType.EnrichBigMap(bmd)
case rightKey:
return or.RightType.EnrichBigMap(bmd)
}
return nil
}
// ToParameters -
func (or *Or) ToParameters() ([]byte, error) {
var prim string
var node Node
switch or.key {
case leftKey:
prim = consts.Left
node = or.LeftType
case rightKey:
prim = consts.Right
node = or.RightType
default:
return nil, nil
}
var builder bytes.Buffer
if _, err := builder.WriteString(fmt.Sprintf(`{"prim":"%s","args":[`, prim)); err != nil {
return nil, err
}
b, err := node.ToParameters()
if err != nil {
return nil, err
}
if _, err := builder.Write(b); err != nil {
return nil, err
}
if _, err := builder.WriteString(`]}`); err != nil {
return nil, err
}
return builder.Bytes(), nil
}
// FindByName -
func (or *Or) FindByName(name string, isEntrypoint bool) Node {
if or.GetName() == name {
return or
}
if node := or.LeftType.FindByName(name, isEntrypoint); node != nil {
return node
}
if node := or.RightType.FindByName(name, isEntrypoint); node != nil {
return node
}
return nil
}
// Docs -
func (or *Or) Docs(inferredName string) ([]Typedef, string, error) {
name := getNameDocString(or, inferredName)
typedef := Typedef{
Name: name,
Type: or.Prim,
Args: make([]TypedefArg, 0),
}
result := make([]Typedef, 0)
types := []Node{or.LeftType, or.RightType}
for i := range types {
if isSimpleDocType(types[i].GetPrim()) {
typedef.Args = append(typedef.Args, TypedefArg{
Key: types[i].GetName(),
Value: types[i].GetPrim(),
})
continue
}
args, varName, err := types[i].Docs(name)
if err != nil {
return nil, "", err
}
if types[i].IsPrim(or.Prim) {
typedef.Args = append(typedef.Args, args[0].Args...)
for j := range args {
if args[j].Type != or.Prim {
result = append(result, args[j])
}
}
} else {
typedef.Args = append(typedef.Args, TypedefArg{
Key: types[i].GetName(),
Value: varName,
})
for j := range args {
if !isFlatDocType(args[j]) {
result = append(result, args[j])
}
}
}
}
result = append([]Typedef{typedef}, result...)
return result, makeVarDocString(name), nil
}
// Compare -
func (or *Or) Compare(second Comparable) (int, error) {
secondItem, ok := second.(*Or)
if !ok {
return 0, consts.ErrTypeIsNotComparable
}
var err error
var res int
switch {
case or.key == leftKey && secondItem.key == leftKey:
res, err = or.LeftType.Compare(secondItem.LeftType)
case or.key == rightKey && secondItem.key == rightKey:
res, err = or.RightType.Compare(secondItem.RightType)
case or.key == leftKey && secondItem.key == rightKey:
res = -1
case or.key == rightKey && secondItem.key == leftKey:
res = 1
}
if err != nil {
return 0, err
}
return res, nil
}
// Distinguish -
func (or *Or) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*Or)
if !ok {
return nil, nil
}
node, err := or.Default.ToMiguel()
if err != nil {
return nil, err
}
node.Children = make([]*MiguelNode, 0)
switch {
case or.key == leftKey && second.key == leftKey:
child, err := or.LeftType.Distinguish(second.LeftType)
if err != nil {
return nil, err
}
node.Children = append(node.Children, child)
case or.key == rightKey && second.key == rightKey:
child, err := or.RightType.Distinguish(second.RightType)
if err != nil {
return nil, err
}
node.Children = append(node.Children, child)
case or.key == 0 && second.key == 0:
default:
if second.key == rightKey {
child, err := second.RightType.ToMiguel()
if err != nil {
return nil, err
}
node.Children = append(node.Children, child)
node.setDiffType(MiguelKindUpdate)
}
if second.key == leftKey {
child, err := second.LeftType.ToMiguel()
if err != nil {
return nil, err
}
node.Children = append(node.Children, child)
node.setDiffType(MiguelKindUpdate)
}
}
return node, nil
}
// EqualType -
func (or *Or) EqualType(node Node) bool {
if !or.Default.EqualType(node) {
return false
}
second, ok := node.(*Or)
if !ok {
return false
}
if !or.LeftType.EqualType(second.LeftType) {
return false
}
return or.RightType.EqualType(second.RightType)
}
// FindPointers -
func (or *Or) FindPointers() map[int64]*BigMap {
switch or.key {
case leftKey:
return or.LeftType.FindPointers()
case rightKey:
return or.RightType.FindPointers()
}
return nil
}
// Range -
func (or *Or) Range(handler func(node Node) error) error {
if err := or.LeftType.Range(handler); err != nil {
return err
}
return or.RightType.Range(handler)
}
// GetJSONModel -
func (or *Or) GetJSONModel(model JSONModel) {
if model == nil {
return
}
var node Node
switch or.key {
case leftKey:
node = or.LeftType
case rightKey:
node = or.RightType
default:
return
}
item := make(JSONModel)
node.GetJSONModel(item)
if node.IsPrim(or.Prim) {
val, ok := model[node.GetName()]
if !ok {
return
}
child := val.(JSONModel)
child["schemaKey"] = fmt.Sprintf("%s%v", string(or.key), child["schemaKey"])
delete(model, node.GetName())
model[or.GetName()] = child
} else {
newModel := JSONModel{
"schemaKey": string(or.key),
}
for key := range item {
newModel[key] = item[key]
}
model[or.GetName()] = newModel
}
}