If a user passes a nil extension value to SetExtension(), return a useful error message.
The previous behavior was to silently drop all extensions from the encoded message.
diff --git a/proto/extensions_test.go b/proto/extensions_test.go
index 451ad87..6495f56 100644
--- a/proto/extensions_test.go
+++ b/proto/extensions_test.go
@@ -135,3 +135,19 @@
t.Error("expected some sort of type mismatch error, got nil")
}
}
+
+func TestNilExtension(t *testing.T) {
+ msg := &pb.MyMessage{
+ Count: proto.Int32(1),
+ }
+ if err := proto.SetExtension(msg, pb.E_Ext_Text, proto.String("hello")); err != nil {
+ t.Fatal(err)
+ }
+ if err := proto.SetExtension(msg, pb.E_Ext_More, (*pb.Ext)(nil)); err == nil {
+ t.Error("expected SetExtension to fail due to a nil extension")
+ } else if want := "proto: SetExtension called with nil value of type *testdata.Ext"; err.Error() != want {
+ t.Errorf("expected error %v, got %v", want, err)
+ }
+ // Note: if the behavior of Marshal is ever changed to ignore nil extensions, update
+ // this test to verify that E_Ext_Text is properly propagated through marshal->unmarshal.
+}