-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Improve GetTimestamp parsing #35402
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
Improve GetTimestamp parsing #35402
Conversation
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.
LGTM
minor nits on tests
@@ -49,8 +49,7 @@ func TestGetTimestamp(t *testing.T) { | |||
{"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false}, | |||
{"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false}, | |||
|
|||
// String fallback | |||
{"invalid", "invalid", false}, | |||
{"invalid", "", true}, |
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.
expectedErr
should probably the a string with the expected error instead of just a boolean, but this is existing so doesn't need to be fixed in this PR.
client/container_logs_test.go
Outdated
if err != nil { | ||
t.Fatal(err) | ||
if logCase.expectedError != "" { | ||
require.EqualError(t, err, logCase.expectedError) |
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.
Should probably be assert.EqualError()
in this case so it runs the other cases even if one case fails.
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.
yes, I can update that; I was trying to keep the existing behaviour, but agree updating would make sense.
Looks like I missed updating one test;
|
87214c4
to
f7edecd
Compare
}, | ||
}, | ||
{ | ||
options: types.ContainerLogsOptions{ |
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.
Synced the client/service_logs_...
files with client/container_logs_..
ones.
Looking at this type, I wonder if we should create a types.ServiceLogsOptions
, because service-logs and container-logs are now different in functionality (can possibly addressed, but there's no guarantee that they always have feature parity)
@dnephin wdyt?
What are the point of these functions? Reading the doc strings doesn't seem to indicate much. |
I mean, I can't believe the time package doesn't handle all the cases we need. |
I can do some digging to see what values dont work for the time package, otherwise just wrap those errors directly. Which reminds me; I have to check the API status code that's returned for invalid values |
f7edecd
to
40a7bf4
Compare
d191e77
to
5ddcbec
Compare
I looked if Go's I did make one change, and instead of parsing the diff --git a/api/types/time/timestamp.go b/api/types/time/timestamp.go
index f96b562a7..f97f04e12 100644
--- a/api/types/time/timestamp.go
+++ b/api/types/time/timestamp.go
@@ -86,8 +86,8 @@ func GetTimestamp(value string, reference time.Time) (string, error) {
if strings.Contains(value, "-") {
return "", err // was probably an RFC3339 like timestamp but the parser failed with an error
}
- if _, err := strconv.ParseFloat(value, 64); err != nil {
- return "", fmt.Errorf("failed to parse value as time or duration: %s", value)
+ if _, _, err := parseTimestamp(value); err != nil {
+ return "", fmt.Errorf("failed to parse value as time or duration: %q", value)
}
return value, nil // unix timestamp in and out case (meaning: the value passed at the command line is already in the right format for passing to the server)
}
@@ -107,6 +107,10 @@ func ParseTimestamps(value string, def int64) (int64, int64, error) {
if value == "" {
return def, 0, nil
}
+ return parseTimestamp(value)
+}
+
+func parseTimestamp(value string) (int64, int64, error) {
sa := strings.SplitN(value, ".", 2)
s, err := strconv.ParseInt(sa[0], 10, 64)
if err != nil {
diff --git a/api/types/time/timestamp_test.go b/api/types/time/timestamp_test.go
index ca49eb9cb..10241ec40 100644
--- a/api/types/time/timestamp_test.go
+++ b/api/types/time/timestamp_test.go
@@ -50,6 +50,7 @@ func TestGetTimestamp(t *testing.T) {
{"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false},
{"invalid", "", true},
+ {"", "", true},
}
for _, c := range cases { |
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.
LGTM 🐸
client/service_logs.go
Outdated
@@ -26,7 +27,7 @@ func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options ty | |||
if options.Since != "" { | |||
ts, err := timetypes.GetTimestamp(options.Since, time.Now()) | |||
if err != nil { | |||
return nil, err | |||
return nil, errors.Wrap(err, "invalid value for \"since\"") |
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.
nit:
`invalid value for "since"`
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.
Updated 👍
oh, looks like there's a test I need to update as well; I'll update later |
318ac34
to
eecddb8
Compare
humph
restarting Janky |
eecddb8
to
c8ea1b5
Compare
`GetTimestamp()` "assumed" values it could not parse to be a valid unix timestamp, and would use invalid values ("hello world") as-is (even testing that it did so). This patch validates unix timestamp to be a valid numeric value, and makes other values invalid. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
c8ea1b5
to
48cfe3f
Compare
Rebased (now using gotestyourself); @cpuguy83 @vdemeester this ok to merge? |
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.
LGTM
Codecov Report
@@ Coverage Diff @@
## master #35402 +/- ##
=========================================
Coverage ? 35.25%
=========================================
Files ? 615
Lines ? 46223
Branches ? 0
=========================================
Hits ? 16297
Misses ? 27786
Partials ? 2140 |
GetTimestamp()
"assumed" values it could not parse to be a valid unix timestamp, and would use invalid values ("hello world") as-is (even testing that it did so).This patch validates unix timestamp to be a valid numeric value, and makes other values invalid.
I noticed this while reviewing #32914 (comment)
- Description for the changelog