Skip to content

Commit f5f78eb

Browse files
committed
Support more types in JSONPb marshaler
1 parent ee8f8cb commit f5f78eb

File tree

3 files changed

+206
-200
lines changed

3 files changed

+206
-200
lines changed

examples/integration_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ func testABECreateBody(t *testing.T) {
238238
Amount: 20,
239239
},
240240
},
241+
RepeatedStringValue: []string{"a", "b", "c"},
242+
OneofValue: &gw.ABitOfEverything_OneofString{
243+
OneofString: "x",
244+
},
245+
MapValue: map[string]gw.NumericEnum{
246+
"a": gw.NumericEnum_ONE,
247+
"b": gw.NumericEnum_ZERO,
248+
},
249+
MappedStringValue: map[string]string{
250+
"a": "x",
251+
"b": "y",
252+
},
253+
MappedNestedValue: map[string]*gw.ABitOfEverything_Nested{
254+
"a": {Name: "x", Amount: 1},
255+
"b": {Name: "y", Amount: 2},
256+
},
241257
}
242258
url := "http://localhost:8080/v1/example/a_bit_of_everything"
243259
var m jsonpb.Marshaler

runtime/marshal_jsonpb.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func decodeNonProtoField(d *json.Decoder, v interface{}) error {
144144
if rv.IsNil() {
145145
rv.Set(reflect.New(rv.Type().Elem()))
146146
}
147+
if rv.Type().ConvertibleTo(typeProtoMessage) {
148+
return jsonpb.UnmarshalNext(d, rv.Interface().(proto.Message))
149+
}
147150
rv = rv.Elem()
148151
}
149152
if rv.Kind() == reflect.Map {
@@ -196,3 +199,5 @@ type protoEnum interface {
196199
fmt.Stringer
197200
EnumDescriptor() ([]byte, []int)
198201
}
202+
203+
var typeProtoMessage = reflect.TypeOf((*proto.Message)(nil)).Elem()

0 commit comments

Comments
 (0)