-
Notifications
You must be signed in to change notification settings - Fork 728
GEP2257 Duration parsing #3271
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
Merged
k8s-ci-robot
merged 4 commits into
kubernetes-sigs:main
from
xtineskim:go-durationutils
Aug 24, 2024
Merged
GEP2257 Duration parsing #3271
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62b9397
GEP2257 Duration parsing
xtineskim e6abec2
Address per comments, added maxDuration check
xtineskim b7ca574
Change FormatDuration to be consistent across language implementations
xtineskim f36389e
Remove unecessary regex pattern for formatting
xtineskim 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "regexp" | ||
| "time" | ||
| ) | ||
|
|
||
| var re = regexp.MustCompile(`^([0-9]{1,5}(h|m|s|ms)){1,4}$`) | ||
|
|
||
| func ParseDuration(s string) (*time.Duration, error) { | ||
| /* | ||
| parseDuration parses a GEP-2257 Duration format to a time object | ||
| Valid date units in time.Duration are "ns", "us" (or "µs"), "ms", "s", "m", "h" | ||
| Valid date units according to GEP-2257 are "h", "m", "s", "ms" | ||
|
|
||
| input: string | ||
|
|
||
| output: time.Duration | ||
|
|
||
| See https://gateway-api.sigs.k8s.io/geps/gep-2257/ for more details. | ||
| */ | ||
| if matched := re.MatchString(s); matched == false { | ||
| return nil, errors.New("Invalid duration format") | ||
| } | ||
| parsedTime, err := time.ParseDuration(s) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
xtineskim marked this conversation as resolved.
|
||
|
|
||
| return &parsedTime, nil | ||
|
|
||
| } | ||
|
|
||
| var reSplit = regexp.MustCompile(`[0-9]{1,5}(h|ms|s|m)`) | ||
|
|
||
| func FormatDuration(duration time.Duration) (string, error) { | ||
|
xtineskim marked this conversation as resolved.
|
||
| /* | ||
| formatDuration formats a time object to GEP-2257 Duration format to a GEP-2257 Duration Format | ||
| The time format from GEP-2257 must match the regex | ||
| "^([1-9]{1,5}(h|m|s|ms)){1,4}$" | ||
|
|
||
| A time.Duration allows for negative time, floating points, and allow for zero units | ||
| For example, -4h, 4.5h, and 4h0m0s are all valid in the golang time package | ||
|
|
||
| See https://gateway-api.sigs.k8s.io/geps/gep-2257/ for more details. | ||
|
|
||
| Input: time.Duration | ||
|
|
||
| Returns: string or error if duration cannot be expressed as a GEP-2257 Duration format. | ||
| */ | ||
| m, _ := time.ParseDuration("0s") | ||
| if duration == m { | ||
| return "0s", nil | ||
| } | ||
|
xtineskim marked this conversation as resolved.
Outdated
|
||
| // time.Duration allows for floating point ms, which is not allowed in GEP-2257 | ||
| durationMicroseconds := duration.Microseconds() | ||
|
|
||
| if durationMicroseconds%1000 != 0 { | ||
|
xtineskim marked this conversation as resolved.
|
||
| return "", errors.New("Cannot express sub-milliseconds precision in GEP-2257") | ||
| } | ||
|
|
||
| //Golang's time.Duration allows for floating point seconds instead of converting to ms | ||
| durationMilliseconds := duration.Milliseconds() | ||
|
xtineskim marked this conversation as resolved.
Outdated
|
||
|
|
||
| var ms int64 | ||
| if durationMilliseconds%1000 != 0 { | ||
| ms = durationMilliseconds % 1000 | ||
| durationMilliseconds -= ms | ||
| duration = time.Millisecond * time.Duration(durationMilliseconds) | ||
| } | ||
|
xtineskim marked this conversation as resolved.
Outdated
|
||
|
|
||
| durationString := duration.String() | ||
| if ms > 0 { | ||
| durationString += fmt.Sprintf("%dms", ms) | ||
| } | ||
|
|
||
| // check if a negative value | ||
| if duration < 0 { | ||
| return "", errors.New("Invalid duration format. Cannot have negative durations") | ||
| } | ||
|
xtineskim marked this conversation as resolved.
Outdated
|
||
|
|
||
| // trim the 0 values from the string (for example, 30m0s should result in 30m) | ||
| // going to have a regexp that finds the index of the time units with 0, then appropriately trim those away | ||
| temp := reSplit.FindAll([]byte(durationString), -1) | ||
| res := "" | ||
| for _, t := range temp { | ||
| if t[0] != '0' { | ||
| res += string(t) | ||
| } else { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // check if there are floating number points | ||
| if matched := re.MatchString(res); matched == false { | ||
| return "", errors.New("Invalid duration format") | ||
| } | ||
|
|
||
| return res, nil | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func makeDuration(h, m, s, ms int) time.Duration { | ||
| duration := h*int(time.Hour) + m*int(time.Minute) + s*int(time.Second) + ms*int(time.Millisecond) | ||
| return time.Duration(duration) | ||
| } | ||
|
|
||
| func TestParseDuration(t *testing.T) { | ||
| // valid durations | ||
| validTestCases := []struct { | ||
| name string | ||
| args string | ||
| expected time.Duration | ||
| }{ | ||
| { | ||
| name: "0h to timeDuration of 0s", | ||
| args: "0h", | ||
| expected: makeDuration(0, 0, 0, 0), | ||
| }, | ||
| { | ||
| name: "0s should be 0s", | ||
| args: "0s", | ||
| expected: makeDuration(0, 0, 0, 0), | ||
| }, | ||
| { | ||
| name: "0h0m0s should be 0s", | ||
| args: "0h0m0s", | ||
| expected: makeDuration(0, 0, 0, 0), | ||
| }, | ||
| { | ||
| name: "1h should be 1h", | ||
| args: "1h", | ||
| expected: makeDuration(1, 0, 0, 0), | ||
| }, | ||
| { | ||
| name: "30m should be 30m", | ||
| args: "30m", | ||
| expected: makeDuration(0, 30, 0, 0), | ||
| }, | ||
| { | ||
| name: "10s should be 10s", | ||
| args: "10s", | ||
| expected: makeDuration(0, 0, 10, 0), | ||
| }, | ||
| { | ||
| name: "500ms should be 500ms", | ||
| args: "500ms", | ||
| expected: makeDuration(0, 0, 0, 500), | ||
| }, | ||
| { | ||
| name: "2h30m should be 2h30m", | ||
| args: "2h30m", | ||
| expected: makeDuration(2, 30, 0, 0), | ||
| }, | ||
| { | ||
| name: "150m should be 2h30m", | ||
| args: "150m", | ||
| expected: makeDuration(0, 150, 0, 0), | ||
| }, | ||
| { | ||
| name: "7320s should be 2h30m", | ||
| args: "7320s", | ||
| expected: makeDuration(0, 0, 7320, 0), | ||
| }, | ||
| { | ||
| name: "1h30m10s shoulw be 1h30m10s", | ||
| args: "1h30m10s", | ||
| expected: makeDuration(1, 30, 10, 0), | ||
| }, | ||
| { | ||
| name: "10s30m1h should be 1h30m10s", | ||
| args: "10s30m1h", | ||
| expected: makeDuration(1, 30, 10, 0), | ||
| }, | ||
| { | ||
| name: "100ms200ms300ms should be 600ms", | ||
| args: "100ms200ms300ms", | ||
| expected: makeDuration(0, 0, 0, 600), | ||
| }, | ||
| } | ||
|
|
||
| invalidTestCases := []struct { | ||
| name string | ||
| args string | ||
| }{ | ||
| { | ||
| name: "Missing unit", | ||
| args: "1", | ||
| }, | ||
| { | ||
| name: "Missing unit in 1h1", | ||
| args: "1h1", | ||
| }, | ||
| { | ||
| name: "Too many units/components", | ||
| args: "1h30m10s20ms50h", | ||
| }, | ||
| { | ||
| name: "Too many digits", | ||
| args: "999999h", | ||
| }, | ||
| { | ||
| name: "No floating points allowed", | ||
| args: "1.5h", | ||
| }, | ||
| { | ||
| name: "No floating points for seconds allowed", | ||
| args: "0.5s", | ||
| }, | ||
| { | ||
| name: "Negative numbers not allowed", | ||
| args: "-15m", | ||
| }, | ||
| } | ||
|
|
||
| // Running valid test cases | ||
| for _, tc := range validTestCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| arg, _ := ParseDuration(tc.args) | ||
| assert.Equal(t, tc.expected, *arg) | ||
| }) | ||
| } | ||
|
|
||
| // Running invalid test cases | ||
| for _, tc := range invalidTestCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, errArg := ParseDuration(tc.args) | ||
| assert.Error(t, errArg) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestFormatDuration(t *testing.T) { | ||
| validTestCases := []struct { | ||
| name string | ||
| args string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "0 should be 0s", | ||
| args: "0", | ||
| expected: "0s", | ||
| }, | ||
| { | ||
| name: "1h should be 1h", | ||
| args: "1h", | ||
| expected: "1h", | ||
| }, | ||
| { | ||
| name: "30m should be 30m", | ||
| args: "30m", | ||
| expected: "30m", | ||
| }, | ||
| { | ||
| name: "10s should be 10s", | ||
| args: "10s", | ||
| expected: "10s", | ||
| }, | ||
| { | ||
| name: "500ms should be 500ms", | ||
| args: "500ms", | ||
| expected: "500ms", | ||
| }, | ||
| { | ||
| name: "2h30m should be 2h30m", | ||
| args: "2h30m", | ||
| expected: "2h30m", | ||
| }, | ||
| { | ||
| name: "1h30m10s should be 1h30m10s", | ||
| args: "1h30m10s", | ||
| expected: "1h30m10s", | ||
| }, | ||
| { | ||
| name: "600ms should be 600ms", | ||
| args: "600ms", | ||
| expected: "600ms", | ||
| }, | ||
| { | ||
| name: "2h600ms should be 2h600ms", | ||
| args: "2h600ms", | ||
| expected: "2h600ms", | ||
| }, | ||
| { | ||
| name: "2h30m600ms should be 2h30m600ms", | ||
| args: "2h30m600ms", | ||
| expected: "2h30m600ms", | ||
| }, | ||
| { | ||
| name: "2h30m10s600ms should be 2h30m10s600ms", | ||
| args: "2h30m10s600ms", | ||
| expected: "2h30m10s600ms", | ||
| }, | ||
| { | ||
| name: "0.5m should be 30s", | ||
| args: "0.5m", | ||
| expected: "30s", | ||
| }, | ||
| { | ||
| name: "0.5s should be 500ms", | ||
| args: "0.5s", | ||
| expected: "500ms", | ||
| }, | ||
| } | ||
|
|
||
| // invalid test cases | ||
| invalidTestCases := []struct { | ||
| name string | ||
| args string | ||
| }{ | ||
| { | ||
| name: "Sub-milliseconds not allowed (100us)", | ||
| args: "100us", | ||
| }, | ||
| { | ||
| name: "Sub-milliseconds not allowed (0.5ms)", | ||
| args: "0.5ms", | ||
| }, | ||
| { | ||
| name: "Out of range (greater than 99999 hours)", | ||
| args: "100000h", | ||
| }, | ||
|
xtineskim marked this conversation as resolved.
|
||
| { | ||
| name: "Negative duration not supported", | ||
| args: "-10h", | ||
| }, | ||
| } | ||
| // Valid test cases | ||
|
|
||
| for _, tc := range validTestCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| a, _ := time.ParseDuration(tc.args) | ||
| arg, _ := FormatDuration(a) | ||
| assert.Equal(t, tc.expected, arg) | ||
| }) | ||
| } | ||
|
|
||
| // Invalid test cases | ||
| for _, tc := range invalidTestCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, errArg := ParseDuration(tc.args) | ||
| assert.Error(t, errArg) | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.