taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

key.go (25690B)


      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 
     15 package ini
     16 
     17 import (
     18 	"errors"
     19 	"fmt"
     20 	"strconv"
     21 	"strings"
     22 	"time"
     23 	"unicode/utf8"
     24 )
     25 
     26 // Key represents a key under a section.
     27 type Key struct {
     28 	s               *Section
     29 	Comment         string
     30 	name            string
     31 	value           string
     32 	isAutoIncrement bool
     33 	isBooleanType   bool
     34 
     35 	isShadow bool
     36 	shadows  []*Key
     37 
     38 	nestedValues []string
     39 }
     40 
     41 // newKey simply return a key object with given values.
     42 func newKey(s *Section, name, val string) *Key {
     43 	return &Key{
     44 		s:     s,
     45 		name:  name,
     46 		value: val,
     47 	}
     48 }
     49 
     50 func (k *Key) addShadow(val string) error {
     51 	if k.isShadow {
     52 		return errors.New("cannot add shadow to another shadow key")
     53 	} else if k.isAutoIncrement || k.isBooleanType {
     54 		return errors.New("cannot add shadow to auto-increment or boolean key")
     55 	}
     56 
     57 	// Deduplicate shadows based on their values. A kept empty value is a
     58 	// reset, so each one counts.
     59 	keepEmpty := val == "" && k.s.f.options.PreserveEmptyShadowValues
     60 	if !k.s.f.options.AllowDuplicateShadowValues && !keepEmpty {
     61 		if k.value == val {
     62 			return nil
     63 		}
     64 		for i := range k.shadows {
     65 			if k.shadows[i].value == val {
     66 				return nil
     67 			}
     68 		}
     69 	}
     70 
     71 	shadow := newKey(k.s, k.name, val)
     72 	shadow.isShadow = true
     73 	k.shadows = append(k.shadows, shadow)
     74 	return nil
     75 }
     76 
     77 // AddShadow adds a new shadow key to itself.
     78 func (k *Key) AddShadow(val string) error {
     79 	if !k.s.f.options.AllowShadows {
     80 		return errors.New("shadow key is not allowed")
     81 	}
     82 	return k.addShadow(val)
     83 }
     84 
     85 func (k *Key) addNestedValue(val string) error {
     86 	if k.isAutoIncrement || k.isBooleanType {
     87 		return errors.New("cannot add nested value to auto-increment or boolean key")
     88 	}
     89 
     90 	k.nestedValues = append(k.nestedValues, val)
     91 	return nil
     92 }
     93 
     94 // AddNestedValue adds a nested value to the key.
     95 func (k *Key) AddNestedValue(val string) error {
     96 	if !k.s.f.options.AllowNestedValues {
     97 		return errors.New("nested value is not allowed")
     98 	}
     99 	return k.addNestedValue(val)
    100 }
    101 
    102 // ValueMapper represents a mapping function for values, e.g. os.ExpandEnv
    103 type ValueMapper func(string) string
    104 
    105 // Name returns name of key.
    106 func (k *Key) Name() string {
    107 	return k.name
    108 }
    109 
    110 // Value returns raw value of key for performance purpose.
    111 func (k *Key) Value() string {
    112 	return k.value
    113 }
    114 
    115 // ValueWithShadows returns raw values of key and its shadows if any.
    116 // Empty values are omitted unless LoadOptions.PreserveEmptyShadowValues is set.
    117 func (k *Key) ValueWithShadows() []string {
    118 	keepEmpty := k.s.f.options.PreserveEmptyShadowValues
    119 	if len(k.shadows) == 0 {
    120 		if k.value == "" && !keepEmpty {
    121 			return []string{}
    122 		}
    123 		return []string{k.value}
    124 	}
    125 
    126 	vals := make([]string, 0, len(k.shadows)+1)
    127 	if k.value != "" || keepEmpty {
    128 		vals = append(vals, k.value)
    129 	}
    130 	for _, s := range k.shadows {
    131 		if s.value != "" || keepEmpty {
    132 			vals = append(vals, s.value)
    133 		}
    134 	}
    135 	return vals
    136 }
    137 
    138 // NestedValues returns nested values stored in the key.
    139 // It is possible returned value is nil if no nested values stored in the key.
    140 func (k *Key) NestedValues() []string {
    141 	return k.nestedValues
    142 }
    143 
    144 // transformValue takes a raw value and transforms to its final string.
    145 func (k *Key) transformValue(val string) string {
    146 	res, _ := k.transformValueWithVisited(val, nil)
    147 	return res
    148 }
    149 
    150 func (k *Key) transformValueWithVisited(val string, visited map[*Key]struct{}) (string, bool) {
    151 	if k.s.f.ValueMapper != nil {
    152 		val = k.s.f.ValueMapper(val)
    153 	}
    154 
    155 	// Fail-fast if no indicate char found for recursive value
    156 	if !strings.Contains(val, "%") {
    157 		return val, false
    158 	}
    159 
    160 	if visited == nil {
    161 		visited = make(map[*Key]struct{})
    162 	}
    163 	visited[k] = struct{}{}
    164 	defer delete(visited, k)
    165 
    166 	for i := 0; i < depthValues; i++ {
    167 		vr := varPattern.FindString(val)
    168 		if len(vr) == 0 {
    169 			break
    170 		}
    171 
    172 		// Take off leading '%(' and trailing ')s'.
    173 		noption := vr[2 : len(vr)-2]
    174 
    175 		// Search in the same section.
    176 		// If not found or found the key itself, then search again in default section.
    177 		nk, err := k.s.GetKey(noption)
    178 		if err != nil || k == nk {
    179 			nk, _ = k.s.f.Section("").GetKey(noption)
    180 			if nk == nil {
    181 				// Stop when no results found in the default section,
    182 				// and returns the value as-is.
    183 				break
    184 			}
    185 			if nk == k {
    186 				// Stop when key references itself to prevent cycle.
    187 				return val, true
    188 			}
    189 		}
    190 
    191 		if _, ok := visited[nk]; ok {
    192 			return val, true
    193 		}
    194 
    195 		newVal, hasCycle := nk.transformValueWithVisited(nk.value, visited)
    196 		if hasCycle {
    197 			return val, true
    198 		}
    199 
    200 		// Substitute by new value and take off leading '%(' and trailing ')s'.
    201 		val = strings.ReplaceAll(val, vr, newVal)
    202 	}
    203 	return val, false
    204 }
    205 
    206 // String returns string representation of value.
    207 func (k *Key) String() string {
    208 	return k.transformValue(k.value)
    209 }
    210 
    211 // Validate accepts a validate function which can
    212 // return modifed result as key value.
    213 func (k *Key) Validate(fn func(string) string) string {
    214 	return fn(k.String())
    215 }
    216 
    217 // parseBool returns the boolean value represented by the string.
    218 //
    219 // It accepts 1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On,
    220 // 0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Off.
    221 // Any other value returns an error.
    222 func parseBool(str string) (value bool, err error) {
    223 	switch str {
    224 	case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "y", "ON", "on", "On":
    225 		return true, nil
    226 	case "0", "f", "F", "false", "FALSE", "False", "NO", "no", "No", "n", "OFF", "off", "Off":
    227 		return false, nil
    228 	}
    229 	return false, fmt.Errorf("parsing \"%s\": invalid syntax", str)
    230 }
    231 
    232 // Bool returns bool type value.
    233 func (k *Key) Bool() (bool, error) {
    234 	return parseBool(k.String())
    235 }
    236 
    237 // Float64 returns float64 type value.
    238 func (k *Key) Float64() (float64, error) {
    239 	return strconv.ParseFloat(k.String(), 64)
    240 }
    241 
    242 // Int returns int type value.
    243 func (k *Key) Int() (int, error) {
    244 	v, err := strconv.ParseInt(k.String(), 0, 64)
    245 	return int(v), err
    246 }
    247 
    248 // Int64 returns int64 type value.
    249 func (k *Key) Int64() (int64, error) {
    250 	return strconv.ParseInt(k.String(), 0, 64)
    251 }
    252 
    253 // Uint returns uint type valued.
    254 func (k *Key) Uint() (uint, error) {
    255 	u, e := strconv.ParseUint(k.String(), 0, 64)
    256 	return uint(u), e
    257 }
    258 
    259 // Uint64 returns uint64 type value.
    260 func (k *Key) Uint64() (uint64, error) {
    261 	return strconv.ParseUint(k.String(), 0, 64)
    262 }
    263 
    264 // Duration returns time.Duration type value.
    265 func (k *Key) Duration() (time.Duration, error) {
    266 	return time.ParseDuration(k.String())
    267 }
    268 
    269 // TimeFormat parses with given format and returns time.Time type value.
    270 func (k *Key) TimeFormat(format string) (time.Time, error) {
    271 	return time.Parse(format, k.String())
    272 }
    273 
    274 // Time parses with RFC3339 format and returns time.Time type value.
    275 func (k *Key) Time() (time.Time, error) {
    276 	return k.TimeFormat(time.RFC3339)
    277 }
    278 
    279 // MustString returns default value if key value is empty.
    280 func (k *Key) MustString(defaultVal string) string {
    281 	val := k.String()
    282 	if len(val) == 0 {
    283 		k.value = defaultVal
    284 		return defaultVal
    285 	}
    286 	return val
    287 }
    288 
    289 // MustBool always returns value without error,
    290 // it returns false if error occurs.
    291 func (k *Key) MustBool(defaultVal ...bool) bool {
    292 	val, err := k.Bool()
    293 	if len(defaultVal) > 0 && err != nil {
    294 		k.value = strconv.FormatBool(defaultVal[0])
    295 		return defaultVal[0]
    296 	}
    297 	return val
    298 }
    299 
    300 // MustFloat64 always returns value without error,
    301 // it returns 0.0 if error occurs.
    302 func (k *Key) MustFloat64(defaultVal ...float64) float64 {
    303 	val, err := k.Float64()
    304 	if len(defaultVal) > 0 && err != nil {
    305 		k.value = strconv.FormatFloat(defaultVal[0], 'f', -1, 64)
    306 		return defaultVal[0]
    307 	}
    308 	return val
    309 }
    310 
    311 // MustInt always returns value without error,
    312 // it returns 0 if error occurs.
    313 func (k *Key) MustInt(defaultVal ...int) int {
    314 	val, err := k.Int()
    315 	if len(defaultVal) > 0 && err != nil {
    316 		k.value = strconv.FormatInt(int64(defaultVal[0]), 10)
    317 		return defaultVal[0]
    318 	}
    319 	return val
    320 }
    321 
    322 // MustInt64 always returns value without error,
    323 // it returns 0 if error occurs.
    324 func (k *Key) MustInt64(defaultVal ...int64) int64 {
    325 	val, err := k.Int64()
    326 	if len(defaultVal) > 0 && err != nil {
    327 		k.value = strconv.FormatInt(defaultVal[0], 10)
    328 		return defaultVal[0]
    329 	}
    330 	return val
    331 }
    332 
    333 // MustUint always returns value without error,
    334 // it returns 0 if error occurs.
    335 func (k *Key) MustUint(defaultVal ...uint) uint {
    336 	val, err := k.Uint()
    337 	if len(defaultVal) > 0 && err != nil {
    338 		k.value = strconv.FormatUint(uint64(defaultVal[0]), 10)
    339 		return defaultVal[0]
    340 	}
    341 	return val
    342 }
    343 
    344 // MustUint64 always returns value without error,
    345 // it returns 0 if error occurs.
    346 func (k *Key) MustUint64(defaultVal ...uint64) uint64 {
    347 	val, err := k.Uint64()
    348 	if len(defaultVal) > 0 && err != nil {
    349 		k.value = strconv.FormatUint(defaultVal[0], 10)
    350 		return defaultVal[0]
    351 	}
    352 	return val
    353 }
    354 
    355 // MustDuration always returns value without error,
    356 // it returns zero value if error occurs.
    357 func (k *Key) MustDuration(defaultVal ...time.Duration) time.Duration {
    358 	val, err := k.Duration()
    359 	if len(defaultVal) > 0 && err != nil {
    360 		k.value = defaultVal[0].String()
    361 		return defaultVal[0]
    362 	}
    363 	return val
    364 }
    365 
    366 // MustTimeFormat always parses with given format and returns value without error,
    367 // it returns zero value if error occurs.
    368 func (k *Key) MustTimeFormat(format string, defaultVal ...time.Time) time.Time {
    369 	val, err := k.TimeFormat(format)
    370 	if len(defaultVal) > 0 && err != nil {
    371 		k.value = defaultVal[0].Format(format)
    372 		return defaultVal[0]
    373 	}
    374 	return val
    375 }
    376 
    377 // MustTime always parses with RFC3339 format and returns value without error,
    378 // it returns zero value if error occurs.
    379 func (k *Key) MustTime(defaultVal ...time.Time) time.Time {
    380 	return k.MustTimeFormat(time.RFC3339, defaultVal...)
    381 }
    382 
    383 // In always returns value without error,
    384 // it returns default value if error occurs or doesn't fit into candidates.
    385 func (k *Key) In(defaultVal string, candidates []string) string {
    386 	val := k.String()
    387 	for _, cand := range candidates {
    388 		if val == cand {
    389 			return val
    390 		}
    391 	}
    392 	return defaultVal
    393 }
    394 
    395 // InFloat64 always returns value without error,
    396 // it returns default value if error occurs or doesn't fit into candidates.
    397 func (k *Key) InFloat64(defaultVal float64, candidates []float64) float64 {
    398 	val := k.MustFloat64()
    399 	for _, cand := range candidates {
    400 		if val == cand {
    401 			return val
    402 		}
    403 	}
    404 	return defaultVal
    405 }
    406 
    407 // InInt always returns value without error,
    408 // it returns default value if error occurs or doesn't fit into candidates.
    409 func (k *Key) InInt(defaultVal int, candidates []int) int {
    410 	val := k.MustInt()
    411 	for _, cand := range candidates {
    412 		if val == cand {
    413 			return val
    414 		}
    415 	}
    416 	return defaultVal
    417 }
    418 
    419 // InInt64 always returns value without error,
    420 // it returns default value if error occurs or doesn't fit into candidates.
    421 func (k *Key) InInt64(defaultVal int64, candidates []int64) int64 {
    422 	val := k.MustInt64()
    423 	for _, cand := range candidates {
    424 		if val == cand {
    425 			return val
    426 		}
    427 	}
    428 	return defaultVal
    429 }
    430 
    431 // InUint always returns value without error,
    432 // it returns default value if error occurs or doesn't fit into candidates.
    433 func (k *Key) InUint(defaultVal uint, candidates []uint) uint {
    434 	val := k.MustUint()
    435 	for _, cand := range candidates {
    436 		if val == cand {
    437 			return val
    438 		}
    439 	}
    440 	return defaultVal
    441 }
    442 
    443 // InUint64 always returns value without error,
    444 // it returns default value if error occurs or doesn't fit into candidates.
    445 func (k *Key) InUint64(defaultVal uint64, candidates []uint64) uint64 {
    446 	val := k.MustUint64()
    447 	for _, cand := range candidates {
    448 		if val == cand {
    449 			return val
    450 		}
    451 	}
    452 	return defaultVal
    453 }
    454 
    455 // InTimeFormat always parses with given format and returns value without error,
    456 // it returns default value if error occurs or doesn't fit into candidates.
    457 func (k *Key) InTimeFormat(format string, defaultVal time.Time, candidates []time.Time) time.Time {
    458 	val := k.MustTimeFormat(format)
    459 	for _, cand := range candidates {
    460 		if val.Equal(cand) {
    461 			return val
    462 		}
    463 	}
    464 	return defaultVal
    465 }
    466 
    467 // InTime always parses with RFC3339 format and returns value without error,
    468 // it returns default value if error occurs or doesn't fit into candidates.
    469 func (k *Key) InTime(defaultVal time.Time, candidates []time.Time) time.Time {
    470 	return k.InTimeFormat(time.RFC3339, defaultVal, candidates)
    471 }
    472 
    473 // RangeFloat64 checks if value is in given range inclusively,
    474 // and returns default value if it's not.
    475 func (k *Key) RangeFloat64(defaultVal, min, max float64) float64 {
    476 	val := k.MustFloat64()
    477 	if val < min || val > max {
    478 		return defaultVal
    479 	}
    480 	return val
    481 }
    482 
    483 // RangeInt checks if value is in given range inclusively,
    484 // and returns default value if it's not.
    485 func (k *Key) RangeInt(defaultVal, min, max int) int {
    486 	val := k.MustInt()
    487 	if val < min || val > max {
    488 		return defaultVal
    489 	}
    490 	return val
    491 }
    492 
    493 // RangeInt64 checks if value is in given range inclusively,
    494 // and returns default value if it's not.
    495 func (k *Key) RangeInt64(defaultVal, min, max int64) int64 {
    496 	val := k.MustInt64()
    497 	if val < min || val > max {
    498 		return defaultVal
    499 	}
    500 	return val
    501 }
    502 
    503 // RangeTimeFormat checks if value with given format is in given range inclusively,
    504 // and returns default value if it's not.
    505 func (k *Key) RangeTimeFormat(format string, defaultVal, min, max time.Time) time.Time {
    506 	val := k.MustTimeFormat(format)
    507 	if val.Unix() < min.Unix() || val.Unix() > max.Unix() {
    508 		return defaultVal
    509 	}
    510 	return val
    511 }
    512 
    513 // RangeTime checks if value with RFC3339 format is in given range inclusively,
    514 // and returns default value if it's not.
    515 func (k *Key) RangeTime(defaultVal, min, max time.Time) time.Time {
    516 	return k.RangeTimeFormat(time.RFC3339, defaultVal, min, max)
    517 }
    518 
    519 // escapeDelimited prepares a slice element for joining with delim.
    520 // Backslashes are escaped first, then occurrences of delim, matching Strings().
    521 func escapeDelimited(val, delim string) string {
    522 	if delim == "" {
    523 		return val
    524 	}
    525 	val = strings.ReplaceAll(val, `\`, `\\`)
    526 	return strings.ReplaceAll(val, delim, `\`+delim)
    527 }
    528 
    529 // Strings returns list of string divided by given delimiter.
    530 func (k *Key) Strings(delim string) []string {
    531 	str := k.String()
    532 	if len(str) == 0 {
    533 		return []string{}
    534 	}
    535 
    536 	maxParts := strings.Count(str, delim) + 1
    537 	vals := make([]string, 0, maxParts)
    538 
    539 	var buf strings.Builder
    540 	buf.Grow(len(str))
    541 
    542 	i := 0
    543 	for {
    544 		if str[i] == '\\' {
    545 			i++
    546 			if i >= len(str) {
    547 				break
    548 			}
    549 
    550 			if str[i] != '\\' && !strings.HasPrefix(str[i:], delim) {
    551 				buf.WriteRune('\\')
    552 			}
    553 
    554 			r, size := utf8.DecodeRuneInString(str[i:])
    555 			i += size
    556 			buf.WriteRune(r)
    557 		} else if strings.HasPrefix(str[i:], delim) {
    558 			i += len(delim)
    559 			vals = append(vals, strings.TrimSpace(buf.String()))
    560 			buf.Reset()
    561 		} else {
    562 			r, size := utf8.DecodeRuneInString(str[i:])
    563 			i += size
    564 			buf.WriteRune(r)
    565 		}
    566 
    567 		if i >= len(str) {
    568 			break
    569 		}
    570 	}
    571 
    572 	if buf.Len() > 0 {
    573 		vals = append(vals, strings.TrimSpace(buf.String()))
    574 	}
    575 
    576 	return vals
    577 }
    578 
    579 // StringsWithShadows returns list of string divided by given delimiter.
    580 // Shadows will also be appended if any.
    581 func (k *Key) StringsWithShadows(delim string) []string {
    582 	vals := k.ValueWithShadows()
    583 	results := make([]string, 0, len(vals)*2)
    584 	for i := range vals {
    585 		if len(vals) == 0 {
    586 			continue
    587 		}
    588 
    589 		results = append(results, strings.Split(vals[i], delim)...)
    590 	}
    591 
    592 	for i := range results {
    593 		results[i] = k.transformValue(strings.TrimSpace(results[i]))
    594 	}
    595 	return results
    596 }
    597 
    598 // Float64s returns list of float64 divided by given delimiter. Any invalid input will be treated as zero value.
    599 func (k *Key) Float64s(delim string) []float64 {
    600 	vals, _ := k.parseFloat64s(k.Strings(delim), true, false)
    601 	return vals
    602 }
    603 
    604 // Ints returns list of int divided by given delimiter. Any invalid input will be treated as zero value.
    605 func (k *Key) Ints(delim string) []int {
    606 	vals, _ := k.parseInts(k.Strings(delim), true, false)
    607 	return vals
    608 }
    609 
    610 // Int64s returns list of int64 divided by given delimiter. Any invalid input will be treated as zero value.
    611 func (k *Key) Int64s(delim string) []int64 {
    612 	vals, _ := k.parseInt64s(k.Strings(delim), true, false)
    613 	return vals
    614 }
    615 
    616 // Uints returns list of uint divided by given delimiter. Any invalid input will be treated as zero value.
    617 func (k *Key) Uints(delim string) []uint {
    618 	vals, _ := k.parseUints(k.Strings(delim), true, false)
    619 	return vals
    620 }
    621 
    622 // Uint64s returns list of uint64 divided by given delimiter. Any invalid input will be treated as zero value.
    623 func (k *Key) Uint64s(delim string) []uint64 {
    624 	vals, _ := k.parseUint64s(k.Strings(delim), true, false)
    625 	return vals
    626 }
    627 
    628 // Bools returns list of bool divided by given delimiter. Any invalid input will be treated as zero value.
    629 func (k *Key) Bools(delim string) []bool {
    630 	vals, _ := k.parseBools(k.Strings(delim), true, false)
    631 	return vals
    632 }
    633 
    634 // TimesFormat parses with given format and returns list of time.Time divided by given delimiter.
    635 // Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
    636 func (k *Key) TimesFormat(format, delim string) []time.Time {
    637 	vals, _ := k.parseTimesFormat(format, k.Strings(delim), true, false)
    638 	return vals
    639 }
    640 
    641 // Times parses with RFC3339 format and returns list of time.Time divided by given delimiter.
    642 // Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
    643 func (k *Key) Times(delim string) []time.Time {
    644 	return k.TimesFormat(time.RFC3339, delim)
    645 }
    646 
    647 // ValidFloat64s returns list of float64 divided by given delimiter. If some value is not float, then
    648 // it will not be included to result list.
    649 func (k *Key) ValidFloat64s(delim string) []float64 {
    650 	vals, _ := k.parseFloat64s(k.Strings(delim), false, false)
    651 	return vals
    652 }
    653 
    654 // ValidInts returns list of int divided by given delimiter. If some value is not integer, then it will
    655 // not be included to result list.
    656 func (k *Key) ValidInts(delim string) []int {
    657 	vals, _ := k.parseInts(k.Strings(delim), false, false)
    658 	return vals
    659 }
    660 
    661 // ValidInt64s returns list of int64 divided by given delimiter. If some value is not 64-bit integer,
    662 // then it will not be included to result list.
    663 func (k *Key) ValidInt64s(delim string) []int64 {
    664 	vals, _ := k.parseInt64s(k.Strings(delim), false, false)
    665 	return vals
    666 }
    667 
    668 // ValidUints returns list of uint divided by given delimiter. If some value is not unsigned integer,
    669 // then it will not be included to result list.
    670 func (k *Key) ValidUints(delim string) []uint {
    671 	vals, _ := k.parseUints(k.Strings(delim), false, false)
    672 	return vals
    673 }
    674 
    675 // ValidUint64s returns list of uint64 divided by given delimiter. If some value is not 64-bit unsigned
    676 // integer, then it will not be included to result list.
    677 func (k *Key) ValidUint64s(delim string) []uint64 {
    678 	vals, _ := k.parseUint64s(k.Strings(delim), false, false)
    679 	return vals
    680 }
    681 
    682 // ValidBools returns list of bool divided by given delimiter. If some value is not 64-bit unsigned
    683 // integer, then it will not be included to result list.
    684 func (k *Key) ValidBools(delim string) []bool {
    685 	vals, _ := k.parseBools(k.Strings(delim), false, false)
    686 	return vals
    687 }
    688 
    689 // ValidTimesFormat parses with given format and returns list of time.Time divided by given delimiter.
    690 func (k *Key) ValidTimesFormat(format, delim string) []time.Time {
    691 	vals, _ := k.parseTimesFormat(format, k.Strings(delim), false, false)
    692 	return vals
    693 }
    694 
    695 // ValidTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter.
    696 func (k *Key) ValidTimes(delim string) []time.Time {
    697 	return k.ValidTimesFormat(time.RFC3339, delim)
    698 }
    699 
    700 // StrictFloat64s returns list of float64 divided by given delimiter or error on first invalid input.
    701 func (k *Key) StrictFloat64s(delim string) ([]float64, error) {
    702 	return k.parseFloat64s(k.Strings(delim), false, true)
    703 }
    704 
    705 // StrictInts returns list of int divided by given delimiter or error on first invalid input.
    706 func (k *Key) StrictInts(delim string) ([]int, error) {
    707 	return k.parseInts(k.Strings(delim), false, true)
    708 }
    709 
    710 // StrictInt64s returns list of int64 divided by given delimiter or error on first invalid input.
    711 func (k *Key) StrictInt64s(delim string) ([]int64, error) {
    712 	return k.parseInt64s(k.Strings(delim), false, true)
    713 }
    714 
    715 // StrictUints returns list of uint divided by given delimiter or error on first invalid input.
    716 func (k *Key) StrictUints(delim string) ([]uint, error) {
    717 	return k.parseUints(k.Strings(delim), false, true)
    718 }
    719 
    720 // StrictUint64s returns list of uint64 divided by given delimiter or error on first invalid input.
    721 func (k *Key) StrictUint64s(delim string) ([]uint64, error) {
    722 	return k.parseUint64s(k.Strings(delim), false, true)
    723 }
    724 
    725 // StrictBools returns list of bool divided by given delimiter or error on first invalid input.
    726 func (k *Key) StrictBools(delim string) ([]bool, error) {
    727 	return k.parseBools(k.Strings(delim), false, true)
    728 }
    729 
    730 // StrictTimesFormat parses with given format and returns list of time.Time divided by given delimiter
    731 // or error on first invalid input.
    732 func (k *Key) StrictTimesFormat(format, delim string) ([]time.Time, error) {
    733 	return k.parseTimesFormat(format, k.Strings(delim), false, true)
    734 }
    735 
    736 // StrictTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter
    737 // or error on first invalid input.
    738 func (k *Key) StrictTimes(delim string) ([]time.Time, error) {
    739 	return k.StrictTimesFormat(time.RFC3339, delim)
    740 }
    741 
    742 // parseBools transforms strings to bools.
    743 func (k *Key) parseBools(strs []string, addInvalid, returnOnInvalid bool) ([]bool, error) {
    744 	vals := make([]bool, 0, len(strs))
    745 	parser := func(str string) (interface{}, error) {
    746 		val, err := parseBool(str)
    747 		return val, err
    748 	}
    749 	rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
    750 	if err == nil {
    751 		for _, val := range rawVals {
    752 			vals = append(vals, val.(bool))
    753 		}
    754 	}
    755 	return vals, err
    756 }
    757 
    758 // parseFloat64s transforms strings to float64s.
    759 func (k *Key) parseFloat64s(strs []string, addInvalid, returnOnInvalid bool) ([]float64, error) {
    760 	vals := make([]float64, 0, len(strs))
    761 	parser := func(str string) (interface{}, error) {
    762 		val, err := strconv.ParseFloat(str, 64)
    763 		return val, err
    764 	}
    765 	rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
    766 	if err == nil {
    767 		for _, val := range rawVals {
    768 			vals = append(vals, val.(float64))
    769 		}
    770 	}
    771 	return vals, err
    772 }
    773 
    774 // parseInts transforms strings to ints.
    775 func (k *Key) parseInts(strs []string, addInvalid, returnOnInvalid bool) ([]int, error) {
    776 	vals := make([]int, 0, len(strs))
    777 	parser := func(str string) (interface{}, error) {
    778 		val, err := strconv.ParseInt(str, 0, 64)
    779 		return val, err
    780 	}
    781 	rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
    782 	if err == nil {
    783 		for _, val := range rawVals {
    784 			vals = append(vals, int(val.(int64)))
    785 		}
    786 	}
    787 	return vals, err
    788 }
    789 
    790 // parseInt64s transforms strings to int64s.
    791 func (k *Key) parseInt64s(strs []string, addInvalid, returnOnInvalid bool) ([]int64, error) {
    792 	vals := make([]int64, 0, len(strs))
    793 	parser := func(str string) (interface{}, error) {
    794 		val, err := strconv.ParseInt(str, 0, 64)
    795 		return val, err
    796 	}
    797 
    798 	rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
    799 	if err == nil {
    800 		for _, val := range rawVals {
    801 			vals = append(vals, val.(int64))
    802 		}
    803 	}
    804 	return vals, err
    805 }
    806 
    807 // parseUints transforms strings to uints.
    808 func (k *Key) parseUints(strs []string, addInvalid, returnOnInvalid bool) ([]uint, error) {
    809 	vals := make([]uint, 0, len(strs))
    810 	parser := func(str string) (interface{}, error) {
    811 		val, err := strconv.ParseUint(str, 0, 64)
    812 		return val, err
    813 	}
    814 
    815 	rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
    816 	if err == nil {
    817 		for _, val := range rawVals {
    818 			vals = append(vals, uint(val.(uint64)))
    819 		}
    820 	}
    821 	return vals, err
    822 }
    823 
    824 // parseUint64s transforms strings to uint64s.
    825 func (k *Key) parseUint64s(strs []string, addInvalid, returnOnInvalid bool) ([]uint64, error) {
    826 	vals := make([]uint64, 0, len(strs))
    827 	parser := func(str string) (interface{}, error) {
    828 		val, err := strconv.ParseUint(str, 0, 64)
    829 		return val, err
    830 	}
    831 	rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
    832 	if err == nil {
    833 		for _, val := range rawVals {
    834 			vals = append(vals, val.(uint64))
    835 		}
    836 	}
    837 	return vals, err
    838 }
    839 
    840 type Parser func(str string) (interface{}, error)
    841 
    842 // parseTimesFormat transforms strings to times in given format.
    843 func (k *Key) parseTimesFormat(format string, strs []string, addInvalid, returnOnInvalid bool) ([]time.Time, error) {
    844 	vals := make([]time.Time, 0, len(strs))
    845 	parser := func(str string) (interface{}, error) {
    846 		val, err := time.Parse(format, str)
    847 		return val, err
    848 	}
    849 	rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
    850 	if err == nil {
    851 		for _, val := range rawVals {
    852 			vals = append(vals, val.(time.Time))
    853 		}
    854 	}
    855 	return vals, err
    856 }
    857 
    858 // doParse transforms strings to different types
    859 func (k *Key) doParse(strs []string, addInvalid, returnOnInvalid bool, parser Parser) ([]interface{}, error) {
    860 	vals := make([]interface{}, 0, len(strs))
    861 	for _, str := range strs {
    862 		val, err := parser(str)
    863 		if err != nil && returnOnInvalid {
    864 			return nil, err
    865 		}
    866 		if err == nil || addInvalid {
    867 			vals = append(vals, val)
    868 		}
    869 	}
    870 	return vals, nil
    871 }
    872 
    873 // SetValue changes key value.
    874 func (k *Key) SetValue(v string) {
    875 	if k.s.f.BlockMode {
    876 		k.s.f.lock.Lock()
    877 		defer k.s.f.lock.Unlock()
    878 	}
    879 
    880 	k.value = v
    881 	// The Key Section.Key returns for an empty name isn't in the section.
    882 	if k.name != "" {
    883 		k.s.keysHash[k.name] = v
    884 	}
    885 }