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 ( import (
"fmt" "fmt"
"math/bits"
"github.com/purpleidea/mgmt/lang/types" "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 return fmt.Sprintf("%s%s", prefix, types.TypeInt.String()), nil
case "float64": case "float64":
return fmt.Sprintf("%s%s", prefix, types.TypeFloat.String()), nil return fmt.Sprintf("%s%s", prefix, types.TypeFloat.String()), nil
case "[]string":
return fmt.Sprintf("%s%s", prefix, types.NewType("[]str").String()), nil
default: default:
return "", fmt.Errorf("cannot convert %v to mcl", obj.Type) 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. // ToTestInput prints the arg signature as expected by tests.
func (obj *arg) ToTestInput() (string, error) { func (obj *arg) ToTestInput() (string, error) {
switch obj.Type { switch obj.Type {

View File

@@ -20,6 +20,7 @@ package core
import ( import (
"testpkg" "testpkg"
"github.com/purpleidea/mgmt/lang/funcs/funcgen/util"
"github.com/purpleidea/mgmt/lang/funcs/simple" "github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
) )

View File

@@ -101,18 +101,19 @@ func generateTemplate(c config, f functions, path, templateFile, finalName strin
func (obj *function) MakeGolangArgs() (string, error) { func (obj *function) MakeGolangArgs() (string, error) {
var args []string var args []string
for i, a := range obj.Args { for i, a := range obj.Args {
gol, err := a.ToGolang() input := fmt.Sprintf("input[%d]", i)
gol, err := a.ToGolang(input)
if err != nil { if err != nil {
return "", err return "", err
} }
input := fmt.Sprintf("input[%d].%s()", i, gol)
switch a.Type { switch a.Type {
case "int": case "int":
input = fmt.Sprintf("int(%s)", input) gol = fmt.Sprintf("int(%s)", gol)
case "[]byte": case "[]byte":
input = fmt.Sprintf("[]byte(%s)", input) gol = fmt.Sprintf("[]byte(%s)", gol)
} }
args = append(args, input) args = append(args, gol)
} }
for _, a := range obj.ExtraGolangArgs { for _, a := range obj.ExtraGolangArgs {
args = append(args, a.Value) args = append(args, a.Value)

View File

@@ -33,7 +33,7 @@ import (
) )
var ( var (
validSignature = regexp.MustCompile(`^func (?P<name>[A-Z][a-zA-Z0-9]+)\((?P<args>([a-zA-Z]+( (bool|string|int|int64|float64|\[\]byte))?(, )?){0,})\) (?P<return>(bool|string|int|int64|float64|\[\]byte|)|\((bool|string|int|int64|float64|\[\]byte), error\))$`) validSignature = regexp.MustCompile(`^func (?P<name>[A-Z][a-zA-Z0-9]+)\((?P<args>([a-zA-Z]+( (bool|string|int|int64|float64|\[\]byte|\[\]string))?(, )?){0,})\) (?P<return>(bool|string|int|int64|float64|\[\]byte|)|\((bool|string|int|int64|float64|\[\]byte|), error\))$`)
errExcluded = errors.New("function is excluded") errExcluded = errors.New("function is excluded")
) )

View File

@@ -20,6 +20,7 @@ package core
import ( import (
{{ range $i, $func := .Packages }} {{ if not (eq .Alias "") }}{{.Alias}} {{end}}"{{.Name}}" {{ range $i, $func := .Packages }} {{ if not (eq .Alias "") }}{{.Alias}} {{end}}"{{.Name}}"
{{ end }} {{ end }}
"github.com/purpleidea/mgmt/lang/funcs/funcgen/util"
"github.com/purpleidea/mgmt/lang/funcs/simple" "github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
) )

View File

@@ -0,0 +1,33 @@
// Mgmt
// Copyright (C) 2013-2023+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package util provides some functions to be imported by the generated file.
package util
import (
"github.com/purpleidea/mgmt/lang/types"
)
// MclListToGolang is a helper function that converts an mcl []str to the golang
// equivalent of []string. This is imported by the generated functions.
func MclListToGolang(val types.Value) []string {
ret := []string{}
for _, x := range val.List() {
ret = append(ret, x.Str())
}
return ret
}