This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
value.go
459 lines (422 loc) · 9.25 KB
/
value.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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"strconv"
"time"
"blockwatch.cc/tzgo/tezos"
)
const (
EMPTY_LABEL = `@%%@` // illegal Michelson annotation value
RENDER_TYPE_PRIM = 0 // silently output primitive tree instead if human-readable
RENDER_TYPE_FAIL = 1 // return error if human-readable formatting fails
RENDER_TYPE_PANIC = 2 // panic with error if human-readable formatting fails
RENDER_TYPE_DEBUG = 3 // return error and primitives
)
type Value struct {
Type Type
Value Prim
Render int
mapped interface{}
}
func NewValue(typ Type, val Prim) Value {
return Value{
Type: typ,
Value: val,
Render: RENDER_TYPE_PRIM,
}
}
func NewValuePtr(typ Type, val Prim) *Value {
v := NewValue(typ, val)
return &v
}
func (v *Value) Decode(buf []byte) error {
return v.Value.UnmarshalBinary(buf)
}
func (v Value) IsPacked() bool {
return v.Value.IsPacked()
}
func (v Value) IsPackedAny() bool {
return v.Value.IsPackedAny()
}
func (v Value) Unpack() (Value, error) {
if !v.Value.IsPacked() {
return v, nil
}
up, err := v.Value.Unpack()
if err != nil {
return v, err
}
vv := Value{
Type: v.Type.Clone(),
Value: up,
Render: v.Render,
}
return vv, nil
}
func (v Value) UnpackAll() (Value, error) {
if !v.Value.IsPackedAny() {
return v, nil
}
up, err := v.Value.UnpackAll()
if err != nil {
return v, err
}
vv := Value{
Type: v.Type.Clone(),
Value: up,
Render: v.Render,
}
return vv, nil
}
func (e *Value) FixType() {
labels := e.Type.Anno
e.Type = e.Value.BuildType()
e.Type.WasPacked = true
e.Type.Anno = labels
}
func (e *Value) Map() (interface{}, error) {
if e.mapped != nil {
return e.mapped, nil
}
m := make(map[string]interface{})
if err := walkTree(m, EMPTY_LABEL, e.Type, NewStack(e.Value), 0); err != nil {
return nil, err
}
e.mapped = m
// lift scalar values
if len(m) == 1 {
for n, v := range m {
if n == "0" {
e.mapped = v
}
}
}
return e.mapped, nil
}
func (e Value) MarshalJSON() ([]byte, error) {
m, err := e.Map()
if err != nil {
switch e.Render {
case RENDER_TYPE_PRIM:
// render the plain prim tree
return json.Marshal(e.Value)
case RENDER_TYPE_FAIL:
// render the plain prim tree, but fail with render error
buf, _ := json.Marshal(e.Value)
return buf, err
case RENDER_TYPE_PANIC:
// panic with render error
panic(err)
case RENDER_TYPE_DEBUG:
// wrap type and value with error
type ErrorMessage struct {
Message string `json:"message"`
Type Prim `json:"type"`
Value Prim `json:"value"`
}
resp := struct {
Error ErrorMessage `json:"error"`
}{
Error: ErrorMessage{
Message: err.Error(),
Type: e.Type.Prim,
Value: e.Value,
},
}
// FIXME: this is a good place to plug in an error reporting facility
return json.Marshal(resp)
default:
return nil, nil
}
}
return json.Marshal(m)
}
func (p Prim) matchOpCode(oc OpCode) bool {
mismatch := false
switch p.Type {
case PrimSequence:
switch oc {
case T_LIST, T_MAP, T_BIG_MAP, T_SET, T_LAMBDA, T_OR, T_OPTION, T_PAIR,
T_SAPLING_STATE, T_TICKET:
default:
mismatch = true
}
case PrimInt:
switch oc {
case T_INT, T_NAT, T_MUTEZ, T_TIMESTAMP, T_BIG_MAP, T_OR, T_OPTION, T_SAPLING_STATE,
T_BLS12_381_G1, T_BLS12_381_G2, T_BLS12_381_FR, // maybe stored as bytes
T_TICKET:
// accept references to bigmap and sapling states
default:
mismatch = true
}
case PrimString:
// sometimes timestamps and addresses can be strings
switch oc {
case T_BYTES, T_STRING, T_ADDRESS, T_CONTRACT, T_KEY_HASH, T_KEY,
T_SIGNATURE, T_TIMESTAMP, T_OR, T_CHAIN_ID, T_OPTION,
T_TICKET, T_TX_ROLLUP_L2_ADDRESS:
default:
mismatch = true
}
case PrimBytes:
switch oc {
case T_BYTES, T_STRING, T_BOOL, T_ADDRESS, T_KEY_HASH, T_KEY,
T_CONTRACT, T_SIGNATURE, T_OPERATION, T_LAMBDA, T_OR,
T_CHAIN_ID, T_OPTION, T_SAPLING_STATE, T_SAPLING_TRANSACTION,
T_BLS12_381_G1, T_BLS12_381_G2, T_BLS12_381_FR, // maybe stored as bytes
T_TICKET, // allow ticket since first value is ticketer address
T_CHEST, T_CHEST_KEY,
T_TX_ROLLUP_L2_ADDRESS:
default:
mismatch = true
}
default:
switch p.OpCode {
case D_PAIR:
switch oc {
case T_PAIR, T_OR, T_LIST, T_OPTION, T_TICKET:
default:
mismatch = true
}
case D_SOME, D_NONE:
switch oc {
case T_OPTION:
default:
mismatch = true
}
case D_UNIT:
switch oc {
case T_UNIT, K_PARAMETER:
default:
mismatch = true
}
case D_LEFT, D_RIGHT:
switch oc {
case T_OR:
default:
mismatch = true
}
}
}
return !mismatch
}
func (v *Value) GetValue(label string) (interface{}, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
return vv, ok
}
}
return nil, false
}
func (v *Value) GetString(label string) (string, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
if s, ok := vv.(string); ok {
return s, true
} else {
return fmt.Sprint(s), true
}
}
}
return "", false
}
func (v *Value) GetBytes(label string) ([]byte, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// hex string or nil
if vv == nil {
return nil, ok
}
if s, ok := vv.(string); ok {
h, err := hex.DecodeString(s)
if err == nil {
return h, true
}
}
}
}
return nil, false
}
func (v *Value) GetInt64(label string) (int64, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// big, string or nil
if vv == nil {
return 0, ok
}
switch t := vv.(type) {
case int:
return int64(t), true
case int64:
return t, true
case *big.Int:
return t.Int64(), true
case tezos.Z:
return t.Int64(), true
case string:
i, err := strconv.ParseInt(t, 10, 64)
if err == nil {
return i, true
}
}
}
}
return 0, false
}
func (v *Value) GetBig(label string) (*big.Int, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// big, string or nil
if vv == nil {
return big.NewInt(0), ok
}
switch t := vv.(type) {
case *big.Int:
return t, true
case tezos.Z:
return t.Big(), true
case string:
return big.NewInt(0).SetString(t, 10)
}
}
}
return nil, false
}
func (v *Value) GetZ(label string) (*tezos.Z, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// big, string or nil
var z tezos.Z
if vv == nil {
return &z, ok
}
switch t := vv.(type) {
case *big.Int:
return z.SetBig(t), true
case tezos.Z:
return &t, true
case string:
b, ok := big.NewInt(0).SetString(t, 10)
return z.SetBig(b), ok
}
}
}
return nil, false
}
func (v *Value) GetBool(label string) (bool, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// bool, string or nil
if vv == nil {
return false, ok
}
switch t := vv.(type) {
case bool:
return t, true
case string:
if b, err := strconv.ParseBool(t); err == nil {
return b, true
}
}
}
}
return false, false
}
func (v *Value) GetTime(label string) (time.Time, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// time, string or nil
if vv == nil {
return time.Time{}, ok
}
switch t := vv.(type) {
case time.Time:
return t, true
case string:
if b, err := time.Parse(t, time.RFC3339); err == nil {
return b, true
}
}
}
}
return time.Time{}, false
}
func (v *Value) GetAddress(label string) (tezos.Address, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// Adddress, string or nil
if vv == nil {
return tezos.InvalidAddress, ok
}
switch t := vv.(type) {
case tezos.Address:
return t, true
case string:
if b, err := tezos.ParseAddress(t); err == nil {
return b, true
}
}
}
}
return tezos.InvalidAddress, false
}
func (v *Value) GetKey(label string) (tezos.Key, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// Key, string or nil
if vv == nil {
return tezos.InvalidKey, ok
}
switch t := vv.(type) {
case tezos.Key:
return t, true
case string:
if b, err := tezos.ParseKey(t); err == nil {
return b, true
}
}
}
}
return tezos.InvalidKey, false
}
func (v *Value) GetSignature(label string) (tezos.Signature, bool) {
if m, err := v.Map(); err == nil {
if vv, ok := getPath(m, label); ok {
// Signature, string or nil
if vv == nil {
return tezos.InvalidSignature, ok
}
switch t := vv.(type) {
case tezos.Signature:
return t, true
case string:
if b, err := tezos.ParseSignature(t); err == nil {
return b, true
}
}
}
}
return tezos.InvalidSignature, false
}
func (v *Value) Unmarshal(val interface{}) error {
if m, err := v.Map(); err == nil {
buf, _ := json.Marshal(m)
return json.Unmarshal(buf, val)
} else {
return err
}
}
type ValueWalkerFunc func(label string, value interface{}) error
func (v *Value) Walk(label string, fn ValueWalkerFunc) error {
val, ok := v.GetValue(label)
if !ok {
return nil
}
return walkValueMap(label, val, fn)
}