convert.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package runtime
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "github.com/golang/protobuf/jsonpb"
  8. "github.com/golang/protobuf/ptypes/duration"
  9. "github.com/golang/protobuf/ptypes/timestamp"
  10. "github.com/golang/protobuf/ptypes/wrappers"
  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 values, 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 values, 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 values, 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 values, 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 values, 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 values, 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 values, 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 values, 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) (*timestamp.Timestamp, error) {
  188. var r *timestamp.Timestamp
  189. err := jsonpb.UnmarshalString(val, r)
  190. return r, err
  191. }
  192. // Duration converts the given string into a timestamp.Duration.
  193. func Duration(val string) (*duration.Duration, error) {
  194. var r *duration.Duration
  195. err := jsonpb.UnmarshalString(val, r)
  196. return r, err
  197. }
  198. // Enum converts the given string into an int32 that should be type casted into the
  199. // correct enum proto type.
  200. func Enum(val string, enumValMap map[string]int32) (int32, error) {
  201. e, ok := enumValMap[val]
  202. if ok {
  203. return e, nil
  204. }
  205. i, err := Int32(val)
  206. if err != nil {
  207. return 0, fmt.Errorf("%s is not valid", val)
  208. }
  209. for _, v := range enumValMap {
  210. if v == i {
  211. return i, nil
  212. }
  213. }
  214. return 0, fmt.Errorf("%s is not valid", val)
  215. }
  216. // EnumSlice converts 'val' where individual enums are separated by 'sep'
  217. // into a int32 slice. Each individual int32 should be type casted into the
  218. // correct enum proto type.
  219. func EnumSlice(val, sep string, enumValMap map[string]int32) ([]int32, error) {
  220. s := strings.Split(val, sep)
  221. values := make([]int32, len(s))
  222. for i, v := range s {
  223. value, err := Enum(v, enumValMap)
  224. if err != nil {
  225. return values, err
  226. }
  227. values[i] = value
  228. }
  229. return values, nil
  230. }
  231. /*
  232. Support fot google.protobuf.wrappers on top of primitive types
  233. */
  234. // StringValue well-known type support as wrapper around string type
  235. func StringValue(val string) (*wrappers.StringValue, error) {
  236. return &wrappers.StringValue{Value: val}, nil
  237. }
  238. // FloatValue well-known type support as wrapper around float32 type
  239. func FloatValue(val string) (*wrappers.FloatValue, error) {
  240. parsedVal, err := Float32(val)
  241. return &wrappers.FloatValue{Value: parsedVal}, err
  242. }
  243. // DoubleValue well-known type support as wrapper around float64 type
  244. func DoubleValue(val string) (*wrappers.DoubleValue, error) {
  245. parsedVal, err := Float64(val)
  246. return &wrappers.DoubleValue{Value: parsedVal}, err
  247. }
  248. // BoolValue well-known type support as wrapper around bool type
  249. func BoolValue(val string) (*wrappers.BoolValue, error) {
  250. parsedVal, err := Bool(val)
  251. return &wrappers.BoolValue{Value: parsedVal}, err
  252. }
  253. // Int32Value well-known type support as wrapper around int32 type
  254. func Int32Value(val string) (*wrappers.Int32Value, error) {
  255. parsedVal, err := Int32(val)
  256. return &wrappers.Int32Value{Value: parsedVal}, err
  257. }
  258. // UInt32Value well-known type support as wrapper around uint32 type
  259. func UInt32Value(val string) (*wrappers.UInt32Value, error) {
  260. parsedVal, err := Uint32(val)
  261. return &wrappers.UInt32Value{Value: parsedVal}, err
  262. }
  263. // Int64Value well-known type support as wrapper around int64 type
  264. func Int64Value(val string) (*wrappers.Int64Value, error) {
  265. parsedVal, err := Int64(val)
  266. return &wrappers.Int64Value{Value: parsedVal}, err
  267. }
  268. // UInt64Value well-known type support as wrapper around uint64 type
  269. func UInt64Value(val string) (*wrappers.UInt64Value, error) {
  270. parsedVal, err := Uint64(val)
  271. return &wrappers.UInt64Value{Value: parsedVal}, err
  272. }
  273. // BytesValue well-known type support as wrapper around bytes[] type
  274. func BytesValue(val string) (*wrappers.BytesValue, error) {
  275. parsedVal, err := Bytes(val)
  276. return &wrappers.BytesValue{Value: parsedVal}, err
  277. }