Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 4 additions & 33 deletions gen/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func (u *unmarshalGen) gStruct(s *Struct) {
// option, a check that the field holds a non-zero value after decoding. A
// required field that is still zero (because it was absent from the encoded
// object, or encoded as a zero value) is a decode error. This runs after the
// struct body has been decoded, so it applies uniformly to the map,
// struct-from-array, and tuple decode paths.
// struct body has been decoded, so it applies uniformly to the map and tuple

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the "tuple" path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can specify //msgp:tuple and have a struct encoded as a positional array. It doesn't look like we've ever used this feature though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can/should we remove that as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel less concerned about it, as it is an explicit opt-in, and not automatic fallback behavior that is being generated whether you specified array/tuple encoding or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I could remove it

// decode paths.
func (u *unmarshalGen) required(s *Struct) {
for i := range s.Fields {
if !u.p.ok() {
Expand Down Expand Up @@ -207,36 +207,8 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
u.p.declare(sz, "int")
u.p.declare(isnil, "bool")

// 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
// order by sorted tag).
u.p.printf("\n%s, %s, bts, err = msgp.Read%sBytes(bts)", sz, isnil, mapHeader)
u.p.printf("\nif _, ok := err.(msgp.TypeError); ok {")

u.assignAndCheck(sz, isnil, arrayHeader)

u.ctx.PushString("struct-from-array")
for i := range s.Fields {
if !ast.IsExported(s.Fields[i].FieldName) {
continue
}

u.p.printf("\nif %s > 0 {", sz)
u.p.printf("\n%s--", sz)
u.ctx.PushString(s.Fields[i].FieldName)
next(u, s.Fields[i].FieldElem)
u.ctx.Pop()
u.p.printf("\n}")
}

u.p.printf("\nif %s > 0 {", sz)
u.p.printf("\nerr = msgp.ErrTooManyArrayFields(%s)", sz)
u.p.wrapErrCheck(u.ctx.ArgsStr())
u.p.printf("\n}")
u.ctx.Pop()

u.p.printf("\n} else {")
u.p.wrapErrCheck(u.ctx.ArgsStr())
// Decode the struct as a msgpack map only
u.assignAndCheck(sz, isnil, mapHeader)

u.p.printf("\nif %s {", isnil)
u.p.printf("\n %s = %s{}", s.Varname(), s.TypeName())
Expand All @@ -263,7 +235,6 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
u.p.wrapErrCheck(u.ctx.ArgsStr())
u.p.print("\n}") // close switch
u.p.print("\n}") // close for loop
u.p.print("\n}") // close else statement for array decode
}

func (u *unmarshalGen) gBase(b *BaseElem) {
Expand Down
71 changes: 71 additions & 0 deletions tests/array_decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package tests

import (
"testing"

"github.com/algorand/msgp/msgp"
)

func wantArrayForMapError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatal("expected array-encoded map-struct to be rejected, got nil")
}
te, ok := msgp.Cause(err).(msgp.TypeError)
if !ok {
t.Fatalf("expected msgp.TypeError, got %T: %v", msgp.Cause(err), err)
}
if te.Method != msgp.MapType || te.Encoded != msgp.ArrayType {
t.Fatalf("expected map-wanted/array-encoded TypeError, got %+v", te)
}
}

func TestMapStructRejectsArray(t *testing.T) {
// A single-field map-struct encoded as a one-element array.
bts := msgp.AppendArrayHeader(nil, 1)
bts = msgp.AppendString(bts, "hello")

var out Inner
_, err := out.UnmarshalMsg(bts)
wantArrayForMapError(t, err)
if out.X != "" {
t.Fatalf("rejected decode must not populate fields, got X=%q", out.X)
}
}

func TestMapStructRejectsMultiFieldArray(t *testing.T) {
// An array whose elements line up positionally with Go field order.
bts := msgp.AppendArrayHeader(nil, 2)
bts = msgp.AppendString(bts, "somereq")
bts = msgp.AppendString(bts, "someopt")

var out MapRequiredOmitEmpty
_, err := out.UnmarshalMsg(bts)
wantArrayForMapError(t, err)
}

func TestMapStructRejectsEmptyArray(t *testing.T) {
// A zero-length array. msgp once special-cased mfixarray(0) inside
// ReadMapHeaderBytes as an empty map for go-codec parity; that special case
// was folded into the now-removed fallback, so a zero-length array is a
// plain map/array type mismatch today.
bts := msgp.AppendArrayHeader(nil, 0)

var out Inner
_, err := out.UnmarshalMsg(bts)
wantArrayForMapError(t, err)
}

func TestTupleStructStillDecodesArray(t *testing.T) {
// Contrast: a struct that genuinely opts into array encoding via
// //msgp:tuple still round-trips through its array form. Only the implicit
// map-struct fallback was removed, not real tuple support.
in := TupleRequired{A: "x", B: 7}
var out TupleRequired
if _, err := out.UnmarshalMsg(in.MarshalMsg(nil)); err != nil {
t.Fatalf("expected tuple array decode to succeed, got %v", err)
}
if out != in {
t.Fatalf("tuple round trip mismatch: got %+v, want %+v", out, in)
}
}
Loading
Loading