Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

92 lignes
2.4 KiB

  1. // Copyright 2019 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package util
  14. import (
  15. "strconv"
  16. )
  17. // TODO(mdlayher): util packages are an anti-pattern and this should be moved
  18. // somewhere else that is more focused in the future.
  19. // A ValueParser enables parsing a single string into a variety of data types
  20. // in a concise and safe way. The Err method must be invoked after invoking
  21. // any other methods to ensure a value was successfully parsed.
  22. type ValueParser struct {
  23. v string
  24. err error
  25. }
  26. // NewValueParser creates a ValueParser using the input string.
  27. func NewValueParser(v string) *ValueParser {
  28. return &ValueParser{v: v}
  29. }
  30. // Int interprets the underlying value as an int and returns that value.
  31. func (vp *ValueParser) Int() int { return int(vp.int64()) }
  32. // PInt64 interprets the underlying value as an int64 and returns a pointer to
  33. // that value.
  34. func (vp *ValueParser) PInt64() *int64 {
  35. if vp.err != nil {
  36. return nil
  37. }
  38. v := vp.int64()
  39. return &v
  40. }
  41. // int64 interprets the underlying value as an int64 and returns that value.
  42. // TODO: export if/when necessary.
  43. func (vp *ValueParser) int64() int64 {
  44. if vp.err != nil {
  45. return 0
  46. }
  47. // A base value of zero makes ParseInt infer the correct base using the
  48. // string's prefix, if any.
  49. const base = 0
  50. v, err := strconv.ParseInt(vp.v, base, 64)
  51. if err != nil {
  52. vp.err = err
  53. return 0
  54. }
  55. return v
  56. }
  57. // PUInt64 interprets the underlying value as an uint64 and returns a pointer to
  58. // that value.
  59. func (vp *ValueParser) PUInt64() *uint64 {
  60. if vp.err != nil {
  61. return nil
  62. }
  63. // A base value of zero makes ParseInt infer the correct base using the
  64. // string's prefix, if any.
  65. const base = 0
  66. v, err := strconv.ParseUint(vp.v, base, 64)
  67. if err != nil {
  68. vp.err = err
  69. return nil
  70. }
  71. return &v
  72. }
  73. // Err returns the last error, if any, encountered by the ValueParser.
  74. func (vp *ValueParser) Err() error {
  75. return vp.err
  76. }