proto2_convert.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package runtime
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. )
  5. // StringP returns a pointer to a string whose pointee is same as the given string value.
  6. func StringP(val string) (*string, error) {
  7. return proto.String(val), nil
  8. }
  9. // BoolP parses the given string representation of a boolean value,
  10. // and returns a pointer to a bool whose value is same as the parsed value.
  11. func BoolP(val string) (*bool, error) {
  12. b, err := Bool(val)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return proto.Bool(b), nil
  17. }
  18. // Float64P parses the given string representation of a floating point number,
  19. // and returns a pointer to a float64 whose value is same as the parsed number.
  20. func Float64P(val string) (*float64, error) {
  21. f, err := Float64(val)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return proto.Float64(f), nil
  26. }
  27. // Float32P parses the given string representation of a floating point number,
  28. // and returns a pointer to a float32 whose value is same as the parsed number.
  29. func Float32P(val string) (*float32, error) {
  30. f, err := Float32(val)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return proto.Float32(f), nil
  35. }
  36. // Int64P parses the given string representation of an integer
  37. // and returns a pointer to a int64 whose value is same as the parsed integer.
  38. func Int64P(val string) (*int64, error) {
  39. i, err := Int64(val)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return proto.Int64(i), nil
  44. }
  45. // Int32P parses the given string representation of an integer
  46. // and returns a pointer to a int32 whose value is same as the parsed integer.
  47. func Int32P(val string) (*int32, error) {
  48. i, err := Int32(val)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return proto.Int32(i), err
  53. }
  54. // Uint64P parses the given string representation of an integer
  55. // and returns a pointer to a uint64 whose value is same as the parsed integer.
  56. func Uint64P(val string) (*uint64, error) {
  57. i, err := Uint64(val)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return proto.Uint64(i), err
  62. }
  63. // Uint32P parses the given string representation of an integer
  64. // and returns a pointer to a uint32 whose value is same as the parsed integer.
  65. func Uint32P(val string) (*uint32, error) {
  66. i, err := Uint32(val)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return proto.Uint32(i), err
  71. }