Skip to content

Commit

Permalink
Generate max-size constants for dynamic fields
Browse files Browse the repository at this point in the history
For example, for the struct:
```
type Log struct {
    Data []Uint256 `json:"data" ssz-max:"6000"`
}
```
Will be generated the following constant in `log_encoding.go`: `const LogMaxDataSize = 6000`

Signed-off-by: Mikhail Sherstennikov <[email protected]>
  • Loading branch information
shermike committed Nov 27, 2024
1 parent 181ec7e commit f18caf3
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
99 changes: 99 additions & 0 deletions spectests/structs_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions sszgen/generator/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,29 @@ func (e *env) size(name string, v *Value) string {
{{.dynamic}}
{{end}}
return
}`
}
{{.fieldsMaxSizes}}
`

str := execTmpl(tmpl, map[string]interface{}{
"name": name,
"fixed": v.fixedSize(),
"dynamic": v.sizeContainer("size", true),
"name": name,
"fixed": v.fixedSize(),
"dynamic": v.sizeContainer("size", true),
"fieldsMaxSizes": v.fieldsMaxSizes(name),
})
return appendObjSignature(str, v)
}

func (v *Value) fieldsMaxSizes(name string) string {
out := []string{}
for _, v := range v.o {
if !v.isFixed() {
out = append(out, fmt.Sprintf("const %sMax%sSize = %d", name, v.name, v.s))
}
}
return strings.Join(out, "\n")
}

func (v *Value) fixedSize() uint64 {
switch v.t {
case TypeVector:
Expand Down
4 changes: 4 additions & 0 deletions tests/codetrie_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f18caf3

Please sign in to comment.