Skip to content

Commit

Permalink
Implement XyzMaxSize() generator
Browse files Browse the repository at this point in the history
- Adds maxsize.go that generates {typeName}MaxSize() functions
- Adds maxtotalbytes annotation that limits the total number of bytes
  that a struct field can encode to
- Adds allocbound handling to string types
  • Loading branch information
iansuvak authored and algorandskiy committed Jun 22, 2023
1 parent 883d17f commit f1cd809
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 164 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ msgp/cover.out
*~
*.coverprofile
.idea/
cover.out
.vscode/
cover.out
40 changes: 26 additions & 14 deletions gen/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,23 @@ func (c Callback) GetName() string { return c.Fname }

// common data/methods for every Elem
type common struct {
vname, alias string
allocbound string
callbacks []Callback
}

func (c *common) SetVarname(s string) { c.vname = s }
func (c *common) Varname() string { return c.vname }
func (c *common) Alias(typ string) { c.alias = typ }
func (c *common) SortInterface() string { return "" }
func (c *common) SetAllocBound(s string) { c.allocbound = s }
func (c *common) AllocBound() string { return c.allocbound }
func (c *common) GetCallbacks() []Callback { return c.callbacks }
func (c *common) AddCallback(cb Callback) { c.callbacks = append(c.callbacks, cb) }
func (c *common) hidden() {}
vname, alias string
allocbound string
maxtotalbytes string
callbacks []Callback
}

func (c *common) SetVarname(s string) { c.vname = s }
func (c *common) Varname() string { return c.vname }
func (c *common) Alias(typ string) { c.alias = typ }
func (c *common) SortInterface() string { return "" }
func (c *common) SetAllocBound(s string) { c.allocbound = s }
func (c *common) AllocBound() string { return c.allocbound }
func (c *common) SetMaxTotalBytes(s string) { c.maxtotalbytes = s }
func (c *common) MaxTotalBytes() string { return c.maxtotalbytes }
func (c *common) GetCallbacks() []Callback { return c.callbacks }
func (c *common) AddCallback(cb Callback) { c.callbacks = append(c.callbacks, cb) }
func (c *common) hidden() {}

func IsDangling(e Elem) bool {
if be, ok := e.(*BaseElem); ok && be.Dangling() {
Expand Down Expand Up @@ -241,6 +244,15 @@ type Elem interface {
// when decoding this type. Meaningful for slices and maps.
AllocBound() string

// SetMaxTotalBytes specifies the maximum number of bytes to allocate when
// decoding this type.
// Blank means unspecified bound. "-" means no bound.
SetMaxTotalBytes(bound string)

// MaxTotalBytes specifies the maximum number of bytes to allocate when
// decoding this type. Meaningful for slices of strings or byteslices.
MaxTotalBytes() string

// AddCallback adds to the elem a Callback it should call at the end of marshaling
AddCallback(Callback)

Expand Down
Loading

0 comments on commit f1cd809

Please sign in to comment.