Add specific error for oneof with nil element. Signed-off-by: David Symonds <dsymonds@golang.org>
diff --git a/proto/all_test.go b/proto/all_test.go index 4888308..18eeb0f 100644 --- a/proto/all_test.go +++ b/proto/all_test.go
@@ -1329,9 +1329,18 @@ func TestTypedNilMarshal(t *testing.T) { // A typed nil should return ErrNil and not crash. - _, err := Marshal((*GoEnum)(nil)) - if err != ErrNil { - t.Errorf("Marshal: got err %v, want ErrNil", err) + { + var m *GoEnum + if _, err := Marshal(m); err != ErrNil { + t.Errorf("Marshal(%#v): got %v, want ErrNil", m, err) + } + } + + { + m := &Communique{Union: &Communique_Msg{nil}} + if _, err := Marshal(m); err == nil || err == ErrNil { + t.Errorf("Marshal(%#v): got %v, want errOneofHasNil", m, err) + } } }
diff --git a/proto/encode.go b/proto/encode.go index 231b074..eb7e047 100644 --- a/proto/encode.go +++ b/proto/encode.go
@@ -64,6 +64,10 @@ // a struct with a repeated field containing a nil element. errRepeatedHasNil = errors.New("proto: repeated field has nil element") + // errOneofHasNil is the error returned if Marshal is called with + // a struct with a oneof field containing a nil element. + errOneofHasNil = errors.New("proto: oneof field has nil value") + // ErrNil is the error returned if Marshal is called with nil. ErrNil = errors.New("proto: Marshal called with nil") ) @@ -1222,7 +1226,9 @@ // Do oneof fields. if prop.oneofMarshaler != nil { m := structPointer_Interface(base, prop.stype).(Message) - if err := prop.oneofMarshaler(m, o); err != nil { + if err := prop.oneofMarshaler(m, o); err == ErrNil { + return errOneofHasNil + } else if err != nil { return err } }