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 11966c6
Showing 1 changed file with 17 additions and 4 deletions.
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

0 comments on commit 11966c6

Please sign in to comment.