aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/go-ini/ini/key.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/go-ini/ini/key.go')
-rw-r--r--vendor/github.com/go-ini/ini/key.go703
1 files changed, 0 insertions, 703 deletions
diff --git a/vendor/github.com/go-ini/ini/key.go b/vendor/github.com/go-ini/ini/key.go
deleted file mode 100644
index 852696f..0000000
--- a/vendor/github.com/go-ini/ini/key.go
+++ /dev/null
@@ -1,703 +0,0 @@
1// Copyright 2014 Unknwon
2//
3// Licensed under the Apache License, Version 2.0 (the "License"): you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations
13// under the License.
14
15package ini
16
17import (
18 "errors"
19 "fmt"
20 "strconv"
21 "strings"
22 "time"
23)
24
25// Key represents a key under a section.
26type Key struct {
27 s *Section
28 name string
29 value string
30 isAutoIncrement bool
31 isBooleanType bool
32
33 isShadow bool
34 shadows []*Key
35
36 Comment string
37}
38
39// newKey simply return a key object with given values.
40func newKey(s *Section, name, val string) *Key {
41 return &Key{
42 s: s,
43 name: name,
44 value: val,
45 }
46}
47
48func (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.
62func (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
69// ValueMapper represents a mapping function for values, e.g. os.ExpandEnv
70type ValueMapper func(string) string
71
72// Name returns name of key.
73func (k *Key) Name() string {
74 return k.name
75}
76
77// Value returns raw value of key for performance purpose.
78func (k *Key) Value() string {
79 return k.value
80}
81
82// ValueWithShadows returns raw values of key and its shadows if any.
83func (k *Key) ValueWithShadows() []string {
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.
96func (k *Key) transformValue(val string) string {
97 if k.s.f.ValueMapper != nil {
98 val = k.s.f.ValueMapper(val)
99 }
100
101 // Fail-fast if no indicate char found for recursive value
102 if !strings.Contains(val, "%") {
103 return val
104 }
105 for i := 0; i < _DEPTH_VALUES; i++ {
106 vr := varPattern.FindString(val)
107 if len(vr) == 0 {
108 break
109 }
110
111 // Take off leading '%(' and trailing ')s'.
112 noption := strings.TrimLeft(vr, "%(")
113 noption = strings.TrimRight(noption, ")s")
114
115 // Search in the same section.
116 nk, err := k.s.GetKey(noption)
117 if err != nil {
118 // Search again in default section.
119 nk, _ = k.s.f.Section("").GetKey(noption)
120 }
121
122 // Substitute by new value and take off leading '%(' and trailing ')s'.
123 val = strings.Replace(val, vr, nk.value, -1)
124 }
125 return val
126}
127
128// String returns string representation of value.
129func (k *Key) String() string {
130 return k.transformValue(k.value)
131}
132
133// Validate accepts a validate function which can
134// return modifed result as key value.
135func (k *Key) Validate(fn func(string) string) string {
136 return fn(k.String())
137}
138
139// parseBool returns the boolean value represented by the string.
140//
141// It accepts 1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On,
142// 0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Off.
143// Any other value returns an error.
144func parseBool(str string) (value bool, err error) {
145 switch str {
146 case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "y", "ON", "on", "On":
147 return true, nil
148 case "0", "f", "F", "false", "FALSE", "False", "NO", "no", "No", "n", "OFF", "off", "Off":
149 return false, nil
150 }
151 return false, fmt.Errorf("parsing \"%s\": invalid syntax", str)
152}
153
154// Bool returns bool type value.
155func (k *Key) Bool() (bool, error) {
156 return parseBool(k.String())
157}
158
159// Float64 returns float64 type value.
160func (k *Key) Float64() (float64, error) {
161 return strconv.ParseFloat(k.String(), 64)
162}
163
164// Int returns int type value.
165func (k *Key) Int() (int, error) {
166 return strconv.Atoi(k.String())
167}
168
169// Int64 returns int64 type value.
170func (k *Key) Int64() (int64, error) {
171 return strconv.ParseInt(k.String(), 10, 64)
172}
173
174// Uint returns uint type valued.
175func (k *Key) Uint() (uint, error) {
176 u, e := strconv.ParseUint(k.String(), 10, 64)
177 return uint(u), e
178}
179
180// Uint64 returns uint64 type value.
181func (k *Key) Uint64() (uint64, error) {
182 return strconv.ParseUint(k.String(), 10, 64)
183}
184
185// Duration returns time.Duration type value.
186func (k *Key) Duration() (time.Duration, error) {
187 return time.ParseDuration(k.String())
188}
189
190// TimeFormat parses with given format and returns time.Time type value.
191func (k *Key) TimeFormat(format string) (time.Time, error) {
192 return time.Parse(format, k.String())
193}
194
195// Time parses with RFC3339 format and returns time.Time type value.
196func (k *Key) Time() (time.Time, error) {
197 return k.TimeFormat(time.RFC3339)
198}
199
200// MustString returns default value if key value is empty.
201func (k *Key) MustString(defaultVal string) string {
202 val := k.String()
203 if len(val) == 0 {
204 k.value = defaultVal
205 return defaultVal
206 }
207 return val
208}
209
210// MustBool always returns value without error,
211// it returns false if error occurs.
212func (k *Key) MustBool(defaultVal ...bool) bool {
213 val, err := k.Bool()
214 if len(defaultVal) > 0 && err != nil {
215 k.value = strconv.FormatBool(defaultVal[0])
216 return defaultVal[0]
217 }
218 return val
219}
220
221// MustFloat64 always returns value without error,
222// it returns 0.0 if error occurs.
223func (k *Key) MustFloat64(defaultVal ...float64) float64 {
224 val, err := k.Float64()
225 if len(defaultVal) > 0 && err != nil {
226 k.value = strconv.FormatFloat(defaultVal[0], 'f', -1, 64)
227 return defaultVal[0]
228 }
229 return val
230}
231
232// MustInt always returns value without error,
233// it returns 0 if error occurs.
234func (k *Key) MustInt(defaultVal ...int) int {
235 val, err := k.Int()
236 if len(defaultVal) > 0 && err != nil {
237 k.value = strconv.FormatInt(int64(defaultVal[0]), 10)
238 return defaultVal[0]
239 }
240 return val
241}
242
243// MustInt64 always returns value without error,
244// it returns 0 if error occurs.
245func (k *Key) MustInt64(defaultVal ...int64) int64 {
246 val, err := k.Int64()
247 if len(defaultVal) > 0 && err != nil {
248 k.value = strconv.FormatInt(defaultVal[0], 10)
249 return defaultVal[0]
250 }
251 return val
252}
253
254// MustUint always returns value without error,
255// it returns 0 if error occurs.
256func (k *Key) MustUint(defaultVal ...uint) uint {
257 val, err := k.Uint()
258 if len(defaultVal) > 0 && err != nil {
259 k.value = strconv.FormatUint(uint64(defaultVal[0]), 10)
260 return defaultVal[0]
261 }
262 return val
263}
264
265// MustUint64 always returns value without error,
266// it returns 0 if error occurs.
267func (k *Key) MustUint64(defaultVal ...uint64) uint64 {
268 val, err := k.Uint64()
269 if len(defaultVal) > 0 && err != nil {
270 k.value = strconv.FormatUint(defaultVal[0], 10)
271 return defaultVal[0]
272 }
273 return val
274}
275
276// MustDuration always returns value without error,
277// it returns zero value if error occurs.
278func (k *Key) MustDuration(defaultVal ...time.Duration) time.Duration {
279 val, err := k.Duration()
280 if len(defaultVal) > 0 && err != nil {
281 k.value = defaultVal[0].String()
282 return defaultVal[0]
283 }
284 return val
285}
286
287// MustTimeFormat always parses with given format and returns value without error,
288// it returns zero value if error occurs.
289func (k *Key) MustTimeFormat(format string, defaultVal ...time.Time) time.Time {
290 val, err := k.TimeFormat(format)
291 if len(defaultVal) > 0 && err != nil {
292 k.value = defaultVal[0].Format(format)
293 return defaultVal[0]
294 }
295 return val
296}
297
298// MustTime always parses with RFC3339 format and returns value without error,
299// it returns zero value if error occurs.
300func (k *Key) MustTime(defaultVal ...time.Time) time.Time {
301 return k.MustTimeFormat(time.RFC3339, defaultVal...)
302}
303
304// In always returns value without error,
305// it returns default value if error occurs or doesn't fit into candidates.
306func (k *Key) In(defaultVal string, candidates []string) string {
307 val := k.String()
308 for _, cand := range candidates {
309 if val == cand {
310 return val
311 }
312 }
313 return defaultVal
314}
315
316// InFloat64 always returns value without error,
317// it returns default value if error occurs or doesn't fit into candidates.
318func (k *Key) InFloat64(defaultVal float64, candidates []float64) float64 {
319 val := k.MustFloat64()
320 for _, cand := range candidates {
321 if val == cand {
322 return val
323 }
324 }
325 return defaultVal
326}
327
328// InInt always returns value without error,
329// it returns default value if error occurs or doesn't fit into candidates.
330func (k *Key) InInt(defaultVal int, candidates []int) int {
331 val := k.MustInt()
332 for _, cand := range candidates {
333 if val == cand {
334 return val
335 }
336 }
337 return defaultVal
338}
339
340// InInt64 always returns value without error,
341// it returns default value if error occurs or doesn't fit into candidates.
342func (k *Key) InInt64(defaultVal int64, candidates []int64) int64 {
343 val := k.MustInt64()
344 for _, cand := range candidates {
345 if val == cand {
346 return val
347 }
348 }
349 return defaultVal
350}
351
352// InUint always returns value without error,
353// it returns default value if error occurs or doesn't fit into candidates.
354func (k *Key) InUint(defaultVal uint, candidates []uint) uint {
355 val := k.MustUint()
356 for _, cand := range candidates {
357 if val == cand {
358 return val
359 }
360 }
361 return defaultVal
362}
363
364// InUint64 always returns value without error,
365// it returns default value if error occurs or doesn't fit into candidates.
366func (k *Key) InUint64(defaultVal uint64, candidates []uint64) uint64 {
367 val := k.MustUint64()
368 for _, cand := range candidates {
369 if val == cand {
370 return val
371 }
372 }
373 return defaultVal
374}
375
376// InTimeFormat always parses with given format and returns value without error,
377// it returns default value if error occurs or doesn't fit into candidates.
378func (k *Key) InTimeFormat(format string, defaultVal time.Time, candidates []time.Time) time.Time {
379 val := k.MustTimeFormat(format)
380 for _, cand := range candidates {
381 if val == cand {
382 return val
383 }
384 }
385 return defaultVal
386}
387
388// InTime always parses with RFC3339 format and returns value without error,
389// it returns default value if error occurs or doesn't fit into candidates.
390func (k *Key) InTime(defaultVal time.Time, candidates []time.Time) time.Time {
391 return k.InTimeFormat(time.RFC3339, defaultVal, candidates)
392}
393
394// RangeFloat64 checks if value is in given range inclusively,
395// and returns default value if it's not.
396func (k *Key) RangeFloat64(defaultVal, min, max float64) float64 {
397 val := k.MustFloat64()
398 if val < min || val > max {
399 return defaultVal
400 }
401 return val
402}
403
404// RangeInt checks if value is in given range inclusively,
405// and returns default value if it's not.
406func (k *Key) RangeInt(defaultVal, min, max int) int {
407 val := k.MustInt()
408 if val < min || val > max {
409 return defaultVal
410 }
411 return val
412}
413
414// RangeInt64 checks if value is in given range inclusively,
415// and returns default value if it's not.
416func (k *Key) RangeInt64(defaultVal, min, max int64) int64 {
417 val := k.MustInt64()
418 if val < min || val > max {
419 return defaultVal
420 }
421 return val
422}
423
424// RangeTimeFormat checks if value with given format is in given range inclusively,
425// and returns default value if it's not.
426func (k *Key) RangeTimeFormat(format string, defaultVal, min, max time.Time) time.Time {
427 val := k.MustTimeFormat(format)
428 if val.Unix() < min.Unix() || val.Unix() > max.Unix() {
429 return defaultVal
430 }
431 return val
432}
433
434// RangeTime checks if value with RFC3339 format is in given range inclusively,
435// and returns default value if it's not.
436func (k *Key) RangeTime(defaultVal, min, max time.Time) time.Time {
437 return k.RangeTimeFormat(time.RFC3339, defaultVal, min, max)
438}
439
440// Strings returns list of string divided by given delimiter.
441func (k *Key) Strings(delim string) []string {
442 str := k.String()
443 if len(str) == 0 {
444 return []string{}
445 }
446
447 vals := strings.Split(str, delim)
448 for i := range vals {
449 // vals[i] = k.transformValue(strings.TrimSpace(vals[i]))
450 vals[i] = strings.TrimSpace(vals[i])
451 }
452 return vals
453}
454
455// StringsWithShadows returns list of string divided by given delimiter.
456// Shadows will also be appended if any.
457func (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
474// Float64s returns list of float64 divided by given delimiter. Any invalid input will be treated as zero value.
475func (k *Key) Float64s(delim string) []float64 {
476 vals, _ := k.getFloat64s(delim, true, false)
477 return vals
478}
479
480// Ints returns list of int divided by given delimiter. Any invalid input will be treated as zero value.
481func (k *Key) Ints(delim string) []int {
482 vals, _ := k.parseInts(k.Strings(delim), true, false)
483 return vals
484}
485
486// Int64s returns list of int64 divided by given delimiter. Any invalid input will be treated as zero value.
487func (k *Key) Int64s(delim string) []int64 {
488 vals, _ := k.parseInt64s(k.Strings(delim), true, false)
489 return vals
490}
491
492// Uints returns list of uint divided by given delimiter. Any invalid input will be treated as zero value.
493func (k *Key) Uints(delim string) []uint {
494 vals, _ := k.getUints(delim, true, false)
495 return vals
496}
497
498// Uint64s returns list of uint64 divided by given delimiter. Any invalid input will be treated as zero value.
499func (k *Key) Uint64s(delim string) []uint64 {
500 vals, _ := k.getUint64s(delim, true, false)
501 return vals
502}
503
504// TimesFormat parses with given format and returns list of time.Time divided by given delimiter.
505// Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
506func (k *Key) TimesFormat(format, delim string) []time.Time {
507 vals, _ := k.getTimesFormat(format, delim, true, false)
508 return vals
509}
510
511// Times parses with RFC3339 format and returns list of time.Time divided by given delimiter.
512// Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
513func (k *Key) Times(delim string) []time.Time {
514 return k.TimesFormat(time.RFC3339, delim)
515}
516
517// ValidFloat64s returns list of float64 divided by given delimiter. If some value is not float, then
518// it will not be included to result list.
519func (k *Key) ValidFloat64s(delim string) []float64 {
520 vals, _ := k.getFloat64s(delim, false, false)
521 return vals
522}
523
524// ValidInts returns list of int divided by given delimiter. If some value is not integer, then it will
525// not be included to result list.
526func (k *Key) ValidInts(delim string) []int {
527 vals, _ := k.parseInts(k.Strings(delim), false, false)
528 return vals
529}
530
531// ValidInt64s returns list of int64 divided by given delimiter. If some value is not 64-bit integer,
532// then it will not be included to result list.
533func (k *Key) ValidInt64s(delim string) []int64 {
534 vals, _ := k.parseInt64s(k.Strings(delim), false, false)
535 return vals
536}
537
538// ValidUints returns list of uint divided by given delimiter. If some value is not unsigned integer,
539// then it will not be included to result list.
540func (k *Key) ValidUints(delim string) []uint {
541 vals, _ := k.getUints(delim, false, false)
542 return vals
543}
544
545// ValidUint64s returns list of uint64 divided by given delimiter. If some value is not 64-bit unsigned
546// integer, then it will not be included to result list.
547func (k *Key) ValidUint64s(delim string) []uint64 {
548 vals, _ := k.getUint64s(delim, false, false)
549 return vals
550}
551
552// ValidTimesFormat parses with given format and returns list of time.Time divided by given delimiter.
553func (k *Key) ValidTimesFormat(format, delim string) []time.Time {
554 vals, _ := k.getTimesFormat(format, delim, false, false)
555 return vals
556}
557
558// ValidTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter.
559func (k *Key) ValidTimes(delim string) []time.Time {
560 return k.ValidTimesFormat(time.RFC3339, delim)
561}
562
563// StrictFloat64s returns list of float64 divided by given delimiter or error on first invalid input.
564func (k *Key) StrictFloat64s(delim string) ([]float64, error) {
565 return k.getFloat64s(delim, false, true)
566}
567
568// StrictInts returns list of int divided by given delimiter or error on first invalid input.
569func (k *Key) StrictInts(delim string) ([]int, error) {
570 return k.parseInts(k.Strings(delim), false, true)
571}
572
573// StrictInt64s returns list of int64 divided by given delimiter or error on first invalid input.
574func (k *Key) StrictInt64s(delim string) ([]int64, error) {
575 return k.parseInt64s(k.Strings(delim), false, true)
576}
577
578// StrictUints returns list of uint divided by given delimiter or error on first invalid input.
579func (k *Key) StrictUints(delim string) ([]uint, error) {
580 return k.getUints(delim, false, true)
581}
582
583// StrictUint64s returns list of uint64 divided by given delimiter or error on first invalid input.
584func (k *Key) StrictUint64s(delim string) ([]uint64, error) {
585 return k.getUint64s(delim, false, true)
586}
587
588// StrictTimesFormat parses with given format and returns list of time.Time divided by given delimiter
589// or error on first invalid input.
590func (k *Key) StrictTimesFormat(format, delim string) ([]time.Time, error) {
591 return k.getTimesFormat(format, delim, false, true)
592}
593
594// StrictTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter
595// or error on first invalid input.
596func (k *Key) StrictTimes(delim string) ([]time.Time, error) {
597 return k.StrictTimesFormat(time.RFC3339, delim)
598}
599
600// getFloat64s returns list of float64 divided by given delimiter.
601func (k *Key) getFloat64s(delim string, addInvalid, returnOnInvalid bool) ([]float64, error) {
602 strs := k.Strings(delim)
603 vals := make([]float64, 0, len(strs))
604 for _, str := range strs {
605 val, err := strconv.ParseFloat(str, 64)
606 if err != nil && returnOnInvalid {
607 return nil, err
608 }
609 if err == nil || addInvalid {
610 vals = append(vals, val)
611 }
612 }
613 return vals, nil
614}
615
616// parseInts transforms strings to ints.
617func (k *Key) parseInts(strs []string, addInvalid, returnOnInvalid bool) ([]int, error) {
618 vals := make([]int, 0, len(strs))
619 for _, str := range strs {
620 val, err := strconv.Atoi(str)
621 if err != nil && returnOnInvalid {
622 return nil, err
623 }
624 if err == nil || addInvalid {
625 vals = append(vals, val)
626 }
627 }
628 return vals, nil
629}
630
631// parseInt64s transforms strings to int64s.
632func (k *Key) parseInt64s(strs []string, addInvalid, returnOnInvalid bool) ([]int64, error) {
633 vals := make([]int64, 0, len(strs))
634 for _, str := range strs {
635 val, err := strconv.ParseInt(str, 10, 64)
636 if err != nil && returnOnInvalid {
637 return nil, err
638 }
639 if err == nil || addInvalid {
640 vals = append(vals, val)
641 }
642 }
643 return vals, nil
644}
645
646// getUints returns list of uint divided by given delimiter.
647func (k *Key) getUints(delim string, addInvalid, returnOnInvalid bool) ([]uint, error) {
648 strs := k.Strings(delim)
649 vals := make([]uint, 0, len(strs))
650 for _, str := range strs {
651 val, err := strconv.ParseUint(str, 10, 0)
652 if err != nil && returnOnInvalid {
653 return nil, err
654 }
655 if err == nil || addInvalid {
656 vals = append(vals, uint(val))
657 }
658 }
659 return vals, nil
660}
661
662// getUint64s returns list of uint64 divided by given delimiter.
663func (k *Key) getUint64s(delim string, addInvalid, returnOnInvalid bool) ([]uint64, error) {
664 strs := k.Strings(delim)
665 vals := make([]uint64, 0, len(strs))
666 for _, str := range strs {
667 val, err := strconv.ParseUint(str, 10, 64)
668 if err != nil && returnOnInvalid {
669 return nil, err
670 }
671 if err == nil || addInvalid {
672 vals = append(vals, val)
673 }
674 }
675 return vals, nil
676}
677
678// getTimesFormat parses with given format and returns list of time.Time divided by given delimiter.
679func (k *Key) getTimesFormat(format, delim string, addInvalid, returnOnInvalid bool) ([]time.Time, error) {
680 strs := k.Strings(delim)
681 vals := make([]time.Time, 0, len(strs))
682 for _, str := range strs {
683 val, err := time.Parse(format, str)
684 if err != nil && returnOnInvalid {
685 return nil, err
686 }
687 if err == nil || addInvalid {
688 vals = append(vals, val)
689 }
690 }
691 return vals, nil
692}
693
694// SetValue changes key value.
695func (k *Key) SetValue(v string) {
696 if k.s.f.BlockMode {
697 k.s.f.lock.Lock()
698 defer k.s.f.lock.Unlock()
699 }
700
701 k.value = v
702 k.s.keysHash[k.name] = v
703}