You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

727 lines
21 KiB

  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. package ini
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "strconv"
  20. "strings"
  21. "time"
  22. )
  23. // Key represents a key under a section.
  24. type Key struct {
  25. s *Section
  26. Comment string
  27. name string
  28. value string
  29. isAutoIncrement bool
  30. isBooleanType bool
  31. isShadow bool
  32. shadows []*Key
  33. }
  34. // newKey simply return a key object with given values.
  35. func newKey(s *Section, name, val string) *Key {
  36. return &Key{
  37. s: s,
  38. name: name,
  39. value: val,
  40. }
  41. }
  42. func (k *Key) addShadow(val string) error {
  43. if k.isShadow {
  44. return errors.New("cannot add shadow to another shadow key")
  45. } else if k.isAutoIncrement || k.isBooleanType {
  46. return errors.New("cannot add shadow to auto-increment or boolean key")
  47. }
  48. shadow := newKey(k.s, k.name, val)
  49. shadow.isShadow = true
  50. k.shadows = append(k.shadows, shadow)
  51. return nil
  52. }
  53. // AddShadow adds a new shadow key to itself.
  54. func (k *Key) AddShadow(val string) error {
  55. if !k.s.f.options.AllowShadows {
  56. return errors.New("shadow key is not allowed")
  57. }
  58. return k.addShadow(val)
  59. }
  60. // ValueMapper represents a mapping function for values, e.g. os.ExpandEnv
  61. type ValueMapper func(string) string
  62. // Name returns name of key.
  63. func (k *Key) Name() string {
  64. return k.name
  65. }
  66. // Value returns raw value of key for performance purpose.
  67. func (k *Key) Value() string {
  68. return k.value
  69. }
  70. // ValueWithShadows returns raw values of key and its shadows if any.
  71. func (k *Key) ValueWithShadows() []string {
  72. if len(k.shadows) == 0 {
  73. return []string{k.value}
  74. }
  75. vals := make([]string, len(k.shadows)+1)
  76. vals[0] = k.value
  77. for i := range k.shadows {
  78. vals[i+1] = k.shadows[i].value
  79. }
  80. return vals
  81. }
  82. // transformValue takes a raw value and transforms to its final string.
  83. func (k *Key) transformValue(val string) string {
  84. if k.s.f.ValueMapper != nil {
  85. val = k.s.f.ValueMapper(val)
  86. }
  87. // Fail-fast if no indicate char found for recursive value
  88. if !strings.Contains(val, "%") {
  89. return val
  90. }
  91. for i := 0; i < _DEPTH_VALUES; i++ {
  92. vr := varPattern.FindString(val)
  93. if len(vr) == 0 {
  94. break
  95. }
  96. // Take off leading '%(' and trailing ')s'.
  97. noption := strings.TrimLeft(vr, "%(")
  98. noption = strings.TrimRight(noption, ")s")
  99. // Search in the same section.
  100. nk, err := k.s.GetKey(noption)
  101. if err != nil || k == nk {
  102. // Search again in default section.
  103. nk, _ = k.s.f.Section("").GetKey(noption)
  104. }
  105. // Substitute by new value and take off leading '%(' and trailing ')s'.
  106. val = strings.Replace(val, vr, nk.value, -1)
  107. }
  108. return val
  109. }
  110. // String returns string representation of value.
  111. func (k *Key) String() string {
  112. return k.transformValue(k.value)
  113. }
  114. // Validate accepts a validate function which can
  115. // return modifed result as key value.
  116. func (k *Key) Validate(fn func(string) string) string {
  117. return fn(k.String())
  118. }
  119. // parseBool returns the boolean value represented by the string.
  120. //
  121. // It accepts 1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On,
  122. // 0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Off.
  123. // Any other value returns an error.
  124. func parseBool(str string) (value bool, err error) {
  125. switch str {
  126. case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "y", "ON", "on", "On":
  127. return true, nil
  128. case "0", "f", "F", "false", "FALSE", "False", "NO", "no", "No", "n", "OFF", "off", "Off":
  129. return false, nil
  130. }
  131. return false, fmt.Errorf("parsing \"%s\": invalid syntax", str)
  132. }
  133. // Bool returns bool type value.
  134. func (k *Key) Bool() (bool, error) {
  135. return parseBool(k.String())
  136. }
  137. // Float64 returns float64 type value.
  138. func (k *Key) Float64() (float64, error) {
  139. return strconv.ParseFloat(k.String(), 64)
  140. }
  141. // Int returns int type value.
  142. func (k *Key) Int() (int, error) {
  143. return strconv.Atoi(k.String())
  144. }
  145. // Int64 returns int64 type value.
  146. func (k *Key) Int64() (int64, error) {
  147. return strconv.ParseInt(k.String(), 10, 64)
  148. }
  149. // Uint returns uint type valued.
  150. func (k *Key) Uint() (uint, error) {
  151. u, e := strconv.ParseUint(k.String(), 10, 64)
  152. return uint(u), e
  153. }
  154. // Uint64 returns uint64 type value.
  155. func (k *Key) Uint64() (uint64, error) {
  156. return strconv.ParseUint(k.String(), 10, 64)
  157. }
  158. // Duration returns time.Duration type value.
  159. func (k *Key) Duration() (time.Duration, error) {
  160. return time.ParseDuration(k.String())
  161. }
  162. // TimeFormat parses with given format and returns time.Time type value.
  163. func (k *Key) TimeFormat(format string) (time.Time, error) {
  164. return time.Parse(format, k.String())
  165. }
  166. // Time parses with RFC3339 format and returns time.Time type value.
  167. func (k *Key) Time() (time.Time, error) {
  168. return k.TimeFormat(time.RFC3339)
  169. }
  170. // MustString returns default value if key value is empty.
  171. func (k *Key) MustString(defaultVal string) string {
  172. val := k.String()
  173. if len(val) == 0 {
  174. k.value = defaultVal
  175. return defaultVal
  176. }
  177. return val
  178. }
  179. // MustBool always returns value without error,
  180. // it returns false if error occurs.
  181. func (k *Key) MustBool(defaultVal ...bool) bool {
  182. val, err := k.Bool()
  183. if len(defaultVal) > 0 && err != nil {
  184. k.value = strconv.FormatBool(defaultVal[0])
  185. return defaultVal[0]
  186. }
  187. return val
  188. }
  189. // MustFloat64 always returns value without error,
  190. // it returns 0.0 if error occurs.
  191. func (k *Key) MustFloat64(defaultVal ...float64) float64 {
  192. val, err := k.Float64()
  193. if len(defaultVal) > 0 && err != nil {
  194. k.value = strconv.FormatFloat(defaultVal[0], 'f', -1, 64)
  195. return defaultVal[0]
  196. }
  197. return val
  198. }
  199. // MustInt always returns value without error,
  200. // it returns 0 if error occurs.
  201. func (k *Key) MustInt(defaultVal ...int) int {
  202. val, err := k.Int()
  203. if len(defaultVal) > 0 && err != nil {
  204. k.value = strconv.FormatInt(int64(defaultVal[0]), 10)
  205. return defaultVal[0]
  206. }
  207. return val
  208. }
  209. // MustInt64 always returns value without error,
  210. // it returns 0 if error occurs.
  211. func (k *Key) MustInt64(defaultVal ...int64) int64 {
  212. val, err := k.Int64()
  213. if len(defaultVal) > 0 && err != nil {
  214. k.value = strconv.FormatInt(defaultVal[0], 10)
  215. return defaultVal[0]
  216. }
  217. return val
  218. }
  219. // MustUint always returns value without error,
  220. // it returns 0 if error occurs.
  221. func (k *Key) MustUint(defaultVal ...uint) uint {
  222. val, err := k.Uint()
  223. if len(defaultVal) > 0 && err != nil {
  224. k.value = strconv.FormatUint(uint64(defaultVal[0]), 10)
  225. return defaultVal[0]
  226. }
  227. return val
  228. }
  229. // MustUint64 always returns value without error,
  230. // it returns 0 if error occurs.
  231. func (k *Key) MustUint64(defaultVal ...uint64) uint64 {
  232. val, err := k.Uint64()
  233. if len(defaultVal) > 0 && err != nil {
  234. k.value = strconv.FormatUint(defaultVal[0], 10)
  235. return defaultVal[0]
  236. }
  237. return val
  238. }
  239. // MustDuration always returns value without error,
  240. // it returns zero value if error occurs.
  241. func (k *Key) MustDuration(defaultVal ...time.Duration) time.Duration {
  242. val, err := k.Duration()
  243. if len(defaultVal) > 0 && err != nil {
  244. k.value = defaultVal[0].String()
  245. return defaultVal[0]
  246. }
  247. return val
  248. }
  249. // MustTimeFormat always parses with given format and returns value without error,
  250. // it returns zero value if error occurs.
  251. func (k *Key) MustTimeFormat(format string, defaultVal ...time.Time) time.Time {
  252. val, err := k.TimeFormat(format)
  253. if len(defaultVal) > 0 && err != nil {
  254. k.value = defaultVal[0].Format(format)
  255. return defaultVal[0]
  256. }
  257. return val
  258. }
  259. // MustTime always parses with RFC3339 format and returns value without error,
  260. // it returns zero value if error occurs.
  261. func (k *Key) MustTime(defaultVal ...time.Time) time.Time {
  262. return k.MustTimeFormat(time.RFC3339, defaultVal...)
  263. }
  264. // In always returns value without error,
  265. // it returns default value if error occurs or doesn't fit into candidates.
  266. func (k *Key) In(defaultVal string, candidates []string) string {
  267. val := k.String()
  268. for _, cand := range candidates {
  269. if val == cand {
  270. return val
  271. }
  272. }
  273. return defaultVal
  274. }
  275. // InFloat64 always returns value without error,
  276. // it returns default value if error occurs or doesn't fit into candidates.
  277. func (k *Key) InFloat64(defaultVal float64, candidates []float64) float64 {
  278. val := k.MustFloat64()
  279. for _, cand := range candidates {
  280. if val == cand {
  281. return val
  282. }
  283. }
  284. return defaultVal
  285. }
  286. // InInt always returns value without error,
  287. // it returns default value if error occurs or doesn't fit into candidates.
  288. func (k *Key) InInt(defaultVal int, candidates []int) int {
  289. val := k.MustInt()
  290. for _, cand := range candidates {
  291. if val == cand {
  292. return val
  293. }
  294. }
  295. return defaultVal
  296. }
  297. // InInt64 always returns value without error,
  298. // it returns default value if error occurs or doesn't fit into candidates.
  299. func (k *Key) InInt64(defaultVal int64, candidates []int64) int64 {
  300. val := k.MustInt64()
  301. for _, cand := range candidates {
  302. if val == cand {
  303. return val
  304. }
  305. }
  306. return defaultVal
  307. }
  308. // InUint always returns value without error,
  309. // it returns default value if error occurs or doesn't fit into candidates.
  310. func (k *Key) InUint(defaultVal uint, candidates []uint) uint {
  311. val := k.MustUint()
  312. for _, cand := range candidates {
  313. if val == cand {
  314. return val
  315. }
  316. }
  317. return defaultVal
  318. }
  319. // InUint64 always returns value without error,
  320. // it returns default value if error occurs or doesn't fit into candidates.
  321. func (k *Key) InUint64(defaultVal uint64, candidates []uint64) uint64 {
  322. val := k.MustUint64()
  323. for _, cand := range candidates {
  324. if val == cand {
  325. return val
  326. }
  327. }
  328. return defaultVal
  329. }
  330. // InTimeFormat always parses with given format and returns value without error,
  331. // it returns default value if error occurs or doesn't fit into candidates.
  332. func (k *Key) InTimeFormat(format string, defaultVal time.Time, candidates []time.Time) time.Time {
  333. val := k.MustTimeFormat(format)
  334. for _, cand := range candidates {
  335. if val == cand {
  336. return val
  337. }
  338. }
  339. return defaultVal
  340. }
  341. // InTime always parses with RFC3339 format and returns value without error,
  342. // it returns default value if error occurs or doesn't fit into candidates.
  343. func (k *Key) InTime(defaultVal time.Time, candidates []time.Time) time.Time {
  344. return k.InTimeFormat(time.RFC3339, defaultVal, candidates)
  345. }
  346. // RangeFloat64 checks if value is in given range inclusively,
  347. // and returns default value if it's not.
  348. func (k *Key) RangeFloat64(defaultVal, min, max float64) float64 {
  349. val := k.MustFloat64()
  350. if val < min || val > max {
  351. return defaultVal
  352. }
  353. return val
  354. }
  355. // RangeInt checks if value is in given range inclusively,
  356. // and returns default value if it's not.
  357. func (k *Key) RangeInt(defaultVal, min, max int) int {
  358. val := k.MustInt()
  359. if val < min || val > max {
  360. return defaultVal
  361. }
  362. return val
  363. }
  364. // RangeInt64 checks if value is in given range inclusively,
  365. // and returns default value if it's not.
  366. func (k *Key) RangeInt64(defaultVal, min, max int64) int64 {
  367. val := k.MustInt64()
  368. if val < min || val > max {
  369. return defaultVal
  370. }
  371. return val
  372. }
  373. // RangeTimeFormat checks if value with given format is in given range inclusively,
  374. // and returns default value if it's not.
  375. func (k *Key) RangeTimeFormat(format string, defaultVal, min, max time.Time) time.Time {
  376. val := k.MustTimeFormat(format)
  377. if val.Unix() < min.Unix() || val.Unix() > max.Unix() {
  378. return defaultVal
  379. }
  380. return val
  381. }
  382. // RangeTime checks if value with RFC3339 format is in given range inclusively,
  383. // and returns default value if it's not.
  384. func (k *Key) RangeTime(defaultVal, min, max time.Time) time.Time {
  385. return k.RangeTimeFormat(time.RFC3339, defaultVal, min, max)
  386. }
  387. // Strings returns list of string divided by given delimiter.
  388. func (k *Key) Strings(delim string) []string {
  389. str := k.String()
  390. if len(str) == 0 {
  391. return []string{}
  392. }
  393. runes := []rune(str)
  394. vals := make([]string, 0, 2)
  395. var buf bytes.Buffer
  396. escape := false
  397. idx := 0
  398. for {
  399. if escape {
  400. escape = false
  401. if runes[idx] != '\\' && !strings.HasPrefix(string(runes[idx:]), delim) {
  402. buf.WriteRune('\\')
  403. }
  404. buf.WriteRune(runes[idx])
  405. } else {
  406. if runes[idx] == '\\' {
  407. escape = true
  408. } else if strings.HasPrefix(string(runes[idx:]), delim) {
  409. idx += len(delim) - 1
  410. vals = append(vals, strings.TrimSpace(buf.String()))
  411. buf.Reset()
  412. } else {
  413. buf.WriteRune(runes[idx])
  414. }
  415. }
  416. idx += 1
  417. if idx == len(runes) {
  418. break
  419. }
  420. }
  421. if buf.Len() > 0 {
  422. vals = append(vals, strings.TrimSpace(buf.String()))
  423. }
  424. return vals
  425. }
  426. // StringsWithShadows returns list of string divided by given delimiter.
  427. // Shadows will also be appended if any.
  428. func (k *Key) StringsWithShadows(delim string) []string {
  429. vals := k.ValueWithShadows()
  430. results := make([]string, 0, len(vals)*2)
  431. for i := range vals {
  432. if len(vals) == 0 {
  433. continue
  434. }
  435. results = append(results, strings.Split(vals[i], delim)...)
  436. }
  437. for i := range results {
  438. results[i] = k.transformValue(strings.TrimSpace(results[i]))
  439. }
  440. return results
  441. }
  442. // Float64s returns list of float64 divided by given delimiter. Any invalid input will be treated as zero value.
  443. func (k *Key) Float64s(delim string) []float64 {
  444. vals, _ := k.parseFloat64s(k.Strings(delim), true, false)
  445. return vals
  446. }
  447. // Ints returns list of int divided by given delimiter. Any invalid input will be treated as zero value.
  448. func (k *Key) Ints(delim string) []int {
  449. vals, _ := k.parseInts(k.Strings(delim), true, false)
  450. return vals
  451. }
  452. // Int64s returns list of int64 divided by given delimiter. Any invalid input will be treated as zero value.
  453. func (k *Key) Int64s(delim string) []int64 {
  454. vals, _ := k.parseInt64s(k.Strings(delim), true, false)
  455. return vals
  456. }
  457. // Uints returns list of uint divided by given delimiter. Any invalid input will be treated as zero value.
  458. func (k *Key) Uints(delim string) []uint {
  459. vals, _ := k.parseUints(k.Strings(delim), true, false)
  460. return vals
  461. }
  462. // Uint64s returns list of uint64 divided by given delimiter. Any invalid input will be treated as zero value.
  463. func (k *Key) Uint64s(delim string) []uint64 {
  464. vals, _ := k.parseUint64s(k.Strings(delim), true, false)
  465. return vals
  466. }
  467. // TimesFormat parses with given format and returns list of time.Time divided by given delimiter.
  468. // Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
  469. func (k *Key) TimesFormat(format, delim string) []time.Time {
  470. vals, _ := k.parseTimesFormat(format, k.Strings(delim), true, false)
  471. return vals
  472. }
  473. // Times parses with RFC3339 format and returns list of time.Time divided by given delimiter.
  474. // Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
  475. func (k *Key) Times(delim string) []time.Time {
  476. return k.TimesFormat(time.RFC3339, delim)
  477. }
  478. // ValidFloat64s returns list of float64 divided by given delimiter. If some value is not float, then
  479. // it will not be included to result list.
  480. func (k *Key) ValidFloat64s(delim string) []float64 {
  481. vals, _ := k.parseFloat64s(k.Strings(delim), false, false)
  482. return vals
  483. }
  484. // ValidInts returns list of int divided by given delimiter. If some value is not integer, then it will
  485. // not be included to result list.
  486. func (k *Key) ValidInts(delim string) []int {
  487. vals, _ := k.parseInts(k.Strings(delim), false, false)
  488. return vals
  489. }
  490. // ValidInt64s returns list of int64 divided by given delimiter. If some value is not 64-bit integer,
  491. // then it will not be included to result list.
  492. func (k *Key) ValidInt64s(delim string) []int64 {
  493. vals, _ := k.parseInt64s(k.Strings(delim), false, false)
  494. return vals
  495. }
  496. // ValidUints returns list of uint divided by given delimiter. If some value is not unsigned integer,
  497. // then it will not be included to result list.
  498. func (k *Key) ValidUints(delim string) []uint {
  499. vals, _ := k.parseUints(k.Strings(delim), false, false)
  500. return vals
  501. }
  502. // ValidUint64s returns list of uint64 divided by given delimiter. If some value is not 64-bit unsigned
  503. // integer, then it will not be included to result list.
  504. func (k *Key) ValidUint64s(delim string) []uint64 {
  505. vals, _ := k.parseUint64s(k.Strings(delim), false, false)
  506. return vals
  507. }
  508. // ValidTimesFormat parses with given format and returns list of time.Time divided by given delimiter.
  509. func (k *Key) ValidTimesFormat(format, delim string) []time.Time {
  510. vals, _ := k.parseTimesFormat(format, k.Strings(delim), false, false)
  511. return vals
  512. }
  513. // ValidTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter.
  514. func (k *Key) ValidTimes(delim string) []time.Time {
  515. return k.ValidTimesFormat(time.RFC3339, delim)
  516. }
  517. // StrictFloat64s returns list of float64 divided by given delimiter or error on first invalid input.
  518. func (k *Key) StrictFloat64s(delim string) ([]float64, error) {
  519. return k.parseFloat64s(k.Strings(delim), false, true)
  520. }
  521. // StrictInts returns list of int divided by given delimiter or error on first invalid input.
  522. func (k *Key) StrictInts(delim string) ([]int, error) {
  523. return k.parseInts(k.Strings(delim), false, true)
  524. }
  525. // StrictInt64s returns list of int64 divided by given delimiter or error on first invalid input.
  526. func (k *Key) StrictInt64s(delim string) ([]int64, error) {
  527. return k.parseInt64s(k.Strings(delim), false, true)
  528. }
  529. // StrictUints returns list of uint divided by given delimiter or error on first invalid input.
  530. func (k *Key) StrictUints(delim string) ([]uint, error) {
  531. return k.parseUints(k.Strings(delim), false, true)
  532. }
  533. // StrictUint64s returns list of uint64 divided by given delimiter or error on first invalid input.
  534. func (k *Key) StrictUint64s(delim string) ([]uint64, error) {
  535. return k.parseUint64s(k.Strings(delim), false, true)
  536. }
  537. // StrictTimesFormat parses with given format and returns list of time.Time divided by given delimiter
  538. // or error on first invalid input.
  539. func (k *Key) StrictTimesFormat(format, delim string) ([]time.Time, error) {
  540. return k.parseTimesFormat(format, k.Strings(delim), false, true)
  541. }
  542. // StrictTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter
  543. // or error on first invalid input.
  544. func (k *Key) StrictTimes(delim string) ([]time.Time, error) {
  545. return k.StrictTimesFormat(time.RFC3339, delim)
  546. }
  547. // parseFloat64s transforms strings to float64s.
  548. func (k *Key) parseFloat64s(strs []string, addInvalid, returnOnInvalid bool) ([]float64, error) {
  549. vals := make([]float64, 0, len(strs))
  550. for _, str := range strs {
  551. val, err := strconv.ParseFloat(str, 64)
  552. if err != nil && returnOnInvalid {
  553. return nil, err
  554. }
  555. if err == nil || addInvalid {
  556. vals = append(vals, val)
  557. }
  558. }
  559. return vals, nil
  560. }
  561. // parseInts transforms strings to ints.
  562. func (k *Key) parseInts(strs []string, addInvalid, returnOnInvalid bool) ([]int, error) {
  563. vals := make([]int, 0, len(strs))
  564. for _, str := range strs {
  565. val, err := strconv.Atoi(str)
  566. if err != nil && returnOnInvalid {
  567. return nil, err
  568. }
  569. if err == nil || addInvalid {
  570. vals = append(vals, val)
  571. }
  572. }
  573. return vals, nil
  574. }
  575. // parseInt64s transforms strings to int64s.
  576. func (k *Key) parseInt64s(strs []string, addInvalid, returnOnInvalid bool) ([]int64, error) {
  577. vals := make([]int64, 0, len(strs))
  578. for _, str := range strs {
  579. val, err := strconv.ParseInt(str, 10, 64)
  580. if err != nil && returnOnInvalid {
  581. return nil, err
  582. }
  583. if err == nil || addInvalid {
  584. vals = append(vals, val)
  585. }
  586. }
  587. return vals, nil
  588. }
  589. // parseUints transforms strings to uints.
  590. func (k *Key) parseUints(strs []string, addInvalid, returnOnInvalid bool) ([]uint, error) {
  591. vals := make([]uint, 0, len(strs))
  592. for _, str := range strs {
  593. val, err := strconv.ParseUint(str, 10, 0)
  594. if err != nil && returnOnInvalid {
  595. return nil, err
  596. }
  597. if err == nil || addInvalid {
  598. vals = append(vals, uint(val))
  599. }
  600. }
  601. return vals, nil
  602. }
  603. // parseUint64s transforms strings to uint64s.
  604. func (k *Key) parseUint64s(strs []string, addInvalid, returnOnInvalid bool) ([]uint64, error) {
  605. vals := make([]uint64, 0, len(strs))
  606. for _, str := range strs {
  607. val, err := strconv.ParseUint(str, 10, 64)
  608. if err != nil && returnOnInvalid {
  609. return nil, err
  610. }
  611. if err == nil || addInvalid {
  612. vals = append(vals, val)
  613. }
  614. }
  615. return vals, nil
  616. }
  617. // parseTimesFormat transforms strings to times in given format.
  618. func (k *Key) parseTimesFormat(format string, strs []string, addInvalid, returnOnInvalid bool) ([]time.Time, error) {
  619. vals := make([]time.Time, 0, len(strs))
  620. for _, str := range strs {
  621. val, err := time.Parse(format, str)
  622. if err != nil && returnOnInvalid {
  623. return nil, err
  624. }
  625. if err == nil || addInvalid {
  626. vals = append(vals, val)
  627. }
  628. }
  629. return vals, nil
  630. }
  631. // SetValue changes key value.
  632. func (k *Key) SetValue(v string) {
  633. if k.s.f.BlockMode {
  634. k.s.f.lock.Lock()
  635. defer k.s.f.lock.Unlock()
  636. }
  637. k.value = v
  638. k.s.keysHash[k.name] = v
  639. }