diff --git a/proto/text.go b/proto/text.go index 720eac4705..4ad159058b 100644 --- a/proto/text.go +++ b/proto/text.go @@ -283,20 +283,23 @@ func writeStruct(w *textWriter, sv reflect.Value) error { if err := w.WriteByte('\n'); err != nil { return err } - // value - if _, err := w.WriteString("value:"); err != nil { - return err - } - if !w.compact { - if err := w.WriteByte(' '); err != nil { + // nil values aren't legal, but we can avoid panicking because of them. + if val.Kind() != reflect.Ptr || !val.IsNil() { + // value + if _, err := w.WriteString("value:"); err != nil { + return err + } + if !w.compact { + if err := w.WriteByte(' '); err != nil { + return err + } + } + if err := writeAny(w, val, props.mvalprop); err != nil { + return err + } + if err := w.WriteByte('\n'); err != nil { return err } - } - if err := writeAny(w, val, props.mvalprop); err != nil { - return err - } - if err := w.WriteByte('\n'); err != nil { - return err } // close struct w.unindent() diff --git a/proto/text_test.go b/proto/text_test.go index 03e9a98ce9..39861d1ca8 100644 --- a/proto/text_test.go +++ b/proto/text_test.go @@ -426,6 +426,11 @@ func TestProto3Text(t *testing.T) { &pb.MessageWithMap{NameMapping: map[int32]string{1234: "Feist"}}, `name_mapping:`, }, + // map with nil value; not well-defined, but we shouldn't crash + { + &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{7: nil}}, + `msg_mapping:`, + }, } for _, test := range tests { got := strings.TrimSpace(test.m.String())