Skip to content

Commit

Permalink
Reset to v1.1.55
Browse files Browse the repository at this point in the history
  • Loading branch information
ohill committed Oct 2, 2023
1 parent 6d7fe40 commit ab5758d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 251 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jobs:
- uses: actions/checkout@v3.5.3
- uses: actions/setup-go@v4.0.1
with:
go-version: '1.20.6'
go-version: '1.20.5'
- run: make test
24 changes: 0 additions & 24 deletions gen/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ 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) LessFunction() 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 }
Expand Down Expand Up @@ -230,9 +229,6 @@ type Elem interface {
// slice of this type.
SortInterface() string

// LessFunction returns the Less implementation for values of this type.
LessFunction() string

// Comparable returns whether the type is comparable, along the lines
// of the Go spec (https://golang.org/ref/spec#Comparison_operators),
// used to determine whether we can compare to a zero value to determine
Expand Down Expand Up @@ -799,8 +795,6 @@ func (s *BaseElem) BaseType() string {
return "[]byte"
case Time:
return "time.Time"
case Duration:
return "time.Duration"
case Ext:
return "msgp.Extension"

Expand Down Expand Up @@ -915,14 +909,6 @@ func (s *BaseElem) SortInterface() string {
return ""
}

func (s *BaseElem) LessFunction() string {
lessThan, ok := lessFunctions[s.TypeName()]
if ok {
return lessThan
}
return ""
}

func (k Primitive) String() string {
switch k {
case String:
Expand Down Expand Up @@ -1004,13 +990,3 @@ func SetSortInterface(sorttype string, sortintf string) {

sortInterface[sorttype] = sortintf
}

var lessFunctions map[string]string

func SetLessFunction(sorttype string, lessfn string) {
if lessFunctions == nil {
lessFunctions = make(map[string]string)
}

lessFunctions[sorttype] = lessfn
}
61 changes: 20 additions & 41 deletions gen/maxsize.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ func maxSizes(w io.Writer, topics *Topics) *maxSizeGen {

type maxSizeGen struct {
passes
p printer
state maxSizeState
ctx *Context
topics *Topics
panicked bool
p printer
state maxSizeState
ctx *Context
topics *Topics
}

func (s *maxSizeGen) Method() Method { return MaxSize }
Expand All @@ -54,7 +53,7 @@ func (s *maxSizeGen) Apply(dirs []string) error {
// this lets us chain together addition
// operations where possible
func (s *maxSizeGen) addConstant(sz string) {
if !s.p.ok() || s.panicked {
if !s.p.ok() {
return
}

Expand Down Expand Up @@ -83,7 +82,6 @@ func (s *maxSizeGen) Execute(p Elem) ([]string, error) {
if !s.p.ok() {
return nil, s.p.err
}
s.panicked = false
p = s.applyall(p)
if p == nil {
return nil, nil
Expand Down Expand Up @@ -111,17 +109,13 @@ func (s *maxSizeGen) Execute(p Elem) ([]string, error) {
s.p.printf("\nfunc %s (s int) {", getMaxSizeMethod(p.TypeName()))
s.state = assignM
next(s, p)
if s.panicked {
s.p.print("\n}\n")
} else {
s.p.nakedReturn()
}
s.p.nakedReturn()
s.topics.Add(p.TypeName(), getMaxSizeMethod(p.TypeName()))
return nil, s.p.err
}

func (s *maxSizeGen) gStruct(st *Struct) {
if !s.p.ok() || s.panicked {
if !s.p.ok() {
return
}

Expand All @@ -140,7 +134,7 @@ func (s *maxSizeGen) gStruct(st *Struct) {
continue
}

if !s.p.ok() || s.panicked {
if !s.p.ok() {
return
}
next(s, st.Fields[i].FieldElem)
Expand All @@ -162,23 +156,19 @@ func (s *maxSizeGen) gStruct(st *Struct) {
}

func (s *maxSizeGen) gPtr(p *Ptr) {
if s.panicked {
return
}
s.state = addM // inner must use add
next(s, p.Value)
s.state = addM // closing block; reset to add
}

func (s *maxSizeGen) gSlice(sl *Slice) {
if !s.p.ok() || s.panicked {
if !s.p.ok() {
return
}
s.state = addM
s.p.comment("Calculating size of slice: " + sl.Varname())
if (sl.AllocBound() == "" || sl.AllocBound() == "-") && (sl.MaxTotalBytes() == "" || sl.MaxTotalBytes() == "-") {
s.p.printf("\npanic(\"Slice %s is unbounded\")", sl.Varname())
s.panicked = true
s.state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
return
}
Expand All @@ -204,14 +194,13 @@ func (s *maxSizeGen) gSlice(sl *Slice) {
s.addConstant(fmt.Sprintf("((%s) * (%s))", topLevelAllocBound, str))
} else {
s.p.printf("\npanic(\"Unable to determine max size: %s\")", err)
s.panicked = true
}
s.state = addM
return
}

func (s *maxSizeGen) gArray(a *Array) {
if !s.p.ok() || s.panicked {
if !s.p.ok() {
return
}
// If this is not the first line where we define s = ... then we need to reset the state
Expand All @@ -227,23 +216,19 @@ func (s *maxSizeGen) gArray(a *Array) {
s.addConstant(fmt.Sprintf("((%s) * (%s))", a.Size, str))
} else {
s.p.printf("\npanic(\"Unable to determine max size: %s\")", err)
s.panicked = true

}
s.state = addM
return
}

func (s *maxSizeGen) gMap(m *Map) {
if s.panicked {
return
}
vn := m.Varname()
s.state = addM
s.addConstant(builtinSize(mapHeader))
topLevelAllocBound := m.AllocBound()
if topLevelAllocBound != "" && topLevelAllocBound == "-" {
s.p.printf("\npanic(\"Map %s is unbounded\")", m.Varname())
s.panicked = true
s.state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
return
}
Expand All @@ -256,25 +241,21 @@ func (s *maxSizeGen) gMap(m *Map) {
}
}

if !s.panicked {
s.p.comment("Adding size of map keys for " + vn)
s.p.printf("\ns += %s", topLevelAllocBound)
s.state = multM
next(s, m.Key)
}
s.p.comment("Adding size of map keys for " + vn)
s.p.printf("\ns += %s", topLevelAllocBound)
s.state = multM
next(s, m.Key)

if !s.panicked {
s.p.comment("Adding size of map values for " + vn)
s.p.printf("\ns += %s", topLevelAllocBound)
s.state = multM
next(s, m.Value)
}
s.p.comment("Adding size of map values for " + vn)
s.p.printf("\ns += %s", topLevelAllocBound)
s.state = multM
next(s, m.Value)

s.state = addM
}

func (s *maxSizeGen) gBase(b *BaseElem) {
if !s.p.ok() || s.panicked {
if !s.p.ok() {
return
}
if b.MaxTotalBytes() != "" {
Expand All @@ -295,7 +276,6 @@ func (s *maxSizeGen) gBase(b *BaseElem) {
value, err := baseMaxSizeExpr(b.Value, vname, b.BaseName(), b.TypeName(), b.common.AllocBound())
if err != nil {
s.p.printf("\npanic(\"Unable to determine max size: %s\")", err)
s.panicked = true
s.state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
return
}
Expand All @@ -310,7 +290,6 @@ func (s *maxSizeGen) gBase(b *BaseElem) {
value, err := baseMaxSizeExpr(b.Value, vname, b.BaseName(), b.TypeName(), b.common.AllocBound())
if err != nil {
s.p.printf("\npanic(\"Unable to determine max size: %s\")", err)
s.panicked = true
s.state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
return
}
Expand Down
50 changes: 1 addition & 49 deletions gen/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,12 @@ func (u *unmarshalGen) Execute(p Elem) ([]string, error) {
u.p.printf("\n return ((*(%s))(%s)).UnmarshalMsg(bts)", baseType, c)
u.p.printf("\n}")

u.p.printf("\nfunc (%s %s) UnmarshalValidateMsg(bts []byte) ([]byte, error) {", c, methodRecv)
u.p.printf("\n return ((*(%s))(%s)).UnmarshalValidateMsg(bts)", baseType, c)
u.p.printf("\n}")

u.p.printf("\nfunc (_ %[2]s) CanUnmarshalMsg(%[1]s interface{}) bool {", c, methodRecv)
u.p.printf("\n _, ok := (%s).(%s)", c, methodRecv)
u.p.printf("\n return ok")
u.p.printf("\n}")

u.topics.Add(methodRecv, "UnmarshalMsg")
u.topics.Add(methodRecv, "UnmarshalValidateMsg")
u.topics.Add(methodRecv, "CanUnmarshalMsg")

return u.msgs, u.p.err
Expand All @@ -80,7 +75,7 @@ func (u *unmarshalGen) Execute(p Elem) ([]string, error) {
c := p.Varname()
methodRecv := methodReceiver(p)

u.p.printf("\nfunc (%s %s) unmarshalMsg(bts []byte, validate bool) (o []byte, err error) {", c, methodRecv)
u.p.printf("\nfunc (%s %s) UnmarshalMsg(bts []byte) (o []byte, err error) {", c, methodRecv)
next(u, p)
u.p.print("\no = bts")

Expand All @@ -96,21 +91,12 @@ func (u *unmarshalGen) Execute(p Elem) ([]string, error) {
}
u.p.nakedReturn()

u.p.printf("\nfunc (%s %s) UnmarshalMsg(bts []byte) (o []byte, err error) {", c, methodRecv)
u.p.printf("\n return %s.unmarshalMsg(bts, false)", c)
u.p.printf("\n}")

u.p.printf("\nfunc (%s %s) UnmarshalValidateMsg(bts []byte) (o []byte, err error) {", c, methodRecv)
u.p.printf("\n return %s.unmarshalMsg(bts, true)", c)
u.p.printf("\n}")

u.p.printf("\nfunc (_ %[2]s) CanUnmarshalMsg(%[1]s interface{}) bool {", c, methodRecv)
u.p.printf("\n _, ok := (%s).(%s)", c, methodRecv)
u.p.printf("\n return ok")
u.p.printf("\n}")

u.topics.Add(methodRecv, "UnmarshalMsg")
u.topics.Add(methodRecv, "UnmarshalValidateMsg")
u.topics.Add(methodRecv, "CanUnmarshalMsg")

return u.msgs, u.p.err
Expand Down Expand Up @@ -158,13 +144,8 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
u.needsField()
sz := randIdent()
isnil := randIdent()
last := randIdent()
lastIsSet := randIdent()
u.p.declare(sz, "int")
u.p.declare(last, "string")
u.p.declare(lastIsSet, "bool")
u.p.declare(isnil, "bool")
u.p.printf("\n_=%s;\n_=%s", last, lastIsSet) // we might not use these for empty structs

// go-codec compat: decode an array as sequential elements from this struct,
// in the order they are defined in the Go type (as opposed to canonical
Expand All @@ -174,11 +155,6 @@ func (u *unmarshalGen) mapstruct(s *Struct) {

u.assignAndCheck(sz, isnil, arrayHeader)

u.p.print("\nif validate {") // map encoded as array => non canonical
u.p.print("\nerr = &msgp.ErrNonCanonical{}")
u.p.print("\nreturn")
u.p.print("\n}")

u.ctx.PushString("struct-from-array")
for i := range s.Fields {
if !ast.IsExported(s.Fields[i].FieldName) {
Expand Down Expand Up @@ -219,19 +195,13 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
return
}
u.p.printf("\ncase \"%s\":", s.Fields[i].FieldTag)
u.p.printf("\nif validate && %s && \"%s\" < %s {", lastIsSet, s.Fields[i].FieldTag, last)
u.p.print("\nerr = &msgp.ErrNonCanonical{}")
u.p.printf("\nreturn")
u.p.print("\n}")
u.ctx.PushString(s.Fields[i].FieldName)
next(u, s.Fields[i].FieldElem)
u.ctx.Pop()
u.p.printf("\n%s = \"%s\"", last, s.Fields[i].FieldTag)
}
u.p.print("\ndefault:\nerr = msgp.ErrNoField(string(field))")
u.p.wrapErrCheck(u.ctx.ArgsStr())
u.p.print("\n}") // close switch
u.p.printf("\n%s = true", lastIsSet)
u.p.print("\n}") // close for loop
u.p.print("\n}") // close else statement for array decode
}
Expand Down Expand Up @@ -355,27 +325,9 @@ func (u *unmarshalGen) gMap(m *Map) {
u.msgs = append(u.msgs, resizemsgs...)

// loop and get key,value
last := randIdent()
lastSet := randIdent()
u.p.printf("\nvar %s %s; _ = %s", last, m.Key.TypeName(), last) // we might not use the sort if it's not defined
u.p.declare(lastSet, "bool")
u.p.printf("\n_ = %s", lastSet) // we might not use the flag
u.p.printf("\nfor %s > 0 {", sz)
u.p.printf("\nvar %s %s; var %s %s; %s--", m.Keyidx, m.Key.TypeName(), m.Validx, m.Value.TypeName(), sz)
next(u, m.Key)
u.p.printf("\nif validate {")
if m.Key.LessFunction() != "" {
u.p.printf("\nif %s && %s(%s, %s) {", lastSet, m.Key.LessFunction(), m.Keyidx, last)
u.p.printf("\nerr = &msgp.ErrNonCanonical{}")
u.p.printf("\nreturn")
u.p.printf("\n}")
} else {
u.p.printf("\nerr = &msgp.ErrMissingLessFn{}")
u.p.printf("\nreturn")
}
u.p.printf("\n}") // close if validate block
u.p.printf("\n%s=%s", last, m.Keyidx)
u.p.printf("\n%s=true", lastSet)
u.ctx.PushVar(m.Keyidx)
next(u, m.Value)
u.ctx.Pop()
Expand Down
Loading

0 comments on commit ab5758d

Please sign in to comment.