本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

797 lines
22KB

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