Skip to content

Commit 1fd8ba6

Browse files
Tianyu Gengachew22
authored andcommitted
Fix logic handling primitive wrapper in URL params
In ESP, wrapped primitives in URL can also be parsed from seralized string format of the primitive directly without ".value" field reference. This PR changes URL parsing logic to conform with this behavior.
1 parent b2423da commit 1fd8ba6

File tree

2 files changed

+143
-63
lines changed

2 files changed

+143
-63
lines changed

runtime/query.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,45 @@ func populateField(f reflect.Value, value string, props *proto.Properties) error
164164
f.Field(0).SetInt(int64(t.Unix()))
165165
f.Field(1).SetInt(int64(t.Nanosecond()))
166166
return nil
167+
case "DoubleValue":
168+
fallthrough
169+
case "FloatValue":
170+
float64Val, err := strconv.ParseFloat(value, 64)
171+
if err != nil {
172+
return fmt.Errorf("bad DoubleValue: %s", value)
173+
}
174+
f.Field(0).SetFloat(float64Val)
175+
return nil
176+
case "Int64Value":
177+
fallthrough
178+
case "Int32Value":
179+
int64Val, err := strconv.ParseInt(value, 10, 64)
180+
if err != nil {
181+
return fmt.Errorf("bad DoubleValue: %s", value)
182+
}
183+
f.Field(0).SetInt(int64Val)
184+
return nil
185+
case "UInt64Value":
186+
fallthrough
187+
case "UInt32Value":
188+
uint64Val, err := strconv.ParseUint(value, 10, 64)
189+
if err != nil {
190+
return fmt.Errorf("bad DoubleValue: %s", value)
191+
}
192+
f.Field(0).SetUint(uint64Val)
193+
return nil
194+
case "BoolValue":
195+
if value == "true" {
196+
f.Field(0).SetBool(true)
197+
} else if value == "false" {
198+
f.Field(0).SetBool(false)
199+
} else {
200+
return fmt.Errorf("bad BoolValue: %s", value)
201+
}
202+
return nil
203+
case "StringValue":
204+
f.Field(0).SetString(value)
205+
return nil
167206
}
168207
}
169208

runtime/query_test.go

