From 2bbd4d0ee3857c9e6d0b07c68c8461498b840767 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 1 Jun 2021 19:45:26 +0300 Subject: [PATCH] [#493] node/config: Implement integer casters Implement `Int` / `Uint` functions which casts value to `int64` / `uint64`. Implement safe functions `IntSafe` / `UintSafe`. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config/cast.go | 57 ++++++++++++++++++++++++++ cmd/neofs-node/config/cast_test.go | 34 +++++++++++++++ cmd/neofs-node/config/test/config.json | 8 ++++ cmd/neofs-node/config/test/config.yaml | 9 +++- 4 files changed, 107 insertions(+), 1 deletion(-) diff --git a/cmd/neofs-node/config/cast.go b/cmd/neofs-node/config/cast.go index 566d8221..2f6c5d52 100644 --- a/cmd/neofs-node/config/cast.go +++ b/cmd/neofs-node/config/cast.go @@ -68,3 +68,60 @@ func Duration(c *Config, name string) time.Duration { func DurationSafe(c *Config, name string) time.Duration { return cast.ToDuration(c.Value(name)) } + +// Bool reads configuration value +// from c by name and casts it to bool. +// +// Panics if value can not be casted. +func Bool(c *Config, name string) bool { + x, err := cast.ToBoolE(c.Value(name)) + panicOnErr(err) + + return x +} + +// BoolSafe reads configuration value +// from c by name and casts it to bool. +// +// Returns false if value can not be casted. +func BoolSafe(c *Config, name string) bool { + return cast.ToBool(c.Value(name)) +} + +// Uint reads configuration value +// from c by name and casts it to uint64. +// +// Panics if value can not be casted. +func Uint(c *Config, name string) uint64 { + x, err := cast.ToUint64E(c.Value(name)) + panicOnErr(err) + + return x +} + +// UintSafe reads configuration value +// from c by name and casts it to uint64. +// +// Returns 0 if value can not be casted. +func UintSafe(c *Config, name string) uint64 { + return cast.ToUint64(c.Value(name)) +} + +// Int reads configuration value +// from c by name and casts it to int64. +// +// Panics if value can not be casted. +func Int(c *Config, name string) int64 { + x, err := cast.ToInt64E(c.Value(name)) + panicOnErr(err) + + return x +} + +// IntSafe reads configuration value +// from c by name and casts it to int64. +// +// Returns 0 if value can not be casted. +func IntSafe(c *Config, name string) int64 { + return cast.ToInt64(c.Value(name)) +} diff --git a/cmd/neofs-node/config/cast_test.go b/cmd/neofs-node/config/cast_test.go index ac5e699f..9111f295 100644 --- a/cmd/neofs-node/config/cast_test.go +++ b/cmd/neofs-node/config/cast_test.go @@ -62,3 +62,37 @@ func TestDuration(t *testing.T) { require.Equal(t, time.Duration(0), val) }) } + +func TestNumbers(t *testing.T) { + configtest.ForEachFileType("test/config", func(c *config.Config) { + c = c.Sub("number") + + const ( + intPos = "int_pos" + intNeg = "int_neg" + + fractPos = "fract_pos" + fractNeg = "fract_neg" + + incorrect = "incorrect" + ) + + require.EqualValues(t, 1, config.Int(c, intPos)) + require.EqualValues(t, 1, config.Uint(c, intPos)) + + require.EqualValues(t, -1, config.Int(c, intNeg)) + require.Panics(t, func() { config.Uint(c, intNeg) }) + + require.EqualValues(t, 2, config.Int(c, fractPos)) + require.EqualValues(t, 2, config.Uint(c, fractPos)) + + require.EqualValues(t, -2, config.Int(c, fractNeg)) + require.Panics(t, func() { config.Uint(c, fractNeg) }) + + require.Panics(t, func() { config.Int(c, incorrect) }) + require.Panics(t, func() { config.Uint(c, incorrect) }) + + require.Zero(t, config.IntSafe(c, incorrect)) + require.Zero(t, config.UintSafe(c, incorrect)) + }) +} diff --git a/cmd/neofs-node/config/test/config.json b/cmd/neofs-node/config/test/config.json index 1523ff79..8332e3e8 100644 --- a/cmd/neofs-node/config/test/config.json +++ b/cmd/neofs-node/config/test/config.json @@ -3,6 +3,7 @@ "section": { "any": "thing" }, + "string_slice": { "empty": [], "filled": [ @@ -18,5 +19,12 @@ "duration": { "correct": "15m", "incorrect": "some string" + }, + "number": { + "int_pos": 1, + "int_neg": -1, + "fract_pos": 2.5, + "fract_neg": -2.5, + "incorrect": "some string" } } diff --git a/cmd/neofs-node/config/test/config.yaml b/cmd/neofs-node/config/test/config.yaml index 39d2f1af..1b5f6c07 100644 --- a/cmd/neofs-node/config/test/config.yaml +++ b/cmd/neofs-node/config/test/config.yaml @@ -19,4 +19,11 @@ string: duration: correct: 15m - incorrect: some string \ No newline at end of file + incorrect: some string + +number: + int_pos: 1 + int_neg: -1 + fract_pos: 2.5 + fract_neg: -2.5 + incorrect: some string