forked from ferranbt/fastssz
/
marshal.go
188 lines (162 loc) · 4.2 KB
/
marshal.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
package generator
import (
"fmt"
"strings"
)
// marshal creates a function that encodes the structs in SSZ format. It creates two functions:
// 1. MarshalTo(dst []byte) marshals the content to the target array.
// 2. Marshal() marshals the content to a newly created array.
func (e *env) marshal(name string, v *Value) string {
tmpl := `// MarshalSSZ ssz marshals the {{.name}} object
func (:: *{{.name}}) MarshalSSZ() ([]byte, error) {
return ssz.MarshalSSZ(::)
}
// MarshalSSZTo ssz marshals the {{.name}} object to a target array
func (:: *{{.name}}) MarshalSSZTo(buf []byte) (dst []byte, err error) {
dst = buf
{{.offset}}
{{.marshal}}
return
}`
data := map[string]interface{}{
"name": name,
"marshal": v.marshalContainer(true),
"offset": "",
}
if !v.isFixed() {
// offset is the position where the offset starts
data["offset"] = fmt.Sprintf("offset := int(%d)\n", v.fixedSize())
}
str := execTmpl(tmpl, data)
return appendObjSignature(str, v)
}
func (v *Value) marshal() string {
switch v.t {
case TypeContainer, TypeReference:
return v.marshalContainer(false)
case TypeBytes:
name := v.name
if v.c {
name += "[:]"
}
tmpl := `{{.validate}}dst = append(dst, ::.{{.name}}...)`
return execTmpl(tmpl, map[string]interface{}{
"validate": v.validate(),
"name": name,
})
case TypeUint:
var name string
if v.ref != "" || v.obj != "" {
// alias to Uint64
name = fmt.Sprintf("uint64(::.%s)", v.name)
} else {
name = "::." + v.name
}
return fmt.Sprintf("dst = ssz.Marshal%s(dst, %s)", uintVToName(v), name)
case TypeBitList:
return fmt.Sprintf("%sdst = append(dst, ::.%s...)", v.validate(), v.name)
case TypeBool:
return fmt.Sprintf("dst = ssz.MarshalBool(dst, ::.%s)", v.name)
case TypeVector:
if v.e.isFixed() {
return v.marshalVector()
}
fallthrough
case TypeList:
return v.marshalList()
case TypeTime:
return fmt.Sprintf("dst = ssz.MarshalTime(dst, ::.%s)", v.name)
default:
panic(fmt.Errorf("marshal not implemented for type %s", v.t.String()))
}
}
func (v *Value) marshalList() string {
v.e.name = v.name + "[ii]"
// bound check
str := v.validate()
if v.e.isFixed() {
tmpl := `for ii := 0; ii < len(::.{{.name}}); ii++ {
{{.dynamic}}
}`
str += execTmpl(tmpl, map[string]interface{}{
"name": v.name,
"dynamic": v.e.marshal(),
})
return str
}
// encode a list of dynamic objects:
// 1. write offsets for each
// 2. marshal each element
tmpl := `{
offset = 4 * len(::.{{.name}})
for ii := 0; ii < len(::.{{.name}}); ii++ {
dst = ssz.WriteOffset(dst, offset)
{{.size}}
}
}
for ii := 0; ii < len(::.{{.name}}); ii++ {
{{.marshal}}
}`
str += execTmpl(tmpl, map[string]interface{}{
"name": v.name,
"size": v.e.size("offset"),
"marshal": v.e.marshal(),
})
return str
}
func (v *Value) marshalVector() (str string) {
v.e.name = fmt.Sprintf("%s[ii]", v.name)
tmpl := `{{.validate}}for ii := 0; ii < {{.size}}; ii++ {
{{.marshal}}
}`
return execTmpl(tmpl, map[string]interface{}{
"validate": v.validate(),
"name": v.name,
"size": v.s,
"marshal": v.e.marshal(),
})
}
func (v *Value) marshalContainer(start bool) string {
if !start {
tmpl := `{{ if .check }}if ::.{{.name}} == nil {
::.{{.name}} = new({{.obj}})
}
{{ end }}if dst, err = ::.{{.name}}.MarshalSSZTo(dst); err != nil {
return
}`
// validate only for fixed structs
check := v.isFixed()
if v.isListElem() {
check = false
}
if v.noPtr {
check = false
}
return execTmpl(tmpl, map[string]interface{}{
"name": v.name,
"obj": v.objRef(),
"check": check,
})
}
offset := v.fixedSize()
out := []string{}
for indx, i := range v.o {
var str string
if i.isFixed() {
// write the content
str = fmt.Sprintf("// Field (%d) '%s'\n%s\n", indx, i.name, i.marshal())
} else {
// write the offset
str = fmt.Sprintf("// Offset (%d) '%s'\ndst = ssz.WriteOffset(dst, offset)\n%s\n", indx, i.name, i.size("offset"))
offset += i.fixedSize()
}
out = append(out, str)
}
// write the dynamic parts
for indx, i := range v.o {
if !i.isFixed() {
out = append(out, fmt.Sprintf("// Field (%d) '%s'\n%s\n", indx, i.name, i.marshal()))
}
}
return strings.Join(out, "\n")
}