forked from dynport/gocloud
/
generic.go
49 lines (44 loc) · 886 Bytes
/
generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package aws
import (
"fmt"
"net/url"
"reflect"
)
// beging marker types
type Version struct {
}
type Action struct {
}
// end marker types
func ParamsForAction(i interface{}) (url.Values, error) {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
t := v.Type()
values := url.Values{}
for i := 0; i < v.NumField(); i++ {
fieldType := t.Field(i)
awsTag := fieldType.Tag.Get("aws")
if awsTag != "" {
name := awsTag
switch casted := v.Field(i).Interface().(type) {
case int:
if casted > 0 {
values.Add(name, fmt.Sprintf("%d", casted))
}
case string:
if casted != "" {
values.Add(name, casted)
}
case Version:
values.Add("Version", awsTag)
case Action:
values.Add("Action", awsTag)
default:
return nil, fmt.Errorf("unable to handle %s (%T)", casted, casted)
}
}
}
return values, nil
}