http urls monitor.

key.go 21KB

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