Add specific error for oneof with nil element.
Signed-off-by: David Symonds <dsymonds@golang.org>
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
}
}