-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_cadence.go
154 lines (142 loc) Β· 3.54 KB
/
to_cadence.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
package godence
import (
"encoding/hex"
"fmt"
"math/big"
"reflect"
"strings"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
)
// helper for ufix64
type UFix64 uint64
// helper for fix64
type Fix64 int64
// helper for Address
type Address string
// helper for Address
type Path string
// helper for Character
type Character string
// bigIntToCadence
func bigIntToCadence(i *big.Int) (cadence.Value, error) {
// should from small to big
i128, err := cadence.NewInt128FromBig(i)
if err == nil {
return i128, nil
}
u128, err := cadence.NewUInt128FromBig(i)
if err == nil {
return u128, nil
}
i256, err := cadence.NewInt256FromBig(i)
if err == nil {
return i256, nil
}
u256, err := cadence.NewUInt256FromBig(i)
if err == nil {
return u256, nil
}
return nil, fmt.Errorf("unsupport big.Int value: %s", i.Text(10))
}
// arrayOrSliceToCadence
func arrayOrSliceToCadence(value any) (cadence.Value, error) {
ret := []cadence.Value{}
v := reflect.ValueOf(value)
for i := 0; i < v.Len(); i++ {
// convert all elements of slice/array
cv, err := ToCadence(v.Index(i).Interface())
if err != nil {
return nil, err
}
ret = append(ret, cv)
}
return cadence.NewArray(ret), nil
}
// mapToCadence
func mapToCadence(value any) (cadence.Value, error) {
ret := []cadence.KeyValuePair{}
v := reflect.ValueOf(value)
// convert all entry to KeyValuePair
for _, key := range v.MapKeys() {
ck, err := ToCadence(key.Interface())
if err != nil {
return nil, err
}
cv, err := ToCadence(v.MapIndex(key).Interface())
if err != nil {
return nil, err
}
ret = append(ret, cadence.KeyValuePair{
Key: ck,
Value: cv,
})
}
return cadence.NewDictionary(ret), nil
}
// ToCadence Convert any go value to cadence value.
// Type uint64 will convert to UInt64, if you want to convert to UFix64,
// you should use our UFix64 type.
func ToCadence(value any) (cadence.Value, error) {
switch v := value.(type) {
// integer
case int:
return cadence.NewInt(v), nil
case int8:
return cadence.NewInt8(v), nil
case int16:
return cadence.NewInt16(v), nil
case int32:
return cadence.NewInt32(v), nil
case int64:
return cadence.NewInt64(v), nil
case uint:
return cadence.NewUInt(v), nil
case uint8:
return cadence.NewUInt8(v), nil
case uint16:
return cadence.NewUInt16(v), nil
case uint32:
return cadence.NewUInt32(v), nil
case uint64:
return cadence.NewUInt64(v), nil
case Fix64:
return cadence.NewFix64(fmt.Sprintf("%d.0", int64(v)))
case UFix64:
return cadence.NewUFix64(fmt.Sprintf("%d.0", uint64(v)))
case *big.Int:
return bigIntToCadence(v)
// TODO: float, should float convert to Fix64?
// case float32:
// case float64:
case string:
return cadence.NewString(v)
case Address:
decoded, err := hex.DecodeString(strings.TrimPrefix(string(v), "0x"))
return cadence.BytesToAddress(decoded), err
case Path:
part := strings.Split(string(v), "/")
return cadence.NewPath(common.PathDomainFromIdentifier(part[1]), part[2])
case Character:
return cadence.NewCharacter(string(v))
case bool:
return cadence.NewBool(v), nil
}
switch reflect.TypeOf(value).Kind() {
// array or slice
case reflect.Slice, reflect.Array:
return arrayOrSliceToCadence(value)
// map
case reflect.Map:
return mapToCadence(value)
}
return nil, fmt.Errorf("unsupport type: %s", reflect.TypeOf(value))
}
// ToCadence Convert any go value to cadence optional value.
func ToCadenceOptional(value any) (cadence.Value, error) {
v, err := ToCadence(value)
if err != nil {
return nil, err
}
return cadence.NewOptional(v), nil
}