lang: funcs: funcgen: Allow []string as inputs

This allows us to have the strings.Join function generated.
This commit is contained in:
James Shubin
2023-11-25 20:57:34 -05:00
parent ce2f7112a3
commit af1c952700
6 changed files with 76 additions and 6 deletions

View File

@@ -19,6 +19,7 @@ package main
import (
"fmt"
"math/bits"
"github.com/purpleidea/mgmt/lang/types"
)
@@ -63,6 +64,8 @@ func (obj *arg) ToMcl() (string, error) {
return fmt.Sprintf("%s%s", prefix, types.TypeInt.String()), nil
case "float64":
return fmt.Sprintf("%s%s", prefix, types.TypeFloat.String()), nil
case "[]string":
return fmt.Sprintf("%s%s", prefix, types.NewType("[]str").String()), nil
default:
return "", fmt.Errorf("cannot convert %v to mcl", obj.Type)
}
@@ -87,6 +90,37 @@ func (obj *arg) OldToGolang() (string, error) {
}
}
// ToGolang prints the arg signature as expected by golang.
func (obj *arg) ToGolang(val string) (string, error) {
switch obj.Type {
case "bool":
return fmt.Sprintf("%s.Bool()", val), nil
case "string", "[]byte":
return fmt.Sprintf("%s.Str()", val), nil
case "int":
// TODO: consider switching types.Value int64 to int everywhere
if bits.UintSize == 32 { // special case for 32 bit golang
return fmt.Sprintf("int(%s.Int())", val), nil
}
fallthrough
case "int64":
return fmt.Sprintf("%s.Int()", val), nil
case "float64":
return fmt.Sprintf("%s.Float()", val), nil
case "[]string":
// This function is in the child util package and is imported by
// the template.
return fmt.Sprintf("util.MclListToGolang(%s)", val), nil
default:
return "", fmt.Errorf("cannot convert %v to golang", obj)
}
}
// ToTestInput prints the arg signature as expected by tests.
func (obj *arg) ToTestInput() (string, error) {
switch obj.Type {