本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.0KB

  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. "io/ioutil"
  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. // ReadUintFromFile reads a file and attempts to parse a uint64 from it.
  56. func ReadUintFromFile(path string) (uint64, error) {
  57. data, err := ioutil.ReadFile(path)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  62. }
  63. // ParseBool parses a string into a boolean pointer.
  64. func ParseBool(b string) *bool {
  65. var truth bool
  66. switch b {
  67. case "enabled":
  68. truth = true
  69. case "disabled":
  70. truth = false
  71. default:
  72. return nil
  73. }
  74. return &truth
  75. }
上海开阖软件有限公司 沪ICP备12045867号-1