-
Notifications
You must be signed in to change notification settings - Fork 3
/
typetag.go
242 lines (201 loc) · 4.33 KB
/
typetag.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
package aptos
import (
"fmt"
"strings"
)
type TypeTagType uint64
const (
TypeTag_Bool TypeTagType = 0
TypeTag_U8 TypeTagType = 1
TypeTag_U64 TypeTagType = 2
TypeTag_U128 TypeTagType = 3
TypeTag_AccountAddress TypeTagType = 4
TypeTag_Signer TypeTagType = 5
TypeTag_Vector TypeTagType = 6
TypeTag_Struct TypeTagType = 7
TypeTag_U16 TypeTagType = 8
TypeTag_U32 TypeTagType = 9
TypeTag_U256 TypeTagType = 10
)
type TypeTagImpl interface {
BCSStruct
GetType() TypeTagType
String() string
}
type TypeTag struct {
Value TypeTagImpl
}
func (tt *TypeTag) MarshalBCS(bcs *Serializer) {
bcs.Uleb128(uint32(tt.Value.GetType()))
tt.Value.MarshalBCS(bcs)
}
func (tt *TypeTag) UnmarshalBCS(bcs *Deserializer) {
variant := bcs.Uleb128()
switch TypeTagType(variant) {
case TypeTag_Bool:
xt := &BoolTag{}
xt.UnmarshalBCS(bcs)
tt.Value = xt
case TypeTag_U8:
xt := &U8Tag{}
xt.UnmarshalBCS(bcs)
tt.Value = xt
case TypeTag_U16:
xt := &U16Tag{}
xt.UnmarshalBCS(bcs)
tt.Value = xt
case TypeTag_U32:
xt := &U32Tag{}
xt.UnmarshalBCS(bcs)
tt.Value = xt
case TypeTag_U64:
xt := &U64Tag{}
xt.UnmarshalBCS(bcs)
tt.Value = xt
case TypeTag_Struct:
xt := &StructTag{}
xt.UnmarshalBCS(bcs)
tt.Value = xt
default:
bcs.SetError(fmt.Errorf("unknown TypeTag enum %d", variant))
}
}
func (tt *TypeTag) String() string {
return tt.Value.String()
}
func NewTypeTag(v any) *TypeTag {
switch tv := v.(type) {
case uint8:
return &TypeTag{
Value: &U8Tag{Value: tv},
}
}
return nil
}
type BoolTag struct {
Value bool
}
func (xt *BoolTag) String() string {
return "bool"
}
func (xt *BoolTag) GetType() TypeTagType {
return TypeTag_Bool
}
func (xt *BoolTag) MarshalBCS(bcs *Serializer) {
bcs.Bool(xt.Value)
}
func (xt *BoolTag) UnmarshalBCS(bcs *Deserializer) {
xt.Value = bcs.Bool()
}
type U8Tag struct {
Value uint8
}
func (xt *U8Tag) String() string {
return "u8"
}
func (xt *U8Tag) GetType() TypeTagType {
return TypeTag_U8
}
func (xt *U8Tag) MarshalBCS(bcs *Serializer) {
bcs.U8(xt.Value)
}
func (xt *U8Tag) UnmarshalBCS(bcs *Deserializer) {
xt.Value = bcs.U8()
}
type U16Tag struct {
Value uint16
}
func (xt *U16Tag) String() string {
return "u16"
}
func (xt *U16Tag) GetType() TypeTagType {
return TypeTag_U16
}
func (xt *U16Tag) MarshalBCS(bcs *Serializer) {
bcs.U16(xt.Value)
}
func (xt *U16Tag) UnmarshalBCS(bcs *Deserializer) {
xt.Value = bcs.U16()
}
type U32Tag struct {
Value uint32
}
func (xt *U32Tag) String() string {
return "u32"
}
func (xt *U32Tag) GetType() TypeTagType {
return TypeTag_U32
}
func (xt *U32Tag) MarshalBCS(bcs *Serializer) {
bcs.U32(xt.Value)
}
func (xt *U32Tag) UnmarshalBCS(bcs *Deserializer) {
xt.Value = bcs.U32()
}
type U64Tag struct {
Value uint64
}
func (xt *U64Tag) String() string {
return "u64"
}
func (xt *U64Tag) GetType() TypeTagType {
return TypeTag_U64
}
func (xt *U64Tag) MarshalBCS(bcs *Serializer) {
bcs.U64(xt.Value)
}
func (xt *U64Tag) UnmarshalBCS(bcs *Deserializer) {
xt.Value = bcs.U64()
}
type AccountAddressTag struct {
Value AccountAddress
}
func (xt *AccountAddressTag) GetType() TypeTagType {
return TypeTag_AccountAddress
}
func (xt *AccountAddressTag) MarshalBCS(bcs *Serializer) {
xt.Value.MarshalBCS(bcs)
}
func (xt *AccountAddressTag) UnmarshalBCS(bcs *Deserializer) {
xt.Value.UnmarshalBCS(bcs)
}
type StructTag struct {
Address AccountAddress
Module string
Name string
TypeParams []TypeTag
}
func (st *StructTag) MarshalBCS(bcs *Serializer) {
st.Address.MarshalBCS(bcs)
bcs.WriteString(st.Module)
bcs.WriteString(st.Name)
SerializeSequence(st.TypeParams, bcs)
}
func (st *StructTag) UnmarshalBCS(bcs *Deserializer) {
st.Address.UnmarshalBCS(bcs)
st.Module = bcs.ReadString()
st.Name = bcs.ReadString()
st.TypeParams = DeserializeSequence[TypeTag](bcs)
}
func (st *StructTag) GetType() TypeTagType {
return TypeTag_Struct
}
func (st *StructTag) String() string {
out := strings.Builder{}
out.WriteString(st.Address.String())
out.WriteString("::")
out.WriteString(st.Module)
out.WriteString("::")
out.WriteString(st.Name)
if len(st.TypeParams) != 0 {
out.WriteRune('<')
for i, tp := range st.TypeParams {
if i != 0 {
out.WriteRune(',')
}
out.WriteString(tp.String())
}
out.WriteRune('>')
}
return out.String()
}