forked from TrueCloudLab/frostfs-node
[#493] cmd/node: Support duration type casting in config
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
25a13d3611
commit
922d29ff88
4 changed files with 46 additions and 0 deletions
|
@ -1,6 +1,8 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,3 +49,22 @@ func String(c *Config, name string) string {
|
||||||
func StringSafe(c *Config, name string) string {
|
func StringSafe(c *Config, name string) string {
|
||||||
return cast.ToString(c.Value(name))
|
return cast.ToString(c.Value(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Duration reads configuration value
|
||||||
|
// from c by name and casts it to time.Duration.
|
||||||
|
//
|
||||||
|
// Panics if value can not be casted.
|
||||||
|
func Duration(c *Config, name string) time.Duration {
|
||||||
|
x, err := cast.ToDurationE(c.Value(name))
|
||||||
|
panicOnErr(err)
|
||||||
|
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
// DurationSafe reads configuration value
|
||||||
|
// from c by name and casts it to time.Duration.
|
||||||
|
//
|
||||||
|
// Returns 0 if value can not be casted.
|
||||||
|
func DurationSafe(c *Config, name string) time.Duration {
|
||||||
|
return cast.ToDuration(c.Value(name))
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package config_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
||||||
configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test"
|
configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test"
|
||||||
|
@ -45,3 +46,19 @@ func TestString(t *testing.T) {
|
||||||
require.Empty(t, val)
|
require.Empty(t, val)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDuration(t *testing.T) {
|
||||||
|
configtest.ForEachFileType("test/config", func(c *config.Config) {
|
||||||
|
c = c.Sub("duration")
|
||||||
|
|
||||||
|
val := config.Duration(c, "correct")
|
||||||
|
require.Equal(t, 15*time.Minute, val)
|
||||||
|
|
||||||
|
require.Panics(t, func() {
|
||||||
|
config.Duration(c, "incorrect")
|
||||||
|
})
|
||||||
|
|
||||||
|
val = config.DurationSafe(c, "incorrect")
|
||||||
|
require.Equal(t, time.Duration(0), val)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -14,5 +14,9 @@
|
||||||
"string": {
|
"string": {
|
||||||
"correct": "some string",
|
"correct": "some string",
|
||||||
"incorrect": []
|
"incorrect": []
|
||||||
|
},
|
||||||
|
"duration": {
|
||||||
|
"correct": "15m",
|
||||||
|
"incorrect": "some string"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,3 +16,7 @@ string:
|
||||||
correct: some string
|
correct: some string
|
||||||
|
|
||||||
incorrect: []
|
incorrect: []
|
||||||
|
|
||||||
|
duration:
|
||||||
|
correct: 15m
|
||||||
|
incorrect: some string
|
Loading…
Reference in a new issue