summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorMatej Vasek <mvasek@redhat.com>2021-02-02 18:52:32 +0100
committerMatej Vasek <mvasek@redhat.com>2021-02-02 19:49:38 +0100
commit5a746c08f753c2e1227fefdbf9527cddba00e2fa (patch)
treede10e5ff39588f663c1099f278ae012296c3970b /pkg
parentd66a18cb11688060a3ef737dd05758398279f053 (diff)
downloadpodman-5a746c08f753c2e1227fefdbf9527cddba00e2fa.tar.gz
podman-5a746c08f753c2e1227fefdbf9527cddba00e2fa.tar.bz2
podman-5a746c08f753c2e1227fefdbf9527cddba00e2fa.zip
[NO TESTS NEEDED] Improve generator
Signed-off-by: Matej Vasek <mvasek@redhat.com>
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bindings/generator/generator.go41
-rw-r--r--pkg/bindings/util/util.go30
2 files changed, 45 insertions, 26 deletions
diff --git a/pkg/bindings/generator/generator.go b/pkg/bindings/generator/generator.go
index 6a7f600a8..5a5cabe53 100644
--- a/pkg/bindings/generator/generator.go
+++ b/pkg/bindings/generator/generator.go
@@ -54,33 +54,19 @@ func (o *{{.StructName}}) ToParams() (url.Values, error) {
if reflect.Ptr == f.Kind() {
f = f.Elem()
}
- switch f.Kind() {
- case reflect.Bool:
- params.Set(fieldName, strconv.FormatBool(f.Bool()))
- case reflect.String:
- params.Set(fieldName, f.String())
- case reflect.Int, reflect.Int64:
- // f.Int() is always an int64
- params.Set(fieldName, strconv.FormatInt(f.Int(), 10))
- case reflect.Uint, reflect.Uint64:
- // f.Uint() is always an uint64
- params.Set(fieldName, strconv.FormatUint(f.Uint(), 10))
- case reflect.Slice:
- typ := reflect.TypeOf(f.Interface()).Elem()
- switch typ.Kind() {
- case reflect.String:
- sl := f.Slice(0, f.Len())
- s, ok := sl.Interface().([]string)
- if !ok {
- return nil, errors.New("failed to convert to string slice")
+ switch {
+ case util.IsSimpleType(f):
+ params.Set(fieldName, util.SimpleTypeToParam(f))
+ case f.Kind() == reflect.Slice:
+ for i := 0; i < f.Len(); i++ {
+ elem := f.Index(i)
+ if util.IsSimpleType(elem) {
+ params.Add(fieldName, util.SimpleTypeToParam(elem))
+ } else {
+ return nil, errors.New("slices must contain only simple types")
}
- for _, val := range s {
- params.Add(fieldName, val)
- }
- default:
- return nil, errors.Errorf("unknown slice type %s", f.Kind().String())
}
- case reflect.Map:
+ case f.Kind() == reflect.Map:
lowerCaseKeys := make(map[string][]string)
iter := f.MapRange()
for iter.Next() {
@@ -93,7 +79,10 @@ func (o *{{.StructName}}) ToParams() (url.Values, error) {
}
params.Set(fieldName, s)
+ default:
+ panic(fmt.Sprintf("don't known how to handle %s", f.Type().String()))
}
+
}
return params, nil
}
@@ -144,7 +133,7 @@ func main() {
panic(err)
}
// always add reflect
- imports := []string{"\"reflect\""}
+ imports := []string{"\"reflect\"", "\"github.com/containers/podman/v2/pkg/bindings/util\""}
for _, imp := range f.Imports {
imports = append(imports, imp.Path.Value)
}
diff --git a/pkg/bindings/util/util.go b/pkg/bindings/util/util.go
new file mode 100644
index 000000000..403846355
--- /dev/null
+++ b/pkg/bindings/util/util.go
@@ -0,0 +1,30 @@
+package util
+
+import (
+ "reflect"
+ "strconv"
+)
+
+func IsSimpleType(f reflect.Value) bool {
+ switch f.Kind() {
+ case reflect.Bool, reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.String:
+ return true
+ }
+ return false
+}
+
+func SimpleTypeToParam(f reflect.Value) string {
+ switch f.Kind() {
+ case reflect.Bool:
+ return strconv.FormatBool(f.Bool())
+ case reflect.Int, reflect.Int64:
+ // f.Int() is always an int64
+ return strconv.FormatInt(f.Int(), 10)
+ case reflect.Uint, reflect.Uint64:
+ // f.Uint() is always an uint64
+ return strconv.FormatUint(f.Uint(), 10)
+ case reflect.String:
+ return f.String()
+ }
+ panic("the input parameter is not a simple type")
+}