parse.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2018 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. "os"
  16. "strconv"
  17. "strings"
  18. )
  19. // ParseUint32s parses a slice of strings into a slice of uint32s.
  20. func ParseUint32s(ss []string) ([]uint32, error) {
  21. us := make([]uint32, 0, len(ss))
  22. for _, s := range ss {
  23. u, err := strconv.ParseUint(s, 10, 32)
  24. if err != nil {
  25. return nil, err
  26. }
  27. us = append(us, uint32(u))
  28. }
  29. return us, nil
  30. }
  31. // ParseUint64s parses a slice of strings into a slice of uint64s.
  32. func ParseUint64s(ss []string) ([]uint64, error) {
  33. us := make([]uint64, 0, len(ss))
  34. for _, s := range ss {
  35. u, err := strconv.ParseUint(s, 10, 64)
  36. if err != nil {
  37. return nil, err
  38. }
  39. us = append(us, u)
  40. }
  41. return us, nil
  42. }
  43. // ParsePInt64s parses a slice of strings into a slice of int64 pointers.
  44. func ParsePInt64s(ss []string) ([]*int64, error) {
  45. us := make([]*int64, 0, len(ss))
  46. for _, s := range ss {
  47. u, err := strconv.ParseInt(s, 10, 64)
  48. if err != nil {
  49. return nil, err
  50. }
  51. us = append(us, &u)
  52. }
  53. return us, nil
  54. }
  55. // Parses a uint64 from given hex in string.
  56. func ParseHexUint64s(ss []string) ([]*uint64, error) {
  57. us := make([]*uint64, 0, len(ss))
  58. for _, s := range ss {
  59. u, err := strconv.ParseUint(s, 16, 64)
  60. if err != nil {
  61. return nil, err
  62. }
  63. us = append(us, &u)
  64. }
  65. return us, nil
  66. }
  67. // ReadUintFromFile reads a file and attempts to parse a uint64 from it.
  68. func ReadUintFromFile(path string) (uint64, error) {
  69. data, err := os.ReadFile(path)
  70. if err != nil {
  71. return 0, err
  72. }
  73. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  74. }
  75. // ReadIntFromFile reads a file and attempts to parse a int64 from it.
  76. func ReadIntFromFile(path string) (int64, error) {
  77. data, err := os.ReadFile(path)
  78. if err != nil {
  79. return 0, err
  80. }
  81. return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
  82. }
  83. // ParseBool parses a string into a boolean pointer.
  84. func ParseBool(b string) *bool {
  85. var truth bool
  86. switch b {
  87. case "enabled":
  88. truth = true
  89. case "disabled":
  90. truth = false
  91. default:
  92. return nil
  93. }
  94. return &truth
  95. }