From ab5758d5dbecfd25700d59d3a4ab6fec1a95b685 Mon Sep 17 00:00:00 2001 From: "145173879+ohill@users.noreply.github.com" <145173879+ohill@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:50:07 -0400 Subject: [PATCH] Reset to v1.1.55 --- .github/workflows/build.yml | 2 +- gen/elem.go | 24 ------------ gen/maxsize.go | 61 ++++++++++------------------- gen/unmarshal.go | 50 +----------------------- msgp/defs.go | 76 +++---------------------------------- msgp/errors.go | 27 +------------ msgp/read.go | 8 ---- parse/directives.go | 35 ++--------------- 8 files changed, 32 insertions(+), 251 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a6eda27..9afcca9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/gen/elem.go b/gen/elem.go index c7d1b97..707b56b 100644 --- a/gen/elem.go +++ b/gen/elem.go @@ -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 } @@ -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 @@ -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" @@ -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: @@ -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 -} diff --git a/gen/maxsize.go b/gen/maxsize.go index 0fd3dbd..5052249 100644 --- a/gen/maxsize.go +++ b/gen/maxsize.go @@ -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 } @@ -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 } @@ -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 @@ -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 } @@ -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) @@ -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 } @@ -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 @@ -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 } @@ -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() != "" { @@ -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 } @@ -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 } diff --git a/gen/unmarshal.go b/gen/unmarshal.go index b86b2ca..4ad20f4 100644 --- a/gen/unmarshal.go +++ b/gen/unmarshal.go @@ -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 @@ -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") @@ -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 @@ -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 @@ -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) { @@ -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 } @@ -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() diff --git a/msgp/defs.go b/msgp/defs.go index 7660d59..b1188b0 100644 --- a/msgp/defs.go +++ b/msgp/defs.go @@ -5,19 +5,16 @@ // generator implement the Marshaler/Unmarshaler and Encodable/Decodable interfaces. // // This package defines four "families" of functions: -// - AppendXxxx() appends an object to a []byte in MessagePack encoding. -// - ReadXxxxBytes() reads an object from a []byte and returns the remaining bytes. -// - (*Writer).WriteXxxx() writes an object to the buffered *Writer type. -// - (*Reader).ReadXxxx() reads an object from a buffered *Reader type. +// - AppendXxxx() appends an object to a []byte in MessagePack encoding. +// - ReadXxxxBytes() reads an object from a []byte and returns the remaining bytes. +// - (*Writer).WriteXxxx() writes an object to the buffered *Writer type. +// - (*Reader).ReadXxxx() reads an object from a buffered *Reader type. // // Once a type has satisfied the `Encodable` and `Decodable` interfaces, // it can be written and read from arbitrary `io.Writer`s and `io.Reader`s using -// -// msgp.Encode(io.Writer, msgp.Encodable) -// +// msgp.Encode(io.Writer, msgp.Encodable) // and -// -// msgp.Decode(io.Reader, msgp.Decodable) +// msgp.Decode(io.Reader, msgp.Decodable) // // There are also methods for converting MessagePack to JSON without // an explicit de-serialization step. @@ -26,8 +23,6 @@ // the wiki at http://github.com/tinylib/msgp package msgp -import "bytes" - const last4 = 0x0f const first4 = 0xf0 const last5 = 0x1f @@ -145,62 +140,3 @@ const ( mmap16 uint8 = 0xde mmap32 uint8 = 0xdf ) - -// The following section defines exported LessFns for built-in types to be used as a convenience -// when using the msgp:sort directive. - -func IntLess(a, b int) bool { - return a < b -} - -func Int8Less(a, b int8) bool { - return a < b -} - -func Int16Less(a, b int16) bool { - return a < b -} - -func Int32Less(a, b int32) bool { - return a < b -} - -func Int64Less(a, b int64) bool { - return a < b -} - -func UintLess(a, b uint) bool { - return a < b -} - -func Uint8Less(a, b uint8) bool { - return a < b -} - -func Uint16Less(a, b uint16) bool { - return a < b -} - -func Uint32Less(a, b uint32) bool { - return a < b -} - -func Uint64Less(a, b uint64) bool { - return a < b -} - -func Float32Less(a, b float32) bool { - return a < b -} - -func Float64Less(a, b float64) bool { - return a < b -} - -func BytesLess(a, b []byte) bool { - return bytes.Compare(a, b) < 0 -} - -func StringLess(a, b string) bool { - return a < b -} diff --git a/msgp/errors.go b/msgp/errors.go index da63cf2..39d9286 100644 --- a/msgp/errors.go +++ b/msgp/errors.go @@ -81,6 +81,7 @@ func Resumable(e error) bool { // // ErrShortBytes is not wrapped with any context due to backward compatibility // issues with the public API. +// func WrapError(err error, ctx ...interface{}) error { switch e := err.(type) { case errShort: @@ -343,29 +344,3 @@ func (e *ErrUnsupportedType) withContext(ctx string) error { o.ctx = addCtx(o.ctx, ctx) return &o } - -// ErrNonCanonical is returned -// when unmarshaller detects that -// the message is not canonically encoded (pre-sorted) -type ErrNonCanonical struct{} - -// Error implements error -func (e *ErrNonCanonical) Error() string { - return fmt.Sprintf("msgp: non-canonical encoding detected") -} - -// Resumable returns false for errNonCanonical -func (e *ErrNonCanonical) Resumable() bool { return false } - -// ErrNonCanonical is returned -// when unmarshaller detects that -// the message is not canonically encoded (pre-sorted) -type ErrMissingLessFn struct{} - -// Error implements error -func (e *ErrMissingLessFn) Error() string { - return fmt.Sprintf("msgp: can't validate canonicity: missing LessFn") -} - -// Resumable returns false for errNonCanonical -func (e *ErrMissingLessFn) Resumable() bool { return false } diff --git a/msgp/read.go b/msgp/read.go index b99aff8..1eed864 100644 --- a/msgp/read.go +++ b/msgp/read.go @@ -80,11 +80,3 @@ type Unmarshaler interface { UnmarshalMsg([]byte) ([]byte, error) CanUnmarshalMsg(o interface{}) bool } - -// UnmarshalerValidator extends the Unmarshaler interface -// and requires an additional UnmarshalValidateMsg method -// that checks whether the encoded bytes follow canonical encoding rules -type UnmarshalerValidator interface { - Unmarshaler - UnmarshalValidateMsg([]byte) ([]byte, error) -} diff --git a/parse/directives.go b/parse/directives.go index 1ce45b2..75d0011 100644 --- a/parse/directives.go +++ b/parse/directives.go @@ -31,31 +31,12 @@ var directives = map[string]directive{ _postunmarshalcheck: postunmarshalcheck, } -// map of base types with predefined LessFunctions used in the sort directive -var lessFns = map[string]string{ - "int": "msgp.IntLess", - "int8": "msgp.Int8Less", - "int16": "msgp.Int16Less", - "int32": "msgp.Int32Less", - "int64": "msgp.Int64Less", - "uint": "msgp.UintLess", - "uint8": "msgp.Uint8Less", - "uint16": "msgp.Uint16Less", - "uint32": "msgp.Uint32Less", - "uint64": "msgp.Uint64Less", - "float32": "msgp.Float32Less", - "float64": "msgp.Float64Less", - "string": "msgp.StringLess", - "[]byte": "msgp.BytesLess", -} - const _postunmarshalcheck = "postunmarshalcheck" var errNotEnoughArguments = errors.New("postunmarshalcheck did not receive enough arguments. expected at least 3") -// the functions should have no params, and output zero. -// //msgp:postunmarshalcheck {Type} {funcName} {funcName} ... +// the functions should have no params, and output zero. func postunmarshalcheck(text []string, f *FileSet) error { if len(text) < 3 { return errNotEnoughArguments @@ -183,25 +164,15 @@ func astuple(text []string, f *FileSet) error { return nil } -//msgp:sort {Type} {SortInterface} {LessFunction} +//msgp:sort {Type} {SortInterface} func sortintf(text []string, f *FileSet) error { - if len(text) != 4 && len(text) != 3 { + if len(text) != 3 { return nil } sortType := strings.TrimSpace(text[1]) sortIntf := strings.TrimSpace(text[2]) gen.SetSortInterface(sortType, sortIntf) infof("sorting %s using %s\n", sortType, sortIntf) - var lessFn string - if len(text) == 4 { - lessFn = strings.TrimSpace(text[3]) - } else if fn, ok := lessFns[sortType]; ok { - lessFn = fn - } else { - panic(fmt.Sprintf("no default less function for %s and no function is provided", sortType)) - } - gen.SetLessFunction(sortType, lessFn) - infof("less fn %s using %s\n", sortType, lessFn) return nil }