Skip to content

proto: fix and cleanup test for deterministic marshal with custom marshalers #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2018
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
18 changes: 13 additions & 5 deletions proto/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2303,14 +2303,22 @@ func TestInvalidUTF8(t *testing.T) {
}
}

type CustomRawMessage []byte

func (m *CustomRawMessage) Marshal() ([]byte, error) {
return []byte(*m), nil
}
func (m *CustomRawMessage) Reset() { *m = nil }
func (m *CustomRawMessage) String() string { return fmt.Sprintf("%x", *m) }
func (m *CustomRawMessage) ProtoMessage() {}

func TestDeterministicErrorOnCustomMarshaler(t *testing.T) {
u := uint64(0)
in := &CustomDeterministicMarshaler{Field1: &u}
in := CustomRawMessage{1, 2, 3}
var b1 Buffer
b1.SetDeterministic(true)
err := b1.Marshal(in)
if !strings.Contains(err.Error(), "deterministic") {
t.Fatalf("Expected: %s but got %s", "proto: deterministic not supported by the Marshal method of test_proto.CustomDeterministicMarshaler", err.Error())
err := b1.Marshal(&in)
if err == nil || !strings.Contains(err.Error(), "deterministic") {
t.Fatalf("Marshal error:\ngot %v\nwant deterministic not supported error", err)
}
}

Expand Down
8 changes: 5 additions & 3 deletions proto/table_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte
// If the message can marshal itself, let it do it, for compatibility.
// NOTE: This is not efficient.
if u.hasmarshaler {
if deterministic {
return nil, errors.New("proto: deterministic not supported by the Marshal method of " + u.typ.String())
}
m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
b1, err := m.Marshal()
b = append(b, b1...)
Expand Down Expand Up @@ -2718,6 +2715,11 @@ func Marshal(pb Message) ([]byte, error) {
// a Buffer for most applications.
func (p *Buffer) Marshal(pb Message) error {
var err error
if p.deterministic {
if _, ok := pb.(Marshaler); ok {
return fmt.Errorf("proto: deterministic not supported by the Marshal method of %T", pb)
}
}
if m, ok := pb.(newMarshaler); ok {
siz := m.XXX_Size()
p.grow(siz) // make sure buf has enough capacity
Expand Down
5 changes: 0 additions & 5 deletions proto/test_proto/deterministic.go

This file was deleted.

Loading