Skip to content

Commit 7219d5c

Browse files
authored
fix: ISO8601 duration wouldn't parse for valid fractional values (#199)
* fix: ISO8601 duration won't parse for valid fractional values Previously, the library utilized a parsing library for ISO8601 durations that parsed durations into integers only. This caused issues with durations containing fractional values, such as "P0.5S", which are valid ISO8601 strings but failed to parse due to the limitation of the previous library. In this commit, I've replaced the outdated parsing library with a more robust alternative. The new library properly handles fractional values, ensuring accurate parsing of ISO8601 duration strings, including cases like "P0.5S". This update enhances the functionality and reliability of the library by accommodating a broader range of ISO8601 duration formats. Additionally, it ensures compatibility with modern standards and improves the overall usability of the library. Signed-off-by: Kshitiz Agrawal <[email protected]> * fix: resolve failing test cases Signed-off-by: Kshitiz Agrawal <[email protected]> * feat: add test case for fractional ISO duration Signed-off-by: Kshitiz Agrawal <[email protected]> --------- Signed-off-by: Kshitiz Agrawal <[email protected]>
1 parent bb89ed0 commit 7219d5c

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/go-playground/validator/v10 v10.11.1
77
github.com/pkg/errors v0.9.1
88
github.com/relvacode/iso8601 v1.3.0
9-
github.com/senseyeio/duration v0.0.0-20180430131211-7c2a214ada46
9+
github.com/sosodev/duration v1.2.0
1010
github.com/stretchr/testify v1.8.0
1111
gopkg.in/yaml.v3 v3.0.1
1212
k8s.io/apimachinery v0.26.2

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ github.com/relvacode/iso8601 v1.3.0/go.mod h1:FlNp+jz+TXpyRqgmM7tnzHHzBnz776kmAH
5050
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
5151
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
5252
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
53-
github.com/senseyeio/duration v0.0.0-20180430131211-7c2a214ada46 h1:Dz0HrI1AtNSGCE8LXLLqoZU4iuOJXPWndenCsZfstA8=
54-
github.com/senseyeio/duration v0.0.0-20180430131211-7c2a214ada46/go.mod h1:is8FVkzSi7PYLWEXT5MgWhglFsyyiW8ffxAoJqfuFZo=
53+
github.com/sosodev/duration v1.2.0 h1:pqK/FLSjsAADWY74SyWDCjOcd5l7H8GSnnOGEB9A1Us=
54+
github.com/sosodev/duration v1.2.0/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg=
5555
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
5656
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5757
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=

validator/validator.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ package validator
1616

1717
import (
1818
"context"
19+
"errors"
1920
"strconv"
2021

2122
"github.com/relvacode/iso8601"
22-
"github.com/senseyeio/duration"
23+
"github.com/sosodev/duration"
2324
"k8s.io/apimachinery/pkg/util/intstr"
2425

2526
validator "github.com/go-playground/validator/v10"
@@ -60,7 +61,13 @@ func GetValidator() *validator.Validate {
6061

6162
// ValidateISO8601TimeDuration validate the string is iso8601 duration format
6263
func ValidateISO8601TimeDuration(s string) error {
63-
_, err := duration.ParseISO8601(s)
64+
if s == "" {
65+
return errors.New("could not parse duration string")
66+
}
67+
_, err := duration.Parse(s)
68+
if err != nil {
69+
return errors.New("could not parse duration string")
70+
}
6471
return err
6572
}
6673

validator/validator_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func TestValidateISO8601TimeDuration(t *testing.T) {
3838
s: "PT5S",
3939
err: ``,
4040
},
41+
{
42+
desp: "fractional_second_designator",
43+
s: "PT0.5S",
44+
err: ``,
45+
},
4146
{
4247
desp: "empty value",
4348
s: "",

0 commit comments

Comments
 (0)