Skip to content

Ordering shouldn't matter when specify field numbers #34

@uluyol

Description

@uluyol

If I have the following test:

type TestMesg struct {
	F1 string            `protobuf:"1,opt"`
	F2 int32             `protobuf:"10,opt"`
	F4 []byte            `protobuf:"99,opt"`
	F3 protobuf.Ufixed64 `protobuf:"14,opt"`
	F5 float64           `protobuf:"108,opt"`
}

func TestEncodeRoundTrip(t *testing.T) {
	m := TestMesg{
		F1: "abasdfasdf312123",
		F2: 109,
		F3: protobuf.Ufixed64(^uint64(0)),
		F4: []byte{0, 12, 44},
		F5: 505.5,
	}

	buf, err := protobuf.Encode(&m)
	if err != nil {
		t.Errorf("unable to encode")
	}

	var m2 TestMesg
	if err := protobuf.Decode(buf, &m2); err != nil {
		t.Errorf("unable to decode")
	}

	if !reflect.DeepEqual(&m, &m2) {
		t.Errorf("written and read msg differ:\nhave %+v\nwant %+v", &m2, &m)
	}
}

Running it fails with

--- FAIL: TestEncodeRoundTrip (0.00s)
	pb_test.go:38: written and read msg differ:
		have &{F1:abasdfasdf312123 F2:109 F4:[0 12 44] F3:0 F5:505.5}
		want &{F1:abasdfasdf312123 F2:109 F4:[0 12 44] F3:18446744073709551615 F5:505.5}

But if I switch the order of F3 and F4 then it passes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions