[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-01 19:45:26 +03:00 committed by Leonard Lyubich
parent efcd12c71e
commit 2bbd4d0ee3
4 changed files with 107 additions and 1 deletions

View file

@ -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))
}