9 "github.com/golang/protobuf/jsonpb"
10 "github.com/golang/protobuf/ptypes/duration"
11 "github.com/golang/protobuf/ptypes/timestamp"
12 "github.com/golang/protobuf/ptypes/wrappers"
15 // String just returns the given string.
16 // It is just for compatibility to other types.
17 func String(val string) (string, error) {
21 // StringSlice converts 'val' where individual strings are separated by
22 // 'sep' into a string slice.
23 func StringSlice(val, sep string) ([]string, error) {
24 return strings.Split(val, sep), nil
27 // Bool converts the given string representation of a boolean value into bool.
28 func Bool(val string) (bool, error) {
29 return strconv.ParseBool(val)
32 // BoolSlice converts 'val' where individual booleans are separated by
33 // 'sep' into a bool slice.
34 func BoolSlice(val, sep string) ([]bool, error) {
35 s := strings.Split(val, sep)
36 values := make([]bool, len(s))
47 // Float64 converts the given string representation into representation of a floating point number into float64.
48 func Float64(val string) (float64, error) {
49 return strconv.ParseFloat(val, 64)
52 // Float64Slice converts 'val' where individual floating point numbers are separated by
53 // 'sep' into a float64 slice.
54 func Float64Slice(val, sep string) ([]float64, error) {
55 s := strings.Split(val, sep)
56 values := make([]float64, len(s))
58 value, err := Float64(v)
67 // Float32 converts the given string representation of a floating point number into float32.
68 func Float32(val string) (float32, error) {
69 f, err := strconv.ParseFloat(val, 32)
73 return float32(f), nil
76 // Float32Slice converts 'val' where individual floating point numbers are separated by
77 // 'sep' into a float32 slice.
78 func Float32Slice(val, sep string) ([]float32, error) {
79 s := strings.Split(val, sep)
80 values := make([]float32, len(s))
82 value, err := Float32(v)
91 // Int64 converts the given string representation of an integer into int64.
92 func Int64(val string) (int64, error) {
93 return strconv.ParseInt(val, 0, 64)
96 // Int64Slice converts 'val' where individual integers are separated by
97 // 'sep' into a int64 slice.
98 func Int64Slice(val, sep string) ([]int64, error) {
99 s := strings.Split(val, sep)
100 values := make([]int64, len(s))
101 for i, v := range s {
102 value, err := Int64(v)
111 // Int32 converts the given string representation of an integer into int32.
112 func Int32(val string) (int32, error) {
113 i, err := strconv.ParseInt(val, 0, 32)
120 // Int32Slice converts 'val' where individual integers are separated by
121 // 'sep' into a int32 slice.
122 func Int32Slice(val, sep string) ([]int32, error) {
123 s := strings.Split(val, sep)
124 values := make([]int32, len(s))
125 for i, v := range s {
126 value, err := Int32(v)
135 // Uint64 converts the given string representation of an integer into uint64.
136 func Uint64(val string) (uint64, error) {
137 return strconv.ParseUint(val, 0, 64)
140 // Uint64Slice converts 'val' where individual integers are separated by
141 // 'sep' into a uint64 slice.
142 func Uint64Slice(val, sep string) ([]uint64, error) {
143 s := strings.Split(val, sep)
144 values := make([]uint64, len(s))
145 for i, v := range s {
146 value, err := Uint64(v)
155 // Uint32 converts the given string representation of an integer into uint32.
156 func Uint32(val string) (uint32, error) {
157 i, err := strconv.ParseUint(val, 0, 32)
161 return uint32(i), nil
164 // Uint32Slice converts 'val' where individual integers are separated by
165 // 'sep' into a uint32 slice.
166 func Uint32Slice(val, sep string) ([]uint32, error) {
167 s := strings.Split(val, sep)
168 values := make([]uint32, len(s))
169 for i, v := range s {
170 value, err := Uint32(v)
179 // Bytes converts the given string representation of a byte sequence into a slice of bytes
180 // A bytes sequence is encoded in URL-safe base64 without padding
181 func Bytes(val string) ([]byte, error) {
182 b, err := base64.StdEncoding.DecodeString(val)
184 b, err = base64.URLEncoding.DecodeString(val)
192 // BytesSlice converts 'val' where individual bytes sequences, encoded in URL-safe
193 // base64 without padding, are separated by 'sep' into a slice of bytes slices slice.
194 func BytesSlice(val, sep string) ([][]byte, error) {
195 s := strings.Split(val, sep)
196 values := make([][]byte, len(s))
197 for i, v := range s {
198 value, err := Bytes(v)
207 // Timestamp converts the given RFC3339 formatted string into a timestamp.Timestamp.
208 func Timestamp(val string) (*timestamp.Timestamp, error) {
209 var r *timestamp.Timestamp
210 err := jsonpb.UnmarshalString(val, r)
214 // Duration converts the given string into a timestamp.Duration.
215 func Duration(val string) (*duration.Duration, error) {
216 var r *duration.Duration
217 err := jsonpb.UnmarshalString(val, r)
221 // Enum converts the given string into an int32 that should be type casted into the
222 // correct enum proto type.
223 func Enum(val string, enumValMap map[string]int32) (int32, error) {
224 e, ok := enumValMap[val]
231 return 0, fmt.Errorf("%s is not valid", val)
233 for _, v := range enumValMap {
238 return 0, fmt.Errorf("%s is not valid", val)
241 // EnumSlice converts 'val' where individual enums are separated by 'sep'
242 // into a int32 slice. Each individual int32 should be type casted into the
243 // correct enum proto type.
244 func EnumSlice(val, sep string, enumValMap map[string]int32) ([]int32, error) {
245 s := strings.Split(val, sep)
246 values := make([]int32, len(s))
247 for i, v := range s {
248 value, err := Enum(v, enumValMap)
258 Support fot google.protobuf.wrappers on top of primitive types
261 // StringValue well-known type support as wrapper around string type
262 func StringValue(val string) (*wrappers.StringValue, error) {
263 return &wrappers.StringValue{Value: val}, nil
266 // FloatValue well-known type support as wrapper around float32 type
267 func FloatValue(val string) (*wrappers.FloatValue, error) {
268 parsedVal, err := Float32(val)
269 return &wrappers.FloatValue{Value: parsedVal}, err
272 // DoubleValue well-known type support as wrapper around float64 type
273 func DoubleValue(val string) (*wrappers.DoubleValue, error) {
274 parsedVal, err := Float64(val)
275 return &wrappers.DoubleValue{Value: parsedVal}, err
278 // BoolValue well-known type support as wrapper around bool type
279 func BoolValue(val string) (*wrappers.BoolValue, error) {
280 parsedVal, err := Bool(val)
281 return &wrappers.BoolValue{Value: parsedVal}, err
284 // Int32Value well-known type support as wrapper around int32 type
285 func Int32Value(val string) (*wrappers.Int32Value, error) {
286 parsedVal, err := Int32(val)
287 return &wrappers.Int32Value{Value: parsedVal}, err
290 // UInt32Value well-known type support as wrapper around uint32 type
291 func UInt32Value(val string) (*wrappers.UInt32Value, error) {
292 parsedVal, err := Uint32(val)
293 return &wrappers.UInt32Value{Value: parsedVal}, err
296 // Int64Value well-known type support as wrapper around int64 type
297 func Int64Value(val string) (*wrappers.Int64Value, error) {
298 parsedVal, err := Int64(val)
299 return &wrappers.Int64Value{Value: parsedVal}, err
302 // UInt64Value well-known type support as wrapper around uint64 type
303 func UInt64Value(val string) (*wrappers.UInt64Value, error) {
304 parsedVal, err := Uint64(val)
305 return &wrappers.UInt64Value{Value: parsedVal}, err
308 // BytesValue well-known type support as wrapper around bytes[] type
309 func BytesValue(val string) (*wrappers.BytesValue, error) {
310 parsedVal, err := Bytes(val)
311 return &wrappers.BytesValue{Value: parsedVal}, err