mirror of
https://github.com/containers/podman.git
synced 2026-10-27 04:09:19 -04:00
[NO TESTS NEEDED] Improve generator
Signed-off-by: Matej Vasek <mvasek@redhat.com>
This commit is contained in:
1 parent
d66a18cb11
commit
5a746c08f7
2 files changed
+45
-26
No files matched your search
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in new issue
Block a user