Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bergundy committed Sep 13, 2024
1 parent b8e621d commit 66e899e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions nexus/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,14 @@ func validateLinkType(value string) error {
return nil
}

var durationRegexp = regexp.MustCompile("^(\\d+)(ms|s|m|h)$")
var durationRegexp = regexp.MustCompile("^(\\d+(?:\\.\\d+)?)(ms|s|m)$")

Check failure on line 339 in nexus/api.go

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest)

S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple)

func parseDuration(value string) (time.Duration, error) {
m := durationRegexp.FindStringSubmatch(value)
if len(m) == 0 {
return 0, fmt.Errorf("invalid duration: %q", value)
}
v, err := strconv.ParseInt(m[1], 10, 64)
v, err := strconv.ParseFloat(m[1], 64)
if err != nil {
return 0, err
}
Expand All @@ -352,15 +352,14 @@ func parseDuration(value string) (time.Duration, error) {
case "ms":
return time.Millisecond * time.Duration(v), nil
case "s":
return time.Second * time.Duration(v), nil
return time.Millisecond * time.Duration(v*1e3), nil
case "m":
return time.Minute * time.Duration(v), nil
case "h":
return time.Hour * time.Duration(v), nil
return time.Millisecond * time.Duration(v*1e3*60), nil
}
panic("unreachable")
}

// formatDuration converts a duration into a string representation in millisecond resolution.
func formatDuration(d time.Duration) string {
return strconv.FormatInt(d.Milliseconds(), 10) + "ms"
}
4 changes: 2 additions & 2 deletions nexus/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func TestParseDuration(t *testing.T) {
d, err = parseDuration("999m")
require.NoError(t, err)
require.Equal(t, 999*time.Minute, d)
d, err = parseDuration("6h")
d, err = parseDuration("1.3s")
require.NoError(t, err)
require.Equal(t, 6*time.Hour, d)
require.Equal(t, 1300*time.Millisecond, d)
}

0 comments on commit 66e899e

Please sign in to comment.