diff options
author | Matej Vasek <mvasek@redhat.com> | 2021-02-02 18:52:32 +0100 |
---|---|---|
committer | Matej Vasek <mvasek@redhat.com> | 2021-02-02 19:49:38 +0100 |
commit | 5a746c08f753c2e1227fefdbf9527cddba00e2fa (patch) | |
tree | de10e5ff39588f663c1099f278ae012296c3970b /pkg/bindings/generator | |
parent | d66a18cb11688060a3ef737dd05758398279f053 (diff) | |
download | podman-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/bindings/generator')
-rw-r--r-- | pkg/bindings/generator/generator.go | 41 |
1 files changed, 15 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) } |