lang: funcs: funcgen: Allow []string as inputs
This allows us to have the strings.Join function generated.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -20,6 +20,7 @@ package core
|
||||
import (
|
||||
"testpkg"
|
||||
|
||||
"github.com/purpleidea/mgmt/lang/funcs/funcgen/util"
|
||||
"github.com/purpleidea/mgmt/lang/funcs/simple"
|
||||
"github.com/purpleidea/mgmt/lang/types"
|
||||
)
|
||||
|
||||
@@ -101,18 +101,19 @@ func generateTemplate(c config, f functions, path, templateFile, finalName strin
|
||||
func (obj *function) MakeGolangArgs() (string, error) {
|
||||
var args []string
|
||||
for i, a := range obj.Args {
|
||||
gol, err := a.ToGolang()
|
||||
input := fmt.Sprintf("input[%d]", i)
|
||||
gol, err := a.ToGolang(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
input := fmt.Sprintf("input[%d].%s()", i, gol)
|
||||
|
||||
switch a.Type {
|
||||
case "int":
|
||||
input = fmt.Sprintf("int(%s)", input)
|
||||
gol = fmt.Sprintf("int(%s)", gol)
|
||||
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 {
|
||||
args = append(args, a.Value)
|
||||
|
||||
@@ -33,7 +33,7 @@ import (
|
||||
)
|
||||
|
||||
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")
|
||||
)
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ package core
|
||||
import (
|
||||
{{ range $i, $func := .Packages }} {{ if not (eq .Alias "") }}{{.Alias}} {{end}}"{{.Name}}"
|
||||
{{ end }}
|
||||
"github.com/purpleidea/mgmt/lang/funcs/funcgen/util"
|
||||
"github.com/purpleidea/mgmt/lang/funcs/simple"
|
||||
"github.com/purpleidea/mgmt/lang/types"
|
||||
)
|
||||
|
||||
33
lang/funcs/funcgen/util/util.go
Normal file
33
lang/funcs/funcgen/util/util.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user