-
Notifications
You must be signed in to change notification settings - Fork 24
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
controller: replaced parse_duration package with custom parser #607
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.
Nice. Please add unit tests.
Since the new function is only called with values that are previously checked with regex, running the function with a value that doesn't conform to the regex is undefined behavior. Should I "define" this behavior and add some unit tests for those cases, or only add tests for values that conform to the regex? |
It's not super important in this case, so not worth losing a bunch of time on, but... It probably wouldn't hurt to use |
controller/src/constants.rs
Outdated
@@ -15,3 +17,20 @@ pub(crate) fn requeue_slow() -> Action { | |||
pub(crate) fn no_requeue() -> Action { | |||
Action::await_change() | |||
} | |||
|
|||
/// Parse a duration string into a Duration object. | |||
pub(crate) fn parse_duration(input: &str) -> Result<Duration, ParseIntError> { |
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 should return an error type from this crate.
8e82cc9
to
ff9e37c
Compare
^ Added unit tests and removed |
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.
I don't quite follow the parsing logic, but the tests convince me that it works.
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.
Looks like unit tests cover each case, including invalid cases. Might be nice to have the error message contain something more detailed as to why the parsing failed, but hopefully that should be pretty obvious to future us.
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.
Not sure if the parsing function belongs in the constants
module. Maybe a new utils.rs
is more appropriate.
Issue number:
Closes #596
Description of changes:
The
parse_duration
package is no longer being maintained. Since we used the package for a very specific use-case (parsing thetimeout
string, which is already validated by regex), it was easier to remove the package and implement a short function to replace it. This function takes a string that conforms to"^((([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?|\d+)$"
and converts it into aDuration
object.Testing done:
Created
Test
object with various valid values fortimeout
("123", "1d2h3m4s", "500s", "3h100s", "20d", etc.) which all resulted in the correctDuration
. When a value was used fortimeout
that didn't conform to the regex, the object could not be created (as is expected).Terms of contribution:
By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.