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