fix: update parsing logic of config.Duration (#10803)
This commit is contained in:
parent
21fd54c77c
commit
1560346402
|
|
@ -1,8 +1,8 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alecthomas/units"
|
"github.com/alecthomas/units"
|
||||||
|
|
@ -16,40 +16,34 @@ type Size int64
|
||||||
|
|
||||||
// UnmarshalTOML parses the duration from the TOML config file
|
// UnmarshalTOML parses the duration from the TOML config file
|
||||||
func (d *Duration) UnmarshalTOML(b []byte) error {
|
func (d *Duration) UnmarshalTOML(b []byte) error {
|
||||||
var err error
|
// convert to string
|
||||||
b = bytes.Trim(b, `'`)
|
durStr := string(b)
|
||||||
|
|
||||||
// see if we can directly convert it
|
|
||||||
dur, err := time.ParseDuration(string(b))
|
|
||||||
if err == nil {
|
|
||||||
*d = Duration(dur)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse string duration, ie, "1s"
|
|
||||||
if uq, err := strconv.Unquote(string(b)); err == nil && len(uq) > 0 {
|
|
||||||
dur, err := time.ParseDuration(uq)
|
|
||||||
if err == nil {
|
|
||||||
*d = Duration(dur)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Value is a TOML number (e.g. 3, 10, 3.5)
|
||||||
// First try parsing as integer seconds
|
// First try parsing as integer seconds
|
||||||
sI, err := strconv.ParseInt(string(b), 10, 64)
|
sI, err := strconv.ParseInt(durStr, 10, 64)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dur := time.Second * time.Duration(sI)
|
dur := time.Second * time.Duration(sI)
|
||||||
*d = Duration(dur)
|
*d = Duration(dur)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Second try parsing as float seconds
|
// Second try parsing as float seconds
|
||||||
sF, err := strconv.ParseFloat(string(b), 64)
|
sF, err := strconv.ParseFloat(durStr, 64)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dur := time.Second * time.Duration(sF)
|
dur := time.Second * time.Duration(sF)
|
||||||
*d = Duration(dur)
|
*d = Duration(dur)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finally, try value is a TOML string (e.g. "3s", 3s) or literal (e.g. '3s')
|
||||||
|
durStr = strings.ReplaceAll(durStr, "'", "")
|
||||||
|
durStr = strings.ReplaceAll(durStr, "\"", "")
|
||||||
|
dur, err := time.ParseDuration(durStr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*d = Duration(dur)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,11 @@ func TestDuration(t *testing.T) {
|
||||||
d = config.Duration(0)
|
d = config.Duration(0)
|
||||||
require.NoError(t, d.UnmarshalTOML([]byte(`1.5`)))
|
require.NoError(t, d.UnmarshalTOML([]byte(`1.5`)))
|
||||||
require.Equal(t, time.Second, time.Duration(d))
|
require.Equal(t, time.Second, time.Duration(d))
|
||||||
|
|
||||||
|
require.Error(t, d.UnmarshalTOML([]byte(`"1"`))) // string missing unit
|
||||||
|
require.Error(t, d.UnmarshalTOML([]byte(`'2'`))) // string missing unit
|
||||||
|
require.Error(t, d.UnmarshalTOML([]byte(`'ns'`))) // string missing time
|
||||||
|
require.Error(t, d.UnmarshalTOML([]byte(`'us'`))) // string missing time
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSize(t *testing.T) {
|
func TestSize(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue