Sync from internal version.
- fix nil Marshaler fields
- fix text encoding of nil
R=r
CC=golang-dev
http://codereview.appspot.com/2023043
diff --git a/proto/all_test.go b/proto/all_test.go
index 39abc77..e3b9d22 100644
--- a/proto/all_test.go
+++ b/proto/all_test.go
@@ -39,6 +39,7 @@
import (
"bytes"
"fmt"
+ "os"
"testing"
. "goprotobuf.googlecode.com/hg/proto"
@@ -186,7 +187,6 @@
t.Fail()
-
// t.Errorf("%s: \ngood: %s\nbad: %x", msg, s, b.Bytes())
// Print the output in a partially-decoded format; can
// be helpful when updating the test. It produces the output
@@ -1017,6 +1017,48 @@
}
}
+// A type that implements the Marshaler interface, but is not nillable.
+type nonNillableInt uint64
+
+func (nni nonNillableInt) Marshal() ([]byte, os.Error) {
+ return EncodeVarint(uint64(nni)), nil
+}
+
+type NNIMessage struct {
+ nni nonNillableInt
+}
+
+// A type that implements the Marshaler interface and is nillable.
+type nillableMessage struct {
+ x uint64
+}
+
+func (nm *nillableMessage) Marshal() ([]byte, os.Error) {
+ return EncodeVarint(nm.x), nil
+}
+
+type NMMessage struct {
+ nm *nillableMessage
+}
+
+// Verify a type that uses the Marshaler interface, but has a nil pointer.
+func TestNilMarshaler(t *testing.T) {
+ // Try a struct with a Marshaler field that is nil.
+ // It should be directly marshable.
+ nmm := new(NMMessage)
+ if _, err := Marshal(nmm); err != nil {
+ t.Error("unexpected error marshaling nmm: ", err)
+ }
+
+ // Try a struct with a Marshaler field that is not nillable.
+ nnim := new(NNIMessage)
+ nnim.nni = 7
+ var _ Marshaler = nnim.nni // verify it is truly a Marshaler
+ if _, err := Marshal(nnim); err != nil {
+ t.Error("unexpected error marshaling nnim: ", err)
+ }
+}
+
func BenchmarkMarshal(b *testing.B) {
b.StopTimer()