-
Notifications
You must be signed in to change notification settings - Fork 69
feat: Support day unit with edgex-dto-duration validation tag #1014
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
Conversation
Resolves edgexfoundry#1013. Support day unit in edgex-dto-duration validation tag. Signed-off-by: Lindsey Cheng <[email protected]>
| func parseDurationWithDay(durationStr string) (bool, time.Duration) { | ||
| var totalDuration time.Duration | ||
|
|
||
| // Regex to capture decimal days (e.g., "2.5d" or "3d") | ||
| re := regexp.MustCompile(`([\d.]+)d`) | ||
| matches := re.FindStringSubmatch(durationStr) | ||
|
|
||
| // Checks if the duration string contains a valid decimal with 'd'(day) unit | ||
| if len(matches) == 2 { | ||
| day, err := strconv.ParseFloat(matches[1], 64) | ||
| if err != nil { | ||
| return false, 0 | ||
| } | ||
| // Converts days to hours | ||
| totalDuration = time.Duration(day * float64(24*time.Hour)) | ||
|
|
||
| // Remove the matched "Xd" (e.g., "2.5d") from the duration string | ||
| // so we're left with only the remaining duration (e.g., "5h") | ||
| durationStr = re.ReplaceAllString(durationStr, "") | ||
| durationStr = strings.TrimSpace(durationStr) | ||
|
|
||
| // The duration string contains no other time units except for "d" | ||
| if durationStr == "" { | ||
| return true, totalDuration | ||
| } | ||
| } | ||
|
|
||
| // Parse the remaining duration string without day unit | ||
| remainingDuration, err := time.ParseDuration(durationStr) | ||
| if err != nil { | ||
| return false, totalDuration | ||
| } | ||
|
|
||
| totalDuration += remainingDuration | ||
| return true, totalDuration | ||
| } |
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.
The time.ParseDuration is capable of summing up repeated time units inside the duration string. For example, time.ParseDuration("1h30m10m1h") will return a duration 2h40m0s. To be consistent with such behavior, the implementation of parseDurationWithDay should be capable of doing same thing to sum up repeated d time unit. Below is a revised version of parseDurationWithDay to achieve this:
func parseDurationWithDay(durationStr string) (bool, time.Duration) {
var totalDuration time.Duration
// Regex to find all day fragments like "2.2d", "1.5d", etc.
re := regexp.MustCompile(`([\d.]+)d`)
matches := re.FindAllStringSubmatch(durationStr, -1)
// Sum up all matched day durations
for _, match := range matches {
if len(match) != 2 {
continue
}
dayVal, err := strconv.ParseFloat(match[1], 64)
if err != nil {
return false, 0
}
totalDuration += time.Duration(dayVal * float64(24*time.Hour))
}
// Remove all day fragments from the original string to get the rest
durationStr = re.ReplaceAllString(durationStr, "")
durationStr = strings.TrimSpace(durationStr)
// Parse the remaining standard duration if any (e.g., "5h30m")
if durationStr != "" {
remainingDuration, err := time.ParseDuration(durationStr)
if err != nil {
return false, totalDuration
}
totalDuration += remainingDuration
}
return true, totalDuration
}
We should also unit tests to cover such edge case, e.g. 2.2d1.1d20m3h1h10m is parsed to 83h42m0s.
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.
Thanks for pointing out the repeated day units case which I didn't think of.
Modified and unit test added.
Handle repeated days in duration string. Signed-off-by: Lindsey Cheng <[email protected]>
judehung
left a comment
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
Resolves #1013. Support day unit in edgex-dto-duration validation tag.
If your build fails due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/go-mod-core-contracts/blob/main/.github/Contributing.md
PR Checklist
Please check if your PR fulfills the following requirements:
BREAKING CHANGE:describing the break)Testing Instructions
New Dependency Instructions (If applicable)