key.go 21 KB

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