-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
runtime: add support for time types in query parameters #693
Merged
tmc
merged 1 commit into
grpc-ecosystem:master
from
johanbrandhorst:add-duration-query-parameter
Jul 5, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/golang/protobuf/ptypes" | ||
"github.com/golang/protobuf/ptypes/duration" | ||
"github.com/golang/protobuf/ptypes/timestamp" | ||
"github.com/golang/protobuf/ptypes/wrappers" | ||
"github.com/grpc-ecosystem/grpc-gateway/runtime" | ||
|
@@ -25,6 +26,10 @@ func TestPopulateParameters(t *testing.T) { | |
t.Fatalf("Couldn't setup timestamp in Protobuf format: %v", err) | ||
} | ||
|
||
durationT := 13 * time.Hour | ||
durationStr := durationT.String() | ||
durationPb := ptypes.DurationProto(durationT) | ||
|
||
fieldmaskStr := "float_value,double_value" | ||
fieldmaskPb := &field_mask.FieldMask{Paths: []string{"float_value", "double_value"}} | ||
|
||
|
@@ -49,6 +54,7 @@ func TestPopulateParameters(t *testing.T) { | |
"enum_value": {"1"}, | ||
"repeated_enum": {"1", "2", "0"}, | ||
"timestamp_value": {timeStr}, | ||
"duration_value": {durationStr}, | ||
"fieldmask_value": {fieldmaskStr}, | ||
"wrapper_float_value": {"1.5"}, | ||
"wrapper_double_value": {"2.5"}, | ||
|
@@ -94,6 +100,7 @@ func TestPopulateParameters(t *testing.T) { | |
EnumValue: EnumValue_Y, | ||
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X}, | ||
TimestampValue: timePb, | ||
DurationValue: durationPb, | ||
FieldMaskValue: fieldmaskPb, | ||
WrapperFloatValue: &wrappers.FloatValue{Value: 1.5}, | ||
WrapperDoubleValue: &wrappers.DoubleValue{Value: 2.5}, | ||
|
@@ -142,6 +149,7 @@ func TestPopulateParameters(t *testing.T) { | |
"enumValue": {"1"}, | ||
"repeatedEnum": {"1", "2", "0"}, | ||
"timestampValue": {timeStr}, | ||
"durationValue": {durationStr}, | ||
"fieldmaskValue": {fieldmaskStr}, | ||
"wrapperFloatValue": {"1.5"}, | ||
"wrapperDoubleValue": {"2.5"}, | ||
|
@@ -168,6 +176,7 @@ func TestPopulateParameters(t *testing.T) { | |
EnumValue: EnumValue_Y, | ||
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X}, | ||
TimestampValue: timePb, | ||
DurationValue: durationPb, | ||
FieldMaskValue: fieldmaskPb, | ||
WrapperFloatValue: &wrappers.FloatValue{Value: 1.5}, | ||
WrapperDoubleValue: &wrappers.DoubleValue{Value: 2.5}, | ||
|
@@ -338,6 +347,51 @@ func TestPopulateParameters(t *testing.T) { | |
} | ||
} | ||
|
||
func TestPopulateParametersWithNativeTypes(t *testing.T) { | ||
timeT := time.Date(2016, time.December, 15, 12, 23, 32, 49, time.UTC) | ||
timeStr := timeT.Format(time.RFC3339Nano) | ||
|
||
durationT := 13 * time.Hour | ||
durationStr := durationT.String() | ||
|
||
for _, spec := range []struct { | ||
values url.Values | ||
want *nativeProto3Message | ||
}{ | ||
{ | ||
values: url.Values{ | ||
"native_timestamp_value": {timeStr}, | ||
"native_duration_value": {durationStr}, | ||
}, | ||
want: &nativeProto3Message{ | ||
NativeTimeValue: &timeT, | ||
NativeDurationValue: &durationT, | ||
}, | ||
}, | ||
{ | ||
values: url.Values{ | ||
"nativeTimestampValue": {timeStr}, | ||
"nativeDurationValue": {durationStr}, | ||
}, | ||
want: &nativeProto3Message{ | ||
NativeTimeValue: &timeT, | ||
NativeDurationValue: &durationT, | ||
}, | ||
}, | ||
} { | ||
msg := new(nativeProto3Message) | ||
err := runtime.PopulateQueryParameters(msg, spec.values, utilities.NewDoubleArray(nil)) | ||
|
||
if err != nil { | ||
t.Errorf("runtime.PopulateQueryParameters(msg, %v, utilities.NewDoubleArray(nil)) failed with %v; want success", spec.values, err) | ||
continue | ||
} | ||
if got, want := msg, spec.want; !proto.Equal(got, want) { | ||
t.Errorf("runtime.PopulateQueryParameters(msg, %v, utilities.NewDoubleArray(nil)) = %v; want %v", spec.values, got, want) | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be a separate test because |
||
|
||
func TestPopulateParametersWithFilters(t *testing.T) { | ||
for _, spec := range []struct { | ||
values url.Values | ||
|
@@ -537,6 +591,7 @@ type proto3Message struct { | |
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=runtime_test_api.EnumValue" json:"enum_value,omitempty"` | ||
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeatedEnum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"` | ||
TimestampValue *timestamp.Timestamp `protobuf:"bytes,16,opt,name=timestamp_value,json=timestampValue" json:"timestamp_value,omitempty"` | ||
DurationValue *duration.Duration `protobuf:"bytes,42,opt,name=duration_value,json=durationValue" json:"duration_value,omitempty"` | ||
FieldMaskValue *field_mask.FieldMask `protobuf:"bytes,27,opt,name=fieldmask_value,json=fieldmaskValue" json:"fieldmask_value,omitempty"` | ||
OneofValue proto3Message_OneofValue `protobuf_oneof:"oneof_value"` | ||
WrapperDoubleValue *wrappers.DoubleValue `protobuf:"bytes,17,opt,name=wrapper_double_value,json=wrapperDoubleValue" json:"wrapper_double_value,omitempty"` | ||
|
@@ -680,6 +735,15 @@ func _proto3Message_OneofSizer(msg proto.Message) (n int) { | |
return n | ||
} | ||
|
||
type nativeProto3Message struct { | ||
NativeTimeValue *time.Time `protobuf:"bytes,1,opt,name=native_timestamp_value,json=nativeTimestampValue" json:"native_timestamp_value,omitempty"` | ||
NativeDurationValue *time.Duration `protobuf:"bytes,2,opt,name=native_duration_value,json=nativeDurationValue" json:"native_duration_value,omitempty"` | ||
} | ||
|
||
func (m *nativeProto3Message) Reset() { *m = nativeProto3Message{} } | ||
func (m *nativeProto3Message) String() string { return proto.CompactTextString(m) } | ||
func (*nativeProto3Message) ProtoMessage() {} | ||
|
||
type proto2Message struct { | ||
Nested *proto3Message `protobuf:"bytes,1,opt,name=nested,json=nested" json:"nested,omitempty"` | ||
FloatValue *float32 `protobuf:"fixed32,2,opt,name=float_value,json=floatValue" json:"float_value,omitempty"` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part may be controversial, but I think considering this does not add any explicit dependencies on any gogoproto packages, I argue that this should be acceptable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate? What could be considered controversial?