convert.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package runtime
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. "google.golang.org/protobuf/types/known/durationpb"
  9. "google.golang.org/protobuf/types/known/timestamppb"
  10. "google.golang.org/protobuf/types/known/wrapperspb"
  11. )
  12. // String just returns the given string.
  13. // It is just for compatibility to other types.
  14. func String(val string) (string, error) {
  15. return val, nil
  16. }
  17. // StringSlice converts 'val' where individual strings are separated by
  18. // 'sep' into a string slice.
  19. func StringSlice(val, sep string) ([]string, error) {
  20. return strings.Split(val, sep), nil
  21. }
  22. // Bool converts the given string representation of a boolean value into bool.
  23. func Bool(val string) (bool, error) {
  24. return strconv.ParseBool(val)
  25. }
  26. // BoolSlice converts 'val' where individual booleans are separated by
  27. // 'sep' into a bool slice.
  28. func BoolSlice(val, sep string) ([]bool, error) {
  29. s := strings.Split(val, sep)
  30. values := make([]bool, len(s))
  31. for i, v := range s {
  32. value, err := Bool(v)
  33. if err != nil {
  34. return nil, err
  35. }
  36. values[i] = value
  37. }
  38. return values, nil
  39. }
  40. // Float64 converts the given string representation into representation of a floating point number into float64.
  41. func Float64(val string) (float64, error) {
  42. return strconv.ParseFloat(val, 64)
  43. }
  44. // Float64Slice converts 'val' where individual floating point numbers are separated by
  45. // 'sep' into a float64 slice.
  46. func Float64Slice(val, sep string) ([]float64, error) {
  47. s := strings.Split(val, sep)
  48. values := make([]float64, len(s))
  49. for i, v := range s {
  50. value, err := Float64(v)
  51. if err != nil {
  52. return nil, err
  53. }
  54. values[i] = value
  55. }
  56. return values, nil
  57. }
  58. // Float32 converts the given string representation of a floating point number into float32.
  59. func Float32(val string) (float32, error) {
  60. f, err := strconv.ParseFloat(val, 32)
  61. if err != nil {
  62. return 0, err
  63. }
  64. return float32(f), nil
  65. }
  66. // Float32Slice converts 'val' where individual floating point numbers are separated by
  67. // 'sep' into a float32 slice.
  68. func Float32Slice(val, sep string) ([]float32, error) {
  69. s := strings.Split(val, sep)
  70. values := make([]float32, len(s))
  71. for i, v := range s {
  72. value, err := Float32(v)
  73. if err != nil {
  74. return nil, err
  75. }
  76. values[i] = value
  77. }
  78. return values, nil
  79. }
  80. // Int64 converts the given string representation of an integer into int64.
  81. func Int64(val string) (int64, error) {
  82. return strconv.ParseInt(val, 0, 64)
  83. }
  84. // Int64Slice converts 'val' where individual integers are separated by
  85. // 'sep' into a int64 slice.
  86. func Int64Slice(val, sep string) ([]int64, error) {
  87. s := strings.Split(val, sep)
  88. values := make([]int64, len(s))
  89. for i, v := range s {
  90. value, err := Int64(v)
  91. if err != nil {
  92. return nil, err
  93. }
  94. values[i] = value
  95. }
  96. return values, nil
  97. }
  98. // Int32 converts the given string representation of an integer into int32.
  99. func Int32(val string) (int32, error) {
  100. i, err := strconv.ParseInt(val, 0, 32)
  101. if err != nil {
  102. return 0, err
  103. }
  104. return int32(i), nil
  105. }
  106. // Int32Slice converts 'val' where individual integers are separated by
  107. // 'sep' into a int32 slice.
  108. func Int32Slice(val, sep string) ([]int32, error) {
  109. s := strings.Split(val, sep)
  110. values := make([]int32, len(s))
  111. for i, v := range s {
  112. value, err := Int32(v)
  113. if err != nil {
  114. return nil, err
  115. }
  116. values[i] = value
  117. }
  118. return values, nil
  119. }
  120. // Uint64 converts the given string representation of an integer into uint64.
  121. func Uint64(val string) (uint64, error) {
  122. return strconv.ParseUint(val, 0, 64)
  123. }
  124. // Uint64Slice converts 'val' where individual integers are separated by
  125. // 'sep' into a uint64 slice.
  126. func Uint64Slice(val, sep string) ([]uint64, error) {
  127. s := strings.Split(val, sep)
  128. values := make([]uint64, len(s))
  129. for i, v := range s {
  130. value, err := Uint64(v)
  131. if err != nil {
  132. return nil, err
  133. }
  134. values[i] = value
  135. }
  136. return values, nil
  137. }
  138. // Uint32 converts the given string representation of an integer into uint32.
  139. func Uint32(val string) (uint32, error) {
  140. i, err := strconv.ParseUint(val, 0, 32)
  141. if err != nil {
  142. return 0, err
  143. }
  144. return uint32(i), nil
  145. }
  146. // Uint32Slice converts 'val' where individual integers are separated by
  147. // 'sep' into a uint32 slice.
  148. func Uint32Slice(val, sep string) ([]uint32, error) {
  149. s := strings.Split(val, sep)
  150. values := make([]uint32, len(s))
  151. for i, v := range s {
  152. value, err := Uint32(v)
  153. if err != nil {
  154. return nil, err
  155. }
  156. values[i] = value
  157. }
  158. return values, nil
  159. }
  160. // Bytes converts the given string representation of a byte sequence into a slice of bytes
  161. // A bytes sequence is encoded in URL-safe base64 without padding
  162. func Bytes(val string) ([]byte, error) {
  163. b, err := base64.StdEncoding.DecodeString(val)
  164. if err != nil {
  165. b, err = base64.URLEncoding.DecodeString(val)
  166. if err != nil {
  167. return nil, err
  168. }
  169. }
  170. return b, nil
  171. }
  172. // BytesSlice converts 'val' where individual bytes sequences, encoded in URL-safe
  173. // base64 without padding, are separated by 'sep' into a slice of bytes slices slice.
  174. func BytesSlice(val, sep string) ([][]byte, error) {
  175. s := strings.Split(val, sep)
  176. values := make([][]byte, len(s))
  177. for i, v := range s {
  178. value, err := Bytes(v)
  179. if err != nil {
  180. return nil, err
  181. }
  182. values[i] = value
  183. }
  184. return values, nil
  185. }
  186. // Timestamp converts the given RFC3339 formatted string into a timestamp.Timestamp.
  187. func Timestamp(val string) (*timestamppb.Timestamp, error) {
  188. var r timestamppb.Timestamp
  189. val = strconv.Quote(strings.Trim(val, `"`))
  190. unmarshaler := &protojson.UnmarshalOptions{}
  191. if err := unmarshaler.Unmarshal([]byte(val), &r); err != nil {
  192. return nil, err
  193. }
  194. return &r, nil
  195. }
  196. // Duration converts the given string into a timestamp.Duration.
  197. func Duration(val string) (*durationpb.Duration, error) {
  198. var r durationpb.Duration
  199. val = strconv.Quote(strings.Trim(val, `"`))
  200. unmarshaler := &protojson.UnmarshalOptions{}
  201. if err := unmarshaler.Unmarshal([]byte(val), &r); err != nil {
  202. return nil, err
  203. }
  204. return &r, nil
  205. }
  206. // Enum converts the given string into an int32 that should be type casted into the
  207. // correct enum proto type.
  208. func Enum(val string, enumValMap map[string]int32) (int32, error) {
  209. e, ok := enumValMap[val]
  210. if ok {
  211. return e, nil
  212. }
  213. i, err := Int32(val)
  214. if err != nil {
  215. return 0, fmt.Errorf("%s is not valid", val)
  216. }
  217. for _, v := range enumValMap {
  218. if v == i {
  219. return i, nil
  220. }
  221. }
  222. return 0, fmt.Errorf("%s is not valid", val)
  223. }
  224. // EnumSlice converts 'val' where individual enums are separated by 'sep'
  225. // into a int32 slice. Each individual int32 should be type casted into the
  226. // correct enum proto type.
  227. func EnumSlice(val, sep string, enumValMap map[string]int32) ([]int32, error) {
  228. s := strings.Split(val, sep)
  229. values := make([]int32, len(s))
  230. for i, v := range s {
  231. value, err := Enum(v, enumValMap)
  232. if err != nil {
  233. return nil, err
  234. }
  235. values[i] = value
  236. }
  237. return values, nil
  238. }
  239. // Support for google.protobuf.wrappers on top of primitive types
  240. // StringValue well-known type support as wrapper around string type
  241. func StringValue(val string) (*wrapperspb.StringValue, error) {
  242. return wrapperspb.String(val), nil
  243. }
  244. // FloatValue well-known type support as wrapper around float32 type
  245. func FloatValue(val string) (*wrapperspb.FloatValue, error) {
  246. parsedVal, err := Float32(val)
  247. return wrapperspb.Float(parsedVal), err
  248. }
  249. // DoubleValue well-known type support as wrapper around float64 type
  250. func DoubleValue(val string) (*wrapperspb.DoubleValue, error) {
  251. parsedVal, err := Float64(val)
  252. return wrapperspb.Double(parsedVal), err
  253. }
  254. // BoolValue well-known type support as wrapper around bool type
  255. func BoolValue(val string) (*wrapperspb.BoolValue, error) {
  256. parsedVal, err := Bool(val)
  257. return wrapperspb.Bool(parsedVal), err
  258. }
  259. // Int32Value well-known type support as wrapper around int32 type
  260. func Int32Value(val string) (*wrapperspb.Int32Value, error) {
  261. parsedVal, err := Int32(val)
  262. return wrapperspb.Int32(parsedVal), err
  263. }
  264. // UInt32Value well-known type support as wrapper around uint32 type
  265. func UInt32Value(val string) (*wrapperspb.UInt32Value, error) {
  266. parsedVal, err := Uint32(val)
  267. return wrapperspb.UInt32(parsedVal), err
  268. }
  269. // Int64Value well-known type support as wrapper around int64 type
  270. func Int64Value(val string) (*wrapperspb.Int64Value, error) {
  271. parsedVal, err := Int64(val)
  272. return wrapperspb.Int64(parsedVal), err
  273. }
  274. // UInt64Value well-known type support as wrapper around uint64 type
  275. func UInt64Value(val string) (*wrapperspb.UInt64Value, error) {
  276. parsedVal, err := Uint64(val)
  277. return wrapperspb.UInt64(parsedVal), err
  278. }
  279. // BytesValue well-known type support as wrapper around bytes[] type
  280. func BytesValue(val string) (*wrapperspb.BytesValue, error) {
  281. parsedVal, err := Bytes(val)
  282. return wrapperspb.Bytes(parsedVal), err
  283. }