Lines changed: 104 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/golang/protobuf/proto"
1313
"github.com/golang/protobuf/ptypes"
1414
"github.com/golang/protobuf/ptypes/timestamp"
15+
"github.com/golang/protobuf/ptypes/wrappers"
1516
"github.com/grpc-ecosystem/grpc-gateway/runtime"
1617
"github.com/grpc-ecosystem/grpc-gateway/utilities"
1718
)
@@ -32,64 +33,96 @@ func TestPopulateParameters(t *testing.T) {
3233
}{
3334
{
3435
values: url.Values{
35-
"float_value": {"1.5"},
36-
"double_value": {"2.5"},
37-
"int64_value": {"-1"},
38-
"int32_value": {"-2"},
39-
"uint64_value": {"3"},
40-
"uint32_value": {"4"},
41-
"bool_value": {"true"},
42-
"string_value": {"str"},
43-
"repeated_value": {"a", "b", "c"},
44-
"enum_value": {"1"},
45-
"repeated_enum": {"1", "2", "0"},
46-
"timestamp_value": {timeStr},
36+
"float_value": {"1.5"},
37+
"double_value": {"2.5"},
38+
"int64_value": {"-1"},
39+
"int32_value": {"-2"},
40+
"uint64_value": {"3"},
41+
"uint32_value": {"4"},
42+
"bool_value": {"true"},
43+
"string_value": {"str"},
44+
"repeated_value": {"a", "b", "c"},
45+
"enum_value": {"1"},
46+
"repeated_enum": {"1", "2", "0"},
47+
"timestamp_value": {timeStr},
48+
"wrapper_float_value": {"1.5"},
49+
"wrapper_double_value": {"2.5"},
50+
"wrapper_int64_value": {"-1"},
51+
"wrapper_int32_value": {"-2"},
52+
"wrapper_u_int64_value": {"3"},
53+
"wrapper_u_int32_value": {"4"},
54+
"wrapper_bool_value": {"true"},
55+
"wrapper_string_value": {"str"},
4756
},
4857
filter: utilities.NewDoubleArray(nil),
4958
want: &proto3Message{
50-
FloatValue: 1.5,
51-
DoubleValue: 2.5,
52-
Int64Value: -1,
53-
Int32Value: -2,
54-
Uint64Value: 3,
55-
Uint32Value: 4,
56-
BoolValue: true,
57-
StringValue: "str",
58-
RepeatedValue: []string{"a", "b", "c"},
59-
EnumValue: EnumValue_Y,
60-
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
61-
TimestampValue: timePb,
59+
FloatValue: 1.5,
60+
DoubleValue: 2.5,
61+
Int64Value: -1,
62+
Int32Value: -2,
63+
Uint64Value: 3,
64+
Uint32Value: 4,
65+
BoolValue: true,
66+
StringValue: "str",
67+
RepeatedValue: []string{"a", "b", "c"},
68+
EnumValue: EnumValue_Y,
69+
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
70+
TimestampValue: timePb,
71+
WrapperFloatValue: &wrappers.FloatValue{1.5},
72+
WrapperDoubleValue: &wrappers.DoubleValue{2.5},
73+
WrapperInt64Value: &wrappers.Int64Value{-1},
74+
WrapperInt32Value: &wrappers.Int32Value{-2},
75+
WrapperUInt64Value: &wrappers.UInt64Value{3},
76+
WrapperUInt32Value: &wrappers.UInt32Value{4},
77+
WrapperBoolValue: &wrappers.BoolValue{true},
78+
WrapperStringValue: &wrappers.StringValue{"str"},
6279
},
6380
},
6481
{
6582
values: url.Values{
66-
"floatValue": {"1.5"},
67-
"doubleValue": {"2.5"},
68-
"int64Value": {"-1"},
69-
"int32Value": {"-2"},
70-
"uint64Value": {"3"},
71-
"uint32Value": {"4"},
72-
"boolValue": {"true"},
73-
"stringValue": {"str"},
74-
"repeatedValue": {"a", "b", "c"},
75-
"enumValue": {"1"},
76-
"repeatedEnum": {"1", "2", "0"},
77-
"timestampValue": {timeStr},
83+
"floatValue": {"1.5"},
84+
"doubleValue": {"2.5"},
85+
"int64Value": {"-1"},
86+
"int32Value": {"-2"},
87+
"uint64Value": {"3"},
88+
"uint32Value": {"4"},
89+
"boolValue": {"true"},
90+
"stringValue": {"str"},
91+
"repeatedValue": {"a", "b", "c"},
92+
"enumValue": {"1"},
93+
"repeatedEnum": {"1", "2", "0"},
94+
"timestampValue": {timeStr},
95+
"wrapperFloatValue": {"1.5"},
96+
"wrapperDoubleValue": {"2.5"},
97+
"wrapperInt64Value": {"-1"},
98+
"wrapperInt32Value": {"-2"},
99+
"wrapperUInt64Value": {"3"},
100+
"wrapperUInt32Value": {"4"},
101+
"wrapperBoolValue": {"true"},
102+
"wrapperStringValue": {"str"},
78103
},
79104
filter: utilities.NewDoubleArray(nil),
80105
want: &proto3Message{
81-
FloatValue: 1.5,
82-
DoubleValue: 2.5,
83-
Int64Value: -1,
84-
Int32Value: -2,
85-
Uint64Value: 3,
86-
Uint32Value: 4,
87-
BoolValue: true,
88-
StringValue: "str",
89-
RepeatedValue: []string{"a", "b", "c"},
90-
EnumValue: EnumValue_Y,
91-
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
92-
TimestampValue: timePb,
106+
FloatValue: 1.5,
107+
DoubleValue: 2.5,
108+
Int64Value: -1,
109+
Int32Value: -2,
110+
Uint64Value: 3,
111+
Uint32Value: 4,
112+
BoolValue: true,
113+
StringValue: "str",
114+
RepeatedValue: []string{"a", "b", "c"},
115+
EnumValue: EnumValue_Y,
116+
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
117+
TimestampValue: timePb,
118+
WrapperFloatValue: &wrappers.FloatValue{1.5},
119+
WrapperDoubleValue: &wrappers.DoubleValue{2.5},
120+
WrapperInt64Value: &wrappers.Int64Value{-1},
121+
WrapperInt32Value: &wrappers.Int32Value{-2},
122+
WrapperUInt64Value: &wrappers.UInt64Value{3},
123+
WrapperUInt32Value: &wrappers.UInt32Value{4},
124+
WrapperBoolValue: &wrappers.BoolValue{true},
125+
WrapperStringValue: &wrappers.StringValue{"str"},
93126
},
94127
},
95128
{
@@ -428,21 +461,29 @@ func TestPopulateQueryParametersWithInvalidNestedParameters(t *testing.T) {
428461
}
429462

430463
type proto3Message struct {
431-
Nested *proto2Message `protobuf:"bytes,1,opt,name=nested,json=nested" json:"nested,omitempty"`
432-
NestedNonNull proto2Message `protobuf:"bytes,15,opt,name=nested_non_null,json=nestedNonNull" json:"nested_non_null,omitempty"`
433-
FloatValue float32 `protobuf:"fixed32,2,opt,name=float_value,json=floatValue" json:"float_value,omitempty"`
434-
DoubleValue float64 `protobuf:"fixed64,3,opt,name=double_value,json=doubleValue" json:"double_value,omitempty"`
435-
Int64Value int64 `protobuf:"varint,4,opt,name=int64_value,json=int64Value" json:"int64_value,omitempty"`
436-
Int32Value int32 `protobuf:"varint,5,opt,name=int32_value,json=int32Value" json:"int32_value,omitempty"`
437-
Uint64Value uint64 `protobuf:"varint,6,opt,name=uint64_value,json=uint64Value" json:"uint64_value,omitempty"`
438-
Uint32Value uint32 `protobuf:"varint,7,opt,name=uint32_value,json=uint32Value" json:"uint32_value,omitempty"`
439-
BoolValue bool `protobuf:"varint,8,opt,name=bool_value,json=boolValue" json:"bool_value,omitempty"`
440-
StringValue string `protobuf:"bytes,9,opt,name=string_value,json=stringValue" json:"string_value,omitempty"`
441-
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value,json=repeatedValue" json:"repeated_value,omitempty"`
442-
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=runtime_test_api.EnumValue" json:"enum_value,omitempty"`
443-
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeatedEnum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
444-
TimestampValue *timestamp.Timestamp `protobuf:"bytes,16,opt,name=timestamp_value,json=timestampValue" json:"timestamp_value,omitempty"`
445-
OneofValue proto3Message_OneofValue `protobuf_oneof:"oneof_value"`
464+
Nested *proto2Message `protobuf:"bytes,1,opt,name=nested,json=nested" json:"nested,omitempty"`
465+
NestedNonNull proto2Message `protobuf:"bytes,15,opt,name=nested_non_null,json=nestedNonNull" json:"nested_non_null,omitempty"`
466+
FloatValue float32 `protobuf:"fixed32,2,opt,name=float_value,json=floatValue" json:"float_value,omitempty"`
467+
DoubleValue float64 `protobuf:"fixed64,3,opt,name=double_value,json=doubleValue" json:"double_value,omitempty"`
468+
Int64Value int64 `protobuf:"varint,4,opt,name=int64_value,json=int64Value" json:"int64_value,omitempty"`
469+
Int32Value int32 `protobuf:"varint,5,opt,name=int32_value,json=int32Value" json:"int32_value,omitempty"`
470+
Uint64Value uint64 `protobuf:"varint,6,opt,name=uint64_value,json=uint64Value" json:"uint64_value,omitempty"`
471+
Uint32Value uint32 `protobuf:"varint,7,opt,name=uint32_value,json=uint32Value" json:"uint32_value,omitempty"`
472+
BoolValue bool `protobuf:"varint,8,opt,name=bool_value,json=boolValue" json:"bool_value,omitempty"`
473+
StringValue string `protobuf:"bytes,9,opt,name=string_value,json=stringValue" json:"string_value,omitempty"`
474+
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value,json=repeatedValue" json:"repeated_value,omitempty"`
475+
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=runtime_test_api.EnumValue" json:"enum_value,omitempty"`
476+
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeatedEnum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
477+
TimestampValue *timestamp.Timestamp `protobuf:"bytes,16,opt,name=timestamp_value,json=timestampValue" json:"timestamp_value,omitempty"`
478+
OneofValue proto3Message_OneofValue `protobuf_oneof:"oneof_value"`
479+
WrapperDoubleValue *wrappers.DoubleValue `protobuf:"bytes,17,opt,name=wrapper_double_value,json=wrapperDoubleValue" json:"wrapper_double_value,omitempty"`
480+
WrapperFloatValue *wrappers.FloatValue `protobuf:"bytes,18,opt,name=wrapper_float_value,json=wrapperFloatValue" json:"wrapper_float_value,omitempty"`
481+
WrapperInt64Value *wrappers.Int64Value `protobuf:"bytes,19,opt,name=wrapper_int64_value,json=wrapperInt64Value" json:"wrapper_int64_value,omitempty"`
482+
WrapperInt32Value *wrappers.Int32Value `protobuf:"bytes,20,opt,name=wrapper_int32_value,json=wrapperInt32Value" json:"wrapper_int32_value,omitempty"`
483+
WrapperUInt64Value *wrappers.UInt64Value `protobuf:"bytes,21,opt,name=wrapper_u_int64_value,json=wrapperUInt64Value" json:"wrapper_u_int64_value,omitempty"`
484+
WrapperUInt32Value *wrappers.UInt32Value `protobuf:"bytes,22,opt,name=wrapper_u_int32_value,json=wrapperUInt32Value" json:"wrapper_u_int32_value,omitempty"`
485+
WrapperBoolValue *wrappers.BoolValue `protobuf:"bytes,23,opt,name=wrapper_bool_value,json=wrapperBoolValue" json:"wrapper_bool_value,omitempty"`
486+
WrapperStringValue *wrappers.StringValue `protobuf:"bytes,24,opt,name=wrapper_string_value,json=wrapperStringValue" json:"wrapper_string_value,omitempty"`
446487
}
447488

448489
func (m *proto3Message) Reset() { *m = proto3Message{} }

0 commit comments

Comments
 (0)