Skip to content

Update jsonpb to handle Map, Slice, Ptr nil values for EmitDefaults #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion jsonpb/jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU

// IsNil will panic on most value kinds.
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
case reflect.Chan, reflect.Func, reflect.Interface:
if value.IsNil() {
continue
}
Expand Down Expand Up @@ -235,6 +235,10 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU
if value.Len() == 0 {
continue
}
case reflect.Map, reflect.Ptr, reflect.Slice:
if value.IsNil() {
continue
}
}
}

Expand Down Expand Up @@ -402,6 +406,12 @@ func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v refle
var err error
v = reflect.Indirect(v)

// Handle nil pointer
if v.Kind() == reflect.Invalid {
out.write("null")
return out.err
}

// Handle repeated elements.
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
out.write("[")
Expand Down
3 changes: 3 additions & 0 deletions jsonpb/jsonpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ var marshalingTests = []struct {
`{"rFunny":[1,2]}`},
{"empty value", marshaler, &pb.Simple3{}, `{}`},
{"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
{"empty repeated emitted", Marshaler{EmitDefaults: true}, &pb.SimpleSlice3{}, `{"slices":[]}`},
{"empty map emitted", Marshaler{EmitDefaults: true}, &pb.SimpleMap3{}, `{"stringy":{}}`},
{"nested struct null", Marshaler{EmitDefaults: true}, &pb.SimpleNull3{}, `{"simple":null}`},
{"map<int64, int32>", marshaler, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
{"map<int64, int32>", marshalerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
{"map<string, string>", marshaler,
Expand Down
119 changes: 89 additions & 30 deletions jsonpb/jsonpb_test_proto/more_test_objects.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions jsonpb/jsonpb_test_proto/more_test_objects.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ message Simple3 {
double dub = 1;
}

message SimpleSlice3 {
repeated string slices = 1;
}

message SimpleMap3 {
map<string,string> stringy = 1;
}

message SimpleNull3 {
Simple3 simple = 1;
}

enum Numeral {
UNKNOWN = 0;
ARABIC = 1;
Expand Down