key.go 20 KB

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