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