forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/knadh/koanf/getters.go

1165 lines
16 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package koanf
import (
"fmt"
"time"
)
// Int64 returns the int64 value of a given key path or 0 if the path
2024-02-18 10:42:21 +00:00
// does not exist or if the value is not a valid int64.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Int64(path string) int64 {
2024-02-18 10:42:21 +00:00
if v := ko.Get(path); v != nil {
2024-02-18 10:42:21 +00:00
i, _ := toInt64(v)
2024-02-18 10:42:21 +00:00
return i
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
// MustInt64 returns the int64 value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value of 0.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustInt64(path string) int64 {
2024-02-18 10:42:21 +00:00
val := ko.Int64(path)
2024-02-18 10:42:21 +00:00
if val == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Int64s returns the []int64 slice value of a given key path or an
2024-02-18 10:42:21 +00:00
// empty []int64 slice if the path does not exist or if the value
2024-02-18 10:42:21 +00:00
// is not a valid int slice.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Int64s(path string) []int64 {
2024-02-18 10:42:21 +00:00
o := ko.Get(path)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return []int64{}
2024-02-18 10:42:21 +00:00
}
var out []int64
2024-02-18 10:42:21 +00:00
switch v := o.(type) {
2024-02-18 10:42:21 +00:00
case []int64:
2024-02-18 10:42:21 +00:00
return v
2024-02-18 10:42:21 +00:00
case []int:
2024-02-18 10:42:21 +00:00
out = make([]int64, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, vi := range v {
2024-02-18 10:42:21 +00:00
i, err := toInt64(vi)
// On error, return as it's not a valid
2024-02-18 10:42:21 +00:00
// int slice.
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return []int64{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out = append(out, i)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
case []interface{}:
2024-02-18 10:42:21 +00:00
out = make([]int64, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, vi := range v {
2024-02-18 10:42:21 +00:00
i, err := toInt64(vi)
// On error, return as it's not a valid
2024-02-18 10:42:21 +00:00
// int slice.
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return []int64{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out = append(out, i)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
return []int64{}
2024-02-18 10:42:21 +00:00
}
// MustInt64s returns the []int64 slice value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or its default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustInt64s(path string) []int64 {
2024-02-18 10:42:21 +00:00
val := ko.Int64s(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Int64Map returns the map[string]int64 value of a given key path
2024-02-18 10:42:21 +00:00
// or an empty map[string]int64 if the path does not exist or if the
2024-02-18 10:42:21 +00:00
// value is not a valid int64 map.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Int64Map(path string) map[string]int64 {
2024-02-18 10:42:21 +00:00
var (
out = map[string]int64{}
o = ko.Get(path)
2024-02-18 10:42:21 +00:00
)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
mp, ok := o.(map[string]interface{})
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
out = make(map[string]int64, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
switch i := v.(type) {
2024-02-18 10:42:21 +00:00
case int64:
2024-02-18 10:42:21 +00:00
out[k] = i
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// Attempt a conversion.
2024-02-18 10:42:21 +00:00
iv, err := toInt64(i)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return map[string]int64{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out[k] = iv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
// MustInt64Map returns the map[string]int64 value of a given key path
2024-02-18 10:42:21 +00:00
// or panics if its not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustInt64Map(path string) map[string]int64 {
2024-02-18 10:42:21 +00:00
val := ko.Int64Map(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Int returns the int value of a given key path or 0 if the path
2024-02-18 10:42:21 +00:00
// does not exist or if the value is not a valid int.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Int(path string) int {
2024-02-18 10:42:21 +00:00
return int(ko.Int64(path))
2024-02-18 10:42:21 +00:00
}
// MustInt returns the int value of a given key path or panics
2024-02-18 10:42:21 +00:00
// or panics if its not set or set to default value of 0.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustInt(path string) int {
2024-02-18 10:42:21 +00:00
val := ko.Int(path)
2024-02-18 10:42:21 +00:00
if val == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Ints returns the []int slice value of a given key path or an
2024-02-18 10:42:21 +00:00
// empty []int slice if the path does not exist or if the value
2024-02-18 10:42:21 +00:00
// is not a valid int slice.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Ints(path string) []int {
2024-02-18 10:42:21 +00:00
o := ko.Get(path)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return []int{}
2024-02-18 10:42:21 +00:00
}
var out []int
2024-02-18 10:42:21 +00:00
switch v := o.(type) {
2024-02-18 10:42:21 +00:00
case []int:
2024-02-18 10:42:21 +00:00
return v
2024-02-18 10:42:21 +00:00
case []int64:
2024-02-18 10:42:21 +00:00
out = make([]int, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, vi := range v {
2024-02-18 10:42:21 +00:00
out = append(out, int(vi))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
case []interface{}:
2024-02-18 10:42:21 +00:00
out = make([]int, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, vi := range v {
2024-02-18 10:42:21 +00:00
i, err := toInt64(vi)
// On error, return as it's not a valid
2024-02-18 10:42:21 +00:00
// int slice.
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return []int{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out = append(out, int(i))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
return []int{}
2024-02-18 10:42:21 +00:00
}
// MustInts returns the []int slice value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustInts(path string) []int {
2024-02-18 10:42:21 +00:00
val := ko.Ints(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// IntMap returns the map[string]int value of a given key path
2024-02-18 10:42:21 +00:00
// or an empty map[string]int if the path does not exist or if the
2024-02-18 10:42:21 +00:00
// value is not a valid int map.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) IntMap(path string) map[string]int {
2024-02-18 10:42:21 +00:00
var (
mp = ko.Int64Map(path)
2024-02-18 10:42:21 +00:00
out = make(map[string]int, len(mp))
)
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
out[k] = int(v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
// MustIntMap returns the map[string]int value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustIntMap(path string) map[string]int {
2024-02-18 10:42:21 +00:00
val := ko.IntMap(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Float64 returns the float64 value of a given key path or 0 if the path
2024-02-18 10:42:21 +00:00
// does not exist or if the value is not a valid float64.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Float64(path string) float64 {
2024-02-18 10:42:21 +00:00
if v := ko.Get(path); v != nil {
2024-02-18 10:42:21 +00:00
f, _ := toFloat64(v)
2024-02-18 10:42:21 +00:00
return f
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
// MustFloat64 returns the float64 value of a given key path or panics
2024-02-18 10:42:21 +00:00
// or panics if its not set or set to default value 0.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustFloat64(path string) float64 {
2024-02-18 10:42:21 +00:00
val := ko.Float64(path)
2024-02-18 10:42:21 +00:00
if val == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Float64s returns the []float64 slice value of a given key path or an
2024-02-18 10:42:21 +00:00
// empty []float64 slice if the path does not exist or if the value
2024-02-18 10:42:21 +00:00
// is not a valid float64 slice.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Float64s(path string) []float64 {
2024-02-18 10:42:21 +00:00
o := ko.Get(path)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return []float64{}
2024-02-18 10:42:21 +00:00
}
var out []float64
2024-02-18 10:42:21 +00:00
switch v := o.(type) {
2024-02-18 10:42:21 +00:00
case []float64:
2024-02-18 10:42:21 +00:00
return v
2024-02-18 10:42:21 +00:00
case []interface{}:
2024-02-18 10:42:21 +00:00
out = make([]float64, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, vi := range v {
2024-02-18 10:42:21 +00:00
i, err := toFloat64(vi)
// On error, return as it's not a valid
2024-02-18 10:42:21 +00:00
// int slice.
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return []float64{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out = append(out, i)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
return []float64{}
2024-02-18 10:42:21 +00:00
}
// MustFloat64s returns the []Float64 slice value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustFloat64s(path string) []float64 {
2024-02-18 10:42:21 +00:00
val := ko.Float64s(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Float64Map returns the map[string]float64 value of a given key path
2024-02-18 10:42:21 +00:00
// or an empty map[string]float64 if the path does not exist or if the
2024-02-18 10:42:21 +00:00
// value is not a valid float64 map.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Float64Map(path string) map[string]float64 {
2024-02-18 10:42:21 +00:00
var (
out = map[string]float64{}
o = ko.Get(path)
2024-02-18 10:42:21 +00:00
)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
mp, ok := o.(map[string]interface{})
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
out = make(map[string]float64, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
switch i := v.(type) {
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
out[k] = i
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// Attempt a conversion.
2024-02-18 10:42:21 +00:00
iv, err := toFloat64(i)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return map[string]float64{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out[k] = iv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
// MustFloat64Map returns the map[string]float64 value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustFloat64Map(path string) map[string]float64 {
2024-02-18 10:42:21 +00:00
val := ko.Float64Map(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Duration returns the time.Duration value of a given key path assuming
2024-02-18 10:42:21 +00:00
// that the key contains a valid numeric value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Duration(path string) time.Duration {
2024-02-18 10:42:21 +00:00
// Look for a parsable string representation first.
2024-02-18 10:42:21 +00:00
if v := ko.Int64(path); v != 0 {
2024-02-18 10:42:21 +00:00
return time.Duration(v)
2024-02-18 10:42:21 +00:00
}
v, _ := time.ParseDuration(ko.String(path))
2024-02-18 10:42:21 +00:00
return v
2024-02-18 10:42:21 +00:00
}
// MustDuration returns the time.Duration value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if its not set or set to default value 0.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustDuration(path string) time.Duration {
2024-02-18 10:42:21 +00:00
val := ko.Duration(path)
2024-02-18 10:42:21 +00:00
if val == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Time attempts to parse the value of a given key path and return time.Time
2024-02-18 10:42:21 +00:00
// representation. If the value is numeric, it is treated as a UNIX timestamp
2024-02-18 10:42:21 +00:00
// and if it's string, a parse is attempted with the given layout.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Time(path, layout string) time.Time {
2024-02-18 10:42:21 +00:00
// Unix timestamp?
2024-02-18 10:42:21 +00:00
v := ko.Int64(path)
2024-02-18 10:42:21 +00:00
if v != 0 {
2024-02-18 10:42:21 +00:00
return time.Unix(v, 0)
2024-02-18 10:42:21 +00:00
}
// String representation.
2024-02-18 10:42:21 +00:00
s := ko.String(path)
2024-02-18 10:42:21 +00:00
if s != "" {
2024-02-18 10:42:21 +00:00
t, _ := time.Parse(layout, s)
2024-02-18 10:42:21 +00:00
return t
2024-02-18 10:42:21 +00:00
}
return time.Time{}
2024-02-18 10:42:21 +00:00
}
// MustTime attempts to parse the value of a given key path and return time.Time
2024-02-18 10:42:21 +00:00
// representation. If the value is numeric, it is treated as a UNIX timestamp
2024-02-18 10:42:21 +00:00
// and if it's string, a parse is attempted with the given layout. It panics if
2024-02-18 10:42:21 +00:00
// the parsed time is zero.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustTime(path, layout string) time.Time {
2024-02-18 10:42:21 +00:00
val := ko.Time(path, layout)
2024-02-18 10:42:21 +00:00
if val.IsZero() {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// String returns the string value of a given key path or "" if the path
2024-02-18 10:42:21 +00:00
// does not exist or if the value is not a valid string.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) String(path string) string {
2024-02-18 10:42:21 +00:00
if v := ko.Get(path); v != nil {
2024-02-18 10:42:21 +00:00
if i, ok := v.(string); ok {
2024-02-18 10:42:21 +00:00
return i
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("%v", v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
// MustString returns the string value of a given key path
2024-02-18 10:42:21 +00:00
// or panics if its not set or set to default value "".
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustString(path string) string {
2024-02-18 10:42:21 +00:00
val := ko.String(path)
2024-02-18 10:42:21 +00:00
if val == "" {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Strings returns the []string slice value of a given key path or an
2024-02-18 10:42:21 +00:00
// empty []string slice if the path does not exist or if the value
2024-02-18 10:42:21 +00:00
// is not a valid string slice.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Strings(path string) []string {
2024-02-18 10:42:21 +00:00
o := ko.Get(path)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return []string{}
2024-02-18 10:42:21 +00:00
}
var out []string
2024-02-18 10:42:21 +00:00
switch v := o.(type) {
2024-02-18 10:42:21 +00:00
case []interface{}:
2024-02-18 10:42:21 +00:00
out = make([]string, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, u := range v {
2024-02-18 10:42:21 +00:00
if s, ok := u.(string); ok {
2024-02-18 10:42:21 +00:00
out = append(out, s)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
out = append(out, fmt.Sprintf("%v", u))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
case []string:
2024-02-18 10:42:21 +00:00
out := make([]string, len(v))
2024-02-18 10:42:21 +00:00
copy(out[:], v[:])
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
return []string{}
2024-02-18 10:42:21 +00:00
}
// MustStrings returns the []string slice value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustStrings(path string) []string {
2024-02-18 10:42:21 +00:00
val := ko.Strings(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// StringMap returns the map[string]string value of a given key path
2024-02-18 10:42:21 +00:00
// or an empty map[string]string if the path does not exist or if the
2024-02-18 10:42:21 +00:00
// value is not a valid string map.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) StringMap(path string) map[string]string {
2024-02-18 10:42:21 +00:00
var (
out = map[string]string{}
o = ko.Get(path)
2024-02-18 10:42:21 +00:00
)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
switch mp := o.(type) {
2024-02-18 10:42:21 +00:00
case map[string]string:
2024-02-18 10:42:21 +00:00
out = make(map[string]string, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
out[k] = v
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case map[string]interface{}:
2024-02-18 10:42:21 +00:00
out = make(map[string]string, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
switch s := v.(type) {
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
out[k] = s
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// There's a non string type. Return.
2024-02-18 10:42:21 +00:00
return map[string]string{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return out
2024-02-18 10:42:21 +00:00
}
// MustStringMap returns the map[string]string value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustStringMap(path string) map[string]string {
2024-02-18 10:42:21 +00:00
val := ko.StringMap(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// StringsMap returns the map[string][]string value of a given key path
2024-02-18 10:42:21 +00:00
// or an empty map[string][]string if the path does not exist or if the
2024-02-18 10:42:21 +00:00
// value is not a valid strings map.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) StringsMap(path string) map[string][]string {
2024-02-18 10:42:21 +00:00
var (
out = map[string][]string{}
o = ko.Get(path)
2024-02-18 10:42:21 +00:00
)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
switch mp := o.(type) {
2024-02-18 10:42:21 +00:00
case map[string][]string:
2024-02-18 10:42:21 +00:00
out = make(map[string][]string, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
out[k] = make([]string, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, s := range v {
2024-02-18 10:42:21 +00:00
out[k] = append(out[k], s)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case map[string][]interface{}:
2024-02-18 10:42:21 +00:00
out = make(map[string][]string, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
for _, v := range v {
2024-02-18 10:42:21 +00:00
switch sv := v.(type) {
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
out[k] = append(out[k], sv)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return map[string][]string{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case map[string]interface{}:
2024-02-18 10:42:21 +00:00
out = make(map[string][]string, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
switch s := v.(type) {
2024-02-18 10:42:21 +00:00
case []string:
2024-02-18 10:42:21 +00:00
for _, v := range s {
2024-02-18 10:42:21 +00:00
out[k] = append(out[k], v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case []interface{}:
2024-02-18 10:42:21 +00:00
for _, v := range s {
2024-02-18 10:42:21 +00:00
switch sv := v.(type) {
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
out[k] = append(out[k], sv)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return map[string][]string{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// There's a non []interface type. Return.
2024-02-18 10:42:21 +00:00
return map[string][]string{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return out
2024-02-18 10:42:21 +00:00
}
// MustStringsMap returns the map[string][]string value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustStringsMap(path string) map[string][]string {
2024-02-18 10:42:21 +00:00
val := ko.StringsMap(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Bytes returns the []byte value of a given key path or an empty
2024-02-18 10:42:21 +00:00
// []byte slice if the path does not exist or if the value is not a valid string.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Bytes(path string) []byte {
2024-02-18 10:42:21 +00:00
return []byte(ko.String(path))
2024-02-18 10:42:21 +00:00
}
// MustBytes returns the []byte value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustBytes(path string) []byte {
2024-02-18 10:42:21 +00:00
val := ko.Bytes(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// Bool returns the bool value of a given key path or false if the path
2024-02-18 10:42:21 +00:00
// does not exist or if the value is not a valid bool representation.
2024-02-18 10:42:21 +00:00
// Accepted string representations of bool are the ones supported by strconv.ParseBool.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Bool(path string) bool {
2024-02-18 10:42:21 +00:00
if v := ko.Get(path); v != nil {
2024-02-18 10:42:21 +00:00
b, _ := toBool(v)
2024-02-18 10:42:21 +00:00
return b
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Bools returns the []bool slice value of a given key path or an
2024-02-18 10:42:21 +00:00
// empty []bool slice if the path does not exist or if the value
2024-02-18 10:42:21 +00:00
// is not a valid bool slice.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) Bools(path string) []bool {
2024-02-18 10:42:21 +00:00
o := ko.Get(path)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return []bool{}
2024-02-18 10:42:21 +00:00
}
var out []bool
2024-02-18 10:42:21 +00:00
switch v := o.(type) {
2024-02-18 10:42:21 +00:00
case []interface{}:
2024-02-18 10:42:21 +00:00
out = make([]bool, 0, len(v))
2024-02-18 10:42:21 +00:00
for _, u := range v {
2024-02-18 10:42:21 +00:00
b, err := toBool(u)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out = append(out, b)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
case []bool:
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// MustBools returns the []bool value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustBools(path string) []bool {
2024-02-18 10:42:21 +00:00
val := ko.Bools(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}
// BoolMap returns the map[string]bool value of a given key path
2024-02-18 10:42:21 +00:00
// or an empty map[string]bool if the path does not exist or if the
2024-02-18 10:42:21 +00:00
// value is not a valid bool map.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) BoolMap(path string) map[string]bool {
2024-02-18 10:42:21 +00:00
var (
out = map[string]bool{}
o = ko.Get(path)
2024-02-18 10:42:21 +00:00
)
2024-02-18 10:42:21 +00:00
if o == nil {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
mp, ok := o.(map[string]interface{})
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out = make(map[string]bool, len(mp))
2024-02-18 10:42:21 +00:00
for k, v := range mp {
2024-02-18 10:42:21 +00:00
switch i := v.(type) {
2024-02-18 10:42:21 +00:00
case bool:
2024-02-18 10:42:21 +00:00
out[k] = i
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// Attempt a conversion.
2024-02-18 10:42:21 +00:00
b, err := toBool(i)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return map[string]bool{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out[k] = b
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return out
2024-02-18 10:42:21 +00:00
}
// MustBoolMap returns the map[string]bool value of a given key path or panics
2024-02-18 10:42:21 +00:00
// if the value is not set or set to default value.
2024-02-18 10:42:21 +00:00
func (ko *Koanf) MustBoolMap(path string) map[string]bool {
2024-02-18 10:42:21 +00:00
val := ko.BoolMap(path)
2024-02-18 10:42:21 +00:00
if len(val) == 0 {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("invalid value: %s=%v", path, val))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return val
2024-02-18 10:42:21 +00:00
}