Skip to content

Commit

Permalink
Behave gracefully when text formatting a map field with a nil value.
Browse files Browse the repository at this point in the history
Fixes #33.
  • Loading branch information
dsymonds committed May 26, 2015
1 parent c8ba115 commit 34a5f24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
27 changes: 15 additions & 12 deletions proto/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions proto/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ func TestProto3Text(t *testing.T) {
&pb.MessageWithMap{NameMapping: map[int32]string{1234: "Feist"}},
`name_mapping:<key:1234 value:"Feist" >`,
},
// map with nil value; not well-defined, but we shouldn't crash
{
&pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{7: nil}},
`msg_mapping:<key:7 >`,
},
}
for _, test := range tests {
got := strings.TrimSpace(test.m.String())
Expand Down

0 comments on commit 34a5f24

Please sign in to comment.