diff options
Diffstat (limited to 'vendor/github.com/go-ini/ini/key.go')
-rw-r--r-- | vendor/github.com/go-ini/ini/key.go | 104 |
1 files changed, 87 insertions, 17 deletions
diff --git a/vendor/github.com/go-ini/ini/key.go b/vendor/github.com/go-ini/ini/key.go index 9738c55..852696f 100644 --- a/vendor/github.com/go-ini/ini/key.go +++ b/vendor/github.com/go-ini/ini/key.go | |||
@@ -15,6 +15,7 @@ | |||
15 | package ini | 15 | package ini |
16 | 16 | ||
17 | import ( | 17 | import ( |
18 | "errors" | ||
18 | "fmt" | 19 | "fmt" |
19 | "strconv" | 20 | "strconv" |
20 | "strings" | 21 | "strings" |
@@ -29,9 +30,42 @@ type Key struct { | |||
29 | isAutoIncrement bool | 30 | isAutoIncrement bool |
30 | isBooleanType bool | 31 | isBooleanType bool |
31 | 32 | ||
33 | isShadow bool | ||
34 | shadows []*Key | ||
35 | |||
32 | Comment string | 36 | Comment string |
33 | } | 37 | } |
34 | 38 | ||
39 | // newKey simply return a key object with given values. | ||
40 | func newKey(s *Section, name, val string) *Key { | ||
41 | return &Key{ | ||
42 | s: s, | ||
43 | name: name, | ||
44 | value: val, | ||
45 | } | ||
46 | } | ||
47 | |||
48 | func (k *Key) addShadow(val string) error { | ||
49 | if k.isShadow { | ||
50 | return errors.New("cannot add shadow to another shadow key") | ||
51 | } else if k.isAutoIncrement || k.isBooleanType { | ||
52 | return errors.New("cannot add shadow to auto-increment or boolean key") | ||
53 | } | ||
54 | |||
55 | shadow := newKey(k.s, k.name, val) | ||
56 | shadow.isShadow = true | ||
57 | k.shadows = append(k.shadows, shadow) | ||
58 | return nil | ||
59 | } | ||
60 | |||
61 | // AddShadow adds a new shadow key to itself. | ||
62 | func (k *Key) AddShadow(val string) error { | ||
63 | if !k.s.f.options.AllowShadows { | ||
64 | return errors.New("shadow key is not allowed") | ||
65 | } | ||
66 | return k.addShadow(val) | ||
67 | } | ||
68 | |||
35 | // ValueMapper represents a mapping function for values, e.g. os.ExpandEnv | 69 | // ValueMapper represents a mapping function for values, e.g. os.ExpandEnv |
36 | type ValueMapper func(string) string | 70 | type ValueMapper func(string) string |
37 | 71 | ||
@@ -45,16 +79,29 @@ func (k *Key) Value() string { | |||
45 | return k.value | 79 | return k.value |
46 | } | 80 | } |
47 | 81 | ||
48 | // String returns string representation of value. | 82 | // ValueWithShadows returns raw values of key and its shadows if any. |
49 | func (k *Key) String() string { | 83 | func (k *Key) ValueWithShadows() []string { |
50 | val := k.value | 84 | if len(k.shadows) == 0 { |
85 | return []string{k.value} | ||
86 | } | ||
87 | vals := make([]string, len(k.shadows)+1) | ||
88 | vals[0] = k.value | ||
89 | for i := range k.shadows { | ||
90 | vals[i+1] = k.shadows[i].value | ||
91 | } | ||
92 | return vals | ||
93 | } | ||
94 | |||
95 | // transformValue takes a raw value and transforms to its final string. | ||
96 | func (k *Key) transformValue(val string) string { | ||
51 | if k.s.f.ValueMapper != nil { | 97 | if k.s.f.ValueMapper != nil { |
52 | val = k.s.f.ValueMapper(val) | 98 | val = k.s.f.ValueMapper(val) |
53 | } | 99 | } |
54 | if strings.Index(val, "%") == -1 { | 100 | |
101 | // Fail-fast if no indicate char found for recursive value | ||
102 | if !strings.Contains(val, "%") { | ||
55 | return val | 103 | return val |
56 | } | 104 | } |
57 | |||
58 | for i := 0; i < _DEPTH_VALUES; i++ { | 105 | for i := 0; i < _DEPTH_VALUES; i++ { |
59 | vr := varPattern.FindString(val) | 106 | vr := varPattern.FindString(val) |
60 | if len(vr) == 0 { | 107 | if len(vr) == 0 { |
@@ -78,6 +125,11 @@ func (k *Key) String() string { | |||
78 | return val | 125 | return val |
79 | } | 126 | } |
80 | 127 | ||
128 | // String returns string representation of value. | ||
129 | func (k *Key) String() string { | ||
130 | return k.transformValue(k.value) | ||
131 | } | ||
132 | |||
81 | // Validate accepts a validate function which can | 133 | // Validate accepts a validate function which can |
82 | // return modifed result as key value. | 134 | // return modifed result as key value. |
83 | func (k *Key) Validate(fn func(string) string) string { | 135 | func (k *Key) Validate(fn func(string) string) string { |
@@ -394,11 +446,31 @@ func (k *Key) Strings(delim string) []string { | |||
394 | 446 | ||
395 | vals := strings.Split(str, delim) | 447 | vals := strings.Split(str, delim) |
396 | for i := range vals { | 448 | for i := range vals { |
449 | // vals[i] = k.transformValue(strings.TrimSpace(vals[i])) | ||
397 | vals[i] = strings.TrimSpace(vals[i]) | 450 | vals[i] = strings.TrimSpace(vals[i]) |
398 | } | 451 | } |
399 | return vals | 452 | return vals |
400 | } | 453 | } |
401 | 454 | ||
455 | // StringsWithShadows returns list of string divided by given delimiter. | ||
456 | // Shadows will also be appended if any. | ||
457 | func (k *Key) StringsWithShadows(delim string) []string { | ||
458 | vals := k.ValueWithShadows() | ||
459 | results := make([]string, 0, len(vals)*2) | ||
460 | for i := range vals { | ||
461 | if len(vals) == 0 { | ||
462 | continue | ||
463 | } | ||
464 | |||
465 | results = append(results, strings.Split(vals[i], delim)...) | ||
466 | } | ||
467 | |||
468 | for i := range results { | ||
469 | results[i] = k.transformValue(strings.TrimSpace(results[i])) | ||
470 | } | ||
471 | return results | ||
472 | } | ||
473 | |||
402 | // Float64s returns list of float64 divided by given delimiter. Any invalid input will be treated as zero value. | 474 | // Float64s returns list of float64 divided by given delimiter. Any invalid input will be treated as zero value. |
403 | func (k *Key) Float64s(delim string) []float64 { | 475 | func (k *Key) Float64s(delim string) []float64 { |
404 | vals, _ := k.getFloat64s(delim, true, false) | 476 | vals, _ := k.getFloat64s(delim, true, false) |
@@ -407,13 +479,13 @@ func (k *Key) Float64s(delim string) []float64 { | |||
407 | 479 | ||
408 | // Ints returns list of int divided by given delimiter. Any invalid input will be treated as zero value. | 480 | // Ints returns list of int divided by given delimiter. Any invalid input will be treated as zero value. |
409 | func (k *Key) Ints(delim string) []int { | 481 | func (k *Key) Ints(delim string) []int { |
410 | vals, _ := k.getInts(delim, true, false) | 482 | vals, _ := k.parseInts(k.Strings(delim), true, false) |
411 | return vals | 483 | return vals |
412 | } | 484 | } |
413 | 485 | ||
414 | // Int64s returns list of int64 divided by given delimiter. Any invalid input will be treated as zero value. | 486 | // Int64s returns list of int64 divided by given delimiter. Any invalid input will be treated as zero value. |
415 | func (k *Key) Int64s(delim string) []int64 { | 487 | func (k *Key) Int64s(delim string) []int64 { |
416 | vals, _ := k.getInt64s(delim, true, false) | 488 | vals, _ := k.parseInt64s(k.Strings(delim), true, false) |
417 | return vals | 489 | return vals |
418 | } | 490 | } |
419 | 491 | ||
@@ -452,14 +524,14 @@ func (k *Key) ValidFloat64s(delim string) []float64 { | |||
452 | // ValidInts returns list of int divided by given delimiter. If some value is not integer, then it will | 524 | // ValidInts returns list of int divided by given delimiter. If some value is not integer, then it will |
453 | // not be included to result list. | 525 | // not be included to result list. |
454 | func (k *Key) ValidInts(delim string) []int { | 526 | func (k *Key) ValidInts(delim string) []int { |
455 | vals, _ := k.getInts(delim, false, false) | 527 | vals, _ := k.parseInts(k.Strings(delim), false, false) |
456 | return vals | 528 | return vals |
457 | } | 529 | } |
458 | 530 | ||
459 | // ValidInt64s returns list of int64 divided by given delimiter. If some value is not 64-bit integer, | 531 | // ValidInt64s returns list of int64 divided by given delimiter. If some value is not 64-bit integer, |
460 | // then it will not be included to result list. | 532 | // then it will not be included to result list. |
461 | func (k *Key) ValidInt64s(delim string) []int64 { | 533 | func (k *Key) ValidInt64s(delim string) []int64 { |
462 | vals, _ := k.getInt64s(delim, false, false) | 534 | vals, _ := k.parseInt64s(k.Strings(delim), false, false) |
463 | return vals | 535 | return vals |
464 | } | 536 | } |
465 | 537 | ||
@@ -495,12 +567,12 @@ func (k *Key) StrictFloat64s(delim string) ([]float64, error) { | |||
495 | 567 | ||
496 | // StrictInts returns list of int divided by given delimiter or error on first invalid input. | 568 | // StrictInts returns list of int divided by given delimiter or error on first invalid input. |
497 | func (k *Key) StrictInts(delim string) ([]int, error) { | 569 | func (k *Key) StrictInts(delim string) ([]int, error) { |
498 | return k.getInts(delim, false, true) | 570 | return k.parseInts(k.Strings(delim), false, true) |
499 | } | 571 | } |
500 | 572 | ||
501 | // StrictInt64s returns list of int64 divided by given delimiter or error on first invalid input. | 573 | // StrictInt64s returns list of int64 divided by given delimiter or error on first invalid input. |
502 | func (k *Key) StrictInt64s(delim string) ([]int64, error) { | 574 | func (k *Key) StrictInt64s(delim string) ([]int64, error) { |
503 | return k.getInt64s(delim, false, true) | 575 | return k.parseInt64s(k.Strings(delim), false, true) |
504 | } | 576 | } |
505 | 577 | ||
506 | // StrictUints returns list of uint divided by given delimiter or error on first invalid input. | 578 | // StrictUints returns list of uint divided by given delimiter or error on first invalid input. |
@@ -541,9 +613,8 @@ func (k *Key) getFloat64s(delim string, addInvalid, returnOnInvalid bool) ([]flo | |||
541 | return vals, nil | 613 | return vals, nil |
542 | } | 614 | } |
543 | 615 | ||
544 | // getInts returns list of int divided by given delimiter. | 616 | // parseInts transforms strings to ints. |
545 | func (k *Key) getInts(delim string, addInvalid, returnOnInvalid bool) ([]int, error) { | 617 | func (k *Key) parseInts(strs []string, addInvalid, returnOnInvalid bool) ([]int, error) { |
546 | strs := k.Strings(delim) | ||
547 | vals := make([]int, 0, len(strs)) | 618 | vals := make([]int, 0, len(strs)) |
548 | for _, str := range strs { | 619 | for _, str := range strs { |
549 | val, err := strconv.Atoi(str) | 620 | val, err := strconv.Atoi(str) |
@@ -557,9 +628,8 @@ func (k *Key) getInts(delim string, addInvalid, returnOnInvalid bool) ([]int, er | |||
557 | return vals, nil | 628 | return vals, nil |
558 | } | 629 | } |
559 | 630 | ||
560 | // getInt64s returns list of int64 divided by given delimiter. | 631 | // parseInt64s transforms strings to int64s. |
561 | func (k *Key) getInt64s(delim string, addInvalid, returnOnInvalid bool) ([]int64, error) { | 632 | func (k *Key) parseInt64s(strs []string, addInvalid, returnOnInvalid bool) ([]int64, error) { |
562 | strs := k.Strings(delim) | ||
563 | vals := make([]int64, 0, len(strs)) | 633 | vals := make([]int64, 0, len(strs)) |
564 | for _, str := range strs { | 634 | for _, str := range strs { |
565 | val, err := strconv.ParseInt(str, 10, 64) | 635 | val, err := strconv.ParseInt(str, 10, 64) |