| 1 |
package koanf |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
// Int64 returns the int64 value of a given key path or 0 if the path |
| 9 |
// does not exist or if the value is not a valid int64. |
| 10 |
func (ko *Koanf) Int64(path string) int64 { |
| 11 |
if v := ko.Get(path); v != nil { |
| 12 |
i, _ := toInt64(v) |
| 13 |
return i |
| 14 |
} |
| 15 |
return 0 |
| 16 |
} |
| 17 |
|
| 18 |
// MustInt64 returns the int64 value of a given key path or panics |
| 19 |
// if the value is not set or set to default value of 0. |
| 20 |
func (ko *Koanf) MustInt64(path string) int64 { |
| 21 |
val := ko.Int64(path) |
| 22 |
if val == 0 { |
| 23 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 24 |
} |
| 25 |
return val |
| 26 |
} |
| 27 |
|
| 28 |
// Int64s returns the []int64 slice value of a given key path or an |
| 29 |
// empty []int64 slice if the path does not exist or if the value |
| 30 |
// is not a valid int slice. |
| 31 |
func (ko *Koanf) Int64s(path string) []int64 { |
| 32 |
o := ko.Get(path) |
| 33 |
if o == nil { |
| 34 |
return []int64{} |
| 35 |
} |
| 36 |
|
| 37 |
var out []int64 |
| 38 |
switch v := o.(type) { |
| 39 |
case []int64: |
| 40 |
return v |
| 41 |
case []int: |
| 42 |
out = make([]int64, 0, len(v)) |
| 43 |
for _, vi := range v { |
| 44 |
i, err := toInt64(vi) |
| 45 |
|
| 46 |
// On error, return as it's not a valid |
| 47 |
// int slice. |
| 48 |
if err != nil { |
| 49 |
return []int64{} |
| 50 |
} |
| 51 |
out = append(out, i) |
| 52 |
} |
| 53 |
return out |
| 54 |
case []any: |
| 55 |
out = make([]int64, 0, len(v)) |
| 56 |
for _, vi := range v { |
| 57 |
i, err := toInt64(vi) |
| 58 |
|
| 59 |
// On error, return as it's not a valid |
| 60 |
// int slice. |
| 61 |
if err != nil { |
| 62 |
return []int64{} |
| 63 |
} |
| 64 |
out = append(out, i) |
| 65 |
} |
| 66 |
return out |
| 67 |
} |
| 68 |
|
| 69 |
return []int64{} |
| 70 |
} |
| 71 |
|
| 72 |
// MustInt64s returns the []int64 slice value of a given key path or panics |
| 73 |
// if the value is not set or its default value. |
| 74 |
func (ko *Koanf) MustInt64s(path string) []int64 { |
| 75 |
val := ko.Int64s(path) |
| 76 |
if len(val) == 0 { |
| 77 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 78 |
} |
| 79 |
return val |
| 80 |
} |
| 81 |
|
| 82 |
// Int64Map returns the map[string]int64 value of a given key path |
| 83 |
// or an empty map[string]int64 if the path does not exist or if the |
| 84 |
// value is not a valid int64 map. |
| 85 |
func (ko *Koanf) Int64Map(path string) map[string]int64 { |
| 86 |
var ( |
| 87 |
out = map[string]int64{} |
| 88 |
o = ko.Get(path) |
| 89 |
) |
| 90 |
if o == nil { |
| 91 |
return out |
| 92 |
} |
| 93 |
|
| 94 |
mp, ok := o.(map[string]any) |
| 95 |
if !ok { |
| 96 |
return out |
| 97 |
} |
| 98 |
|
| 99 |
out = make(map[string]int64, len(mp)) |
| 100 |
for k, v := range mp { |
| 101 |
switch i := v.(type) { |
| 102 |
case int64: |
| 103 |
out[k] = i |
| 104 |
default: |
| 105 |
// Attempt a conversion. |
| 106 |
iv, err := toInt64(i) |
| 107 |
if err != nil { |
| 108 |
return map[string]int64{} |
| 109 |
} |
| 110 |
out[k] = iv |
| 111 |
} |
| 112 |
} |
| 113 |
return out |
| 114 |
} |
| 115 |
|
| 116 |
// MustInt64Map returns the map[string]int64 value of a given key path |
| 117 |
// or panics if it isn't set or set to default value. |
| 118 |
func (ko *Koanf) MustInt64Map(path string) map[string]int64 { |
| 119 |
val := ko.Int64Map(path) |
| 120 |
if len(val) == 0 { |
| 121 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 122 |
} |
| 123 |
return val |
| 124 |
} |
| 125 |
|
| 126 |
// Int returns the int value of a given key path or 0 if the path |
| 127 |
// does not exist or if the value is not a valid int. |
| 128 |
func (ko *Koanf) Int(path string) int { |
| 129 |
return int(ko.Int64(path)) |
| 130 |
} |
| 131 |
|
| 132 |
// MustInt returns the int value of a given key path or panics |
| 133 |
// if it isn't set or set to default value of 0. |
| 134 |
func (ko *Koanf) MustInt(path string) int { |
| 135 |
val := ko.Int(path) |
| 136 |
if val == 0 { |
| 137 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 138 |
} |
| 139 |
return val |
| 140 |
} |
| 141 |
|
| 142 |
// Ints returns the []int slice value of a given key path or an |
| 143 |
// empty []int slice if the path does not exist or if the value |
| 144 |
// is not a valid int slice. |
| 145 |
func (ko *Koanf) Ints(path string) []int { |
| 146 |
o := ko.Get(path) |
| 147 |
if o == nil { |
| 148 |
return []int{} |
| 149 |
} |
| 150 |
|
| 151 |
var out []int |
| 152 |
switch v := o.(type) { |
| 153 |
case []int: |
| 154 |
return v |
| 155 |
case []int64: |
| 156 |
out = make([]int, 0, len(v)) |
| 157 |
for _, vi := range v { |
| 158 |
out = append(out, int(vi)) |
| 159 |
} |
| 160 |
return out |
| 161 |
case []any: |
| 162 |
out = make([]int, 0, len(v)) |
| 163 |
for _, vi := range v { |
| 164 |
i, err := toInt64(vi) |
| 165 |
|
| 166 |
// On error, return as it's not a valid |
| 167 |
// int slice. |
| 168 |
if err != nil { |
| 169 |
return []int{} |
| 170 |
} |
| 171 |
out = append(out, int(i)) |
| 172 |
} |
| 173 |
return out |
| 174 |
} |
| 175 |
|
| 176 |
return []int{} |
| 177 |
} |
| 178 |
|
| 179 |
// MustInts returns the []int slice value of a given key path or panics |
| 180 |
// if the value is not set or set to default value. |
| 181 |
func (ko *Koanf) MustInts(path string) []int { |
| 182 |
val := ko.Ints(path) |
| 183 |
if len(val) == 0 { |
| 184 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 185 |
} |
| 186 |
return val |
| 187 |
} |
| 188 |
|
| 189 |
// IntMap returns the map[string]int value of a given key path |
| 190 |
// or an empty map[string]int if the path does not exist or if the |
| 191 |
// value is not a valid int map. |
| 192 |
func (ko *Koanf) IntMap(path string) map[string]int { |
| 193 |
var ( |
| 194 |
mp = ko.Int64Map(path) |
| 195 |
out = make(map[string]int, len(mp)) |
| 196 |
) |
| 197 |
for k, v := range mp { |
| 198 |
out[k] = int(v) |
| 199 |
} |
| 200 |
return out |
| 201 |
} |
| 202 |
|
| 203 |
// MustIntMap returns the map[string]int value of a given key path or panics |
| 204 |
// if the value is not set or set to default value. |
| 205 |
func (ko *Koanf) MustIntMap(path string) map[string]int { |
| 206 |
val := ko.IntMap(path) |
| 207 |
if len(val) == 0 { |
| 208 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 209 |
} |
| 210 |
return val |
| 211 |
} |
| 212 |
|
| 213 |
// Float64 returns the float64 value of a given key path or 0 if the path |
| 214 |
// does not exist or if the value is not a valid float64. |
| 215 |
func (ko *Koanf) Float64(path string) float64 { |
| 216 |
if v := ko.Get(path); v != nil { |
| 217 |
f, _ := toFloat64(v) |
| 218 |
return f |
| 219 |
} |
| 220 |
return 0 |
| 221 |
} |
| 222 |
|
| 223 |
// MustFloat64 returns the float64 value of a given key path or panics |
| 224 |
// if it isn't set or set to default value 0. |
| 225 |
func (ko *Koanf) MustFloat64(path string) float64 { |
| 226 |
val := ko.Float64(path) |
| 227 |
if val == 0 { |
| 228 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 229 |
} |
| 230 |
return val |
| 231 |
} |
| 232 |
|
| 233 |
// Float64s returns the []float64 slice value of a given key path or an |
| 234 |
// empty []float64 slice if the path does not exist or if the value |
| 235 |
// is not a valid float64 slice. |
| 236 |
func (ko *Koanf) Float64s(path string) []float64 { |
| 237 |
o := ko.Get(path) |
| 238 |
if o == nil { |
| 239 |
return []float64{} |
| 240 |
} |
| 241 |
|
| 242 |
var out []float64 |
| 243 |
switch v := o.(type) { |
| 244 |
case []float64: |
| 245 |
return v |
| 246 |
case []any: |
| 247 |
out = make([]float64, 0, len(v)) |
| 248 |
for _, vi := range v { |
| 249 |
i, err := toFloat64(vi) |
| 250 |
|
| 251 |
// On error, return as it's not a valid |
| 252 |
// int slice. |
| 253 |
if err != nil { |
| 254 |
return []float64{} |
| 255 |
} |
| 256 |
out = append(out, i) |
| 257 |
} |
| 258 |
return out |
| 259 |
} |
| 260 |
|
| 261 |
return []float64{} |
| 262 |
} |
| 263 |
|
| 264 |
// MustFloat64s returns the []Float64 slice value of a given key path or panics |
| 265 |
// if the value is not set or set to default value. |
| 266 |
func (ko *Koanf) MustFloat64s(path string) []float64 { |
| 267 |
val := ko.Float64s(path) |
| 268 |
if len(val) == 0 { |
| 269 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 270 |
} |
| 271 |
return val |
| 272 |
} |
| 273 |
|
| 274 |
// Float64Map returns the map[string]float64 value of a given key path |
| 275 |
// or an empty map[string]float64 if the path does not exist or if the |
| 276 |
// value is not a valid float64 map. |
| 277 |
func (ko *Koanf) Float64Map(path string) map[string]float64 { |
| 278 |
var ( |
| 279 |
out = map[string]float64{} |
| 280 |
o = ko.Get(path) |
| 281 |
) |
| 282 |
if o == nil { |
| 283 |
return out |
| 284 |
} |
| 285 |
|
| 286 |
mp, ok := o.(map[string]any) |
| 287 |
if !ok { |
| 288 |
return out |
| 289 |
} |
| 290 |
|
| 291 |
out = make(map[string]float64, len(mp)) |
| 292 |
for k, v := range mp { |
| 293 |
switch i := v.(type) { |
| 294 |
case float64: |
| 295 |
out[k] = i |
| 296 |
default: |
| 297 |
// Attempt a conversion. |
| 298 |
iv, err := toFloat64(i) |
| 299 |
if err != nil { |
| 300 |
return map[string]float64{} |
| 301 |
} |
| 302 |
out[k] = iv |
| 303 |
} |
| 304 |
} |
| 305 |
return out |
| 306 |
} |
| 307 |
|
| 308 |
// MustFloat64Map returns the map[string]float64 value of a given key path or panics |
| 309 |
// if the value is not set or set to default value. |
| 310 |
func (ko *Koanf) MustFloat64Map(path string) map[string]float64 { |
| 311 |
val := ko.Float64Map(path) |
| 312 |
if len(val) == 0 { |
| 313 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 314 |
} |
| 315 |
return val |
| 316 |
} |
| 317 |
|
| 318 |
// Duration returns the time.Duration value of a given key path assuming |
| 319 |
// that the key contains a valid numeric value. |
| 320 |
func (ko *Koanf) Duration(path string) time.Duration { |
| 321 |
// Look for a parsable string representation first. |
| 322 |
if v := ko.Int64(path); v != 0 { |
| 323 |
return time.Duration(v) |
| 324 |
} |
| 325 |
|
| 326 |
v, _ := time.ParseDuration(ko.String(path)) |
| 327 |
return v |
| 328 |
} |
| 329 |
|
| 330 |
// MustDuration returns the time.Duration value of a given key path or panics |
| 331 |
// if it isn't set or set to default value 0. |
| 332 |
func (ko *Koanf) MustDuration(path string) time.Duration { |
| 333 |
val := ko.Duration(path) |
| 334 |
if val == 0 { |
| 335 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 336 |
} |
| 337 |
return val |
| 338 |
} |
| 339 |
|
| 340 |
// Time attempts to parse the value of a given key path and return time.Time |
| 341 |
// representation. If the value is numeric, it is treated as a UNIX timestamp |
| 342 |
// and if it's string, a parse is attempted with the given layout. |
| 343 |
func (ko *Koanf) Time(path, layout string) time.Time { |
| 344 |
// Unix timestamp? |
| 345 |
v := ko.Int64(path) |
| 346 |
if v != 0 { |
| 347 |
return time.Unix(v, 0) |
| 348 |
} |
| 349 |
|
| 350 |
// String representation. |
| 351 |
s := ko.String(path) |
| 352 |
if s != "" { |
| 353 |
t, _ := time.Parse(layout, s) |
| 354 |
return t |
| 355 |
} |
| 356 |
|
| 357 |
return time.Time{} |
| 358 |
} |
| 359 |
|
| 360 |
// MustTime attempts to parse the value of a given key path and return time.Time |
| 361 |
// representation. If the value is numeric, it is treated as a UNIX timestamp |
| 362 |
// and if it's string, a parse is attempted with the given layout. It panics if |
| 363 |
// the parsed time is zero. |
| 364 |
func (ko *Koanf) MustTime(path, layout string) time.Time { |
| 365 |
val := ko.Time(path, layout) |
| 366 |
if val.IsZero() { |
| 367 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 368 |
} |
| 369 |
return val |
| 370 |
} |
| 371 |
|
| 372 |
// String returns the string value of a given key path or "" if the path |
| 373 |
// does not exist or if the value is not a valid string. |
| 374 |
func (ko *Koanf) String(path string) string { |
| 375 |
if v := ko.Get(path); v != nil { |
| 376 |
if i, ok := v.(string); ok { |
| 377 |
return i |
| 378 |
} |
| 379 |
return fmt.Sprintf("%v", v) |
| 380 |
} |
| 381 |
return "" |
| 382 |
} |
| 383 |
|
| 384 |
// MustString returns the string value of a given key path |
| 385 |
// or panics if it isn't set or set to default value "". |
| 386 |
func (ko *Koanf) MustString(path string) string { |
| 387 |
val := ko.String(path) |
| 388 |
if val == "" { |
| 389 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 390 |
} |
| 391 |
return val |
| 392 |
} |
| 393 |
|
| 394 |
// Strings returns the []string slice value of a given key path or an |
| 395 |
// empty []string slice if the path does not exist or if the value |
| 396 |
// is not a valid string slice. |
| 397 |
func (ko *Koanf) Strings(path string) []string { |
| 398 |
o := ko.Get(path) |
| 399 |
if o == nil { |
| 400 |
return []string{} |
| 401 |
} |
| 402 |
|
| 403 |
var out []string |
| 404 |
switch v := o.(type) { |
| 405 |
case []any: |
| 406 |
out = make([]string, 0, len(v)) |
| 407 |
for _, u := range v { |
| 408 |
if s, ok := u.(string); ok { |
| 409 |
out = append(out, s) |
| 410 |
} else { |
| 411 |
out = append(out, fmt.Sprintf("%v", u)) |
| 412 |
} |
| 413 |
} |
| 414 |
return out |
| 415 |
case []string: |
| 416 |
out := make([]string, len(v)) |
| 417 |
copy(out, v) |
| 418 |
return out |
| 419 |
} |
| 420 |
|
| 421 |
return []string{} |
| 422 |
} |
| 423 |
|
| 424 |
// MustStrings returns the []string slice value of a given key path or panics |
| 425 |
// if the value is not set or set to default value. |
| 426 |
func (ko *Koanf) MustStrings(path string) []string { |
| 427 |
val := ko.Strings(path) |
| 428 |
if len(val) == 0 { |
| 429 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 430 |
} |
| 431 |
return val |
| 432 |
} |
| 433 |
|
| 434 |
// StringMap returns the map[string]string value of a given key path |
| 435 |
// or an empty map[string]string if the path does not exist or if the |
| 436 |
// value is not a valid string map. |
| 437 |
func (ko *Koanf) StringMap(path string) map[string]string { |
| 438 |
var ( |
| 439 |
out = map[string]string{} |
| 440 |
o = ko.Get(path) |
| 441 |
) |
| 442 |
if o == nil { |
| 443 |
return out |
| 444 |
} |
| 445 |
|
| 446 |
switch mp := o.(type) { |
| 447 |
case map[string]string: |
| 448 |
out = make(map[string]string, len(mp)) |
| 449 |
for k, v := range mp { |
| 450 |
out[k] = v |
| 451 |
} |
| 452 |
case map[string]any: |
| 453 |
out = make(map[string]string, len(mp)) |
| 454 |
for k, v := range mp { |
| 455 |
switch s := v.(type) { |
| 456 |
case string: |
| 457 |
out[k] = s |
| 458 |
default: |
| 459 |
// There's a non string type. Return. |
| 460 |
return map[string]string{} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
return out |
| 466 |
} |
| 467 |
|
| 468 |
// MustStringMap returns the map[string]string value of a given key path or panics |
| 469 |
// if the value is not set or set to default value. |
| 470 |
func (ko *Koanf) MustStringMap(path string) map[string]string { |
| 471 |
val := ko.StringMap(path) |
| 472 |
if len(val) == 0 { |
| 473 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 474 |
} |
| 475 |
return val |
| 476 |
} |
| 477 |
|
| 478 |
// StringsMap returns the map[string][]string value of a given key path |
| 479 |
// or an empty map[string][]string if the path does not exist or if the |
| 480 |
// value is not a valid strings map. |
| 481 |
func (ko *Koanf) StringsMap(path string) map[string][]string { |
| 482 |
var ( |
| 483 |
out = map[string][]string{} |
| 484 |
o = ko.Get(path) |
| 485 |
) |
| 486 |
if o == nil { |
| 487 |
return out |
| 488 |
} |
| 489 |
|
| 490 |
switch mp := o.(type) { |
| 491 |
case map[string][]string: |
| 492 |
out = make(map[string][]string, len(mp)) |
| 493 |
for k, v := range mp { |
| 494 |
out[k] = append(out[k], v...) |
| 495 |
} |
| 496 |
case map[string][]any: |
| 497 |
out = make(map[string][]string, len(mp)) |
| 498 |
for k, v := range mp { |
| 499 |
for _, v := range v { |
| 500 |
switch sv := v.(type) { |
| 501 |
case string: |
| 502 |
out[k] = append(out[k], sv) |
| 503 |
default: |
| 504 |
return map[string][]string{} |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
case map[string]any: |
| 509 |
out = make(map[string][]string, len(mp)) |
| 510 |
for k, v := range mp { |
| 511 |
switch s := v.(type) { |
| 512 |
case []string: |
| 513 |
out[k] = append(out[k], s...) |
| 514 |
case []any: |
| 515 |
for _, v := range s { |
| 516 |
switch sv := v.(type) { |
| 517 |
case string: |
| 518 |
out[k] = append(out[k], sv) |
| 519 |
default: |
| 520 |
return map[string][]string{} |
| 521 |
} |
| 522 |
} |
| 523 |
default: |
| 524 |
// There's a non []interface type. Return. |
| 525 |
return map[string][]string{} |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
return out |
| 531 |
} |
| 532 |
|
| 533 |
// MustStringsMap returns the map[string][]string value of a given key path or panics |
| 534 |
// if the value is not set or set to default value. |
| 535 |
func (ko *Koanf) MustStringsMap(path string) map[string][]string { |
| 536 |
val := ko.StringsMap(path) |
| 537 |
if len(val) == 0 { |
| 538 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 539 |
} |
| 540 |
return val |
| 541 |
} |
| 542 |
|
| 543 |
// Bytes returns the []byte value of a given key path or an empty |
| 544 |
// []byte slice if the path does not exist or if the value is not a valid string. |
| 545 |
func (ko *Koanf) Bytes(path string) []byte { |
| 546 |
return []byte(ko.String(path)) |
| 547 |
} |
| 548 |
|
| 549 |
// MustBytes returns the []byte value of a given key path or panics |
| 550 |
// if the value is not set or set to default value. |
| 551 |
func (ko *Koanf) MustBytes(path string) []byte { |
| 552 |
val := ko.Bytes(path) |
| 553 |
if len(val) == 0 { |
| 554 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 555 |
} |
| 556 |
return val |
| 557 |
} |
| 558 |
|
| 559 |
// Bool returns the bool value of a given key path or false if the path |
| 560 |
// does not exist or if the value is not a valid bool representation. |
| 561 |
// Accepted string representations of bool are the ones supported by strconv.ParseBool. |
| 562 |
func (ko *Koanf) Bool(path string) bool { |
| 563 |
if v := ko.Get(path); v != nil { |
| 564 |
b, _ := toBool(v) |
| 565 |
return b |
| 566 |
} |
| 567 |
return false |
| 568 |
} |
| 569 |
|
| 570 |
// Bools returns the []bool slice value of a given key path or an |
| 571 |
// empty []bool slice if the path does not exist or if the value |
| 572 |
// is not a valid bool slice. |
| 573 |
func (ko *Koanf) Bools(path string) []bool { |
| 574 |
o := ko.Get(path) |
| 575 |
if o == nil { |
| 576 |
return []bool{} |
| 577 |
} |
| 578 |
|
| 579 |
var out []bool |
| 580 |
switch v := o.(type) { |
| 581 |
case []any: |
| 582 |
out = make([]bool, 0, len(v)) |
| 583 |
for _, u := range v { |
| 584 |
b, err := toBool(u) |
| 585 |
if err != nil { |
| 586 |
return nil |
| 587 |
} |
| 588 |
out = append(out, b) |
| 589 |
} |
| 590 |
return out |
| 591 |
case []bool: |
| 592 |
return out |
| 593 |
} |
| 594 |
return nil |
| 595 |
} |
| 596 |
|
| 597 |
// MustBools returns the []bool value of a given key path or panics |
| 598 |
// if the value is not set or set to default value. |
| 599 |
func (ko *Koanf) MustBools(path string) []bool { |
| 600 |
val := ko.Bools(path) |
| 601 |
if len(val) == 0 { |
| 602 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 603 |
} |
| 604 |
return val |
| 605 |
} |
| 606 |
|
| 607 |
// BoolMap returns the map[string]bool value of a given key path |
| 608 |
// or an empty map[string]bool if the path does not exist or if the |
| 609 |
// value is not a valid bool map. |
| 610 |
func (ko *Koanf) BoolMap(path string) map[string]bool { |
| 611 |
var ( |
| 612 |
out = map[string]bool{} |
| 613 |
o = ko.Get(path) |
| 614 |
) |
| 615 |
if o == nil { |
| 616 |
return out |
| 617 |
} |
| 618 |
|
| 619 |
mp, ok := o.(map[string]any) |
| 620 |
if !ok { |
| 621 |
return out |
| 622 |
} |
| 623 |
out = make(map[string]bool, len(mp)) |
| 624 |
for k, v := range mp { |
| 625 |
switch i := v.(type) { |
| 626 |
case bool: |
| 627 |
out[k] = i |
| 628 |
default: |
| 629 |
// Attempt a conversion. |
| 630 |
b, err := toBool(i) |
| 631 |
if err != nil { |
| 632 |
return map[string]bool{} |
| 633 |
} |
| 634 |
out[k] = b |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
return out |
| 639 |
} |
| 640 |
|
| 641 |
// MustBoolMap returns the map[string]bool value of a given key path or panics |
| 642 |
// if the value is not set or set to default value. |
| 643 |
func (ko *Koanf) MustBoolMap(path string) map[string]bool { |
| 644 |
val := ko.BoolMap(path) |
| 645 |
if len(val) == 0 { |
| 646 |
panic(fmt.Sprintf("invalid value: %s=%v", path, val)) |
| 647 |
} |
| 648 |
return val |
| 649 |
} |