diff --git a/lang/funcs/funcgen/config.go b/lang/funcs/funcgen/config.go index f25164bd..fb078e39 100644 --- a/lang/funcs/funcgen/config.go +++ b/lang/funcs/funcgen/config.go @@ -35,7 +35,7 @@ type arg struct { // Value is the value of the argument. Value string `yaml:"value,omitempty"` // Type is the type of the argument. - // Supported: bool, string, int, int64, float64. + // Supported: bool, string, int, int64, float64, []byte. Type string `yaml:"type"` } @@ -57,14 +57,14 @@ func (obj *arg) ToMcl() (string, error) { switch obj.Type { case "bool": return fmt.Sprintf("%s%s", prefix, types.TypeBool.String()), nil - case "string": + case "string", "[]byte": return fmt.Sprintf("%s%s", prefix, types.TypeStr.String()), nil case "int", "int64": return fmt.Sprintf("%s%s", prefix, types.TypeInt.String()), nil case "float64": return fmt.Sprintf("%s%s", prefix, types.TypeFloat.String()), nil default: - return "", fmt.Errorf("cannot convert %v to mcl", obj) + return "", fmt.Errorf("cannot convert %v to mcl", obj.Type) } } @@ -73,7 +73,7 @@ func (obj *arg) ToGolang() (string, error) { switch obj.Type { case "bool": return "Bool", nil - case "string": + case "string", "[]byte": return "Str", nil case "int", "int64": return "Int", nil @@ -89,7 +89,7 @@ func (obj *arg) ToTestInput() (string, error) { switch obj.Type { case "bool": return fmt.Sprintf("&types.BoolValue{V: %s}", obj.Name), nil - case "string": + case "string", "[]byte": return fmt.Sprintf("&types.StrValue{V: %s}", obj.Name), nil case "int": return fmt.Sprintf("&types.IntValue{V: %s}", obj.Name), nil diff --git a/lang/funcs/funcgen/fixtures/func_base.tpl b/lang/funcs/funcgen/fixtures/func_base.tpl index 51135acd..1f824e8c 100644 --- a/lang/funcs/funcgen/fixtures/func_base.tpl +++ b/lang/funcs/funcgen/fixtures/func_base.tpl @@ -45,6 +45,10 @@ func init() { T: types.NewType("func(s float, i int, x int, j int, k int, b bool, t str) str"), V: TestpkgWithInt, }) + simple.ModuleRegister("golang/testpkg", "super_byte", &types.FuncValue{ + T: types.NewType("func(s str, t str) str"), + V: TestpkgSuperByte, + }) } @@ -81,3 +85,9 @@ func TestpkgWithInt(input []types.Value) (types.Value, error) { V: testpkg.WithInt(input[0].Float(), int(input[1].Int()), input[2].Int(), int(input[3].Int()), int(input[4].Int()), input[5].Bool(), input[6].Str()), }, nil } + +func TestpkgSuperByte(input []types.Value) (types.Value, error) { + return &types.StrValue{ + V: string(testpkg.SuperByte([]byte(input[0].Str()), input[1].Str())), + }, nil +} diff --git a/lang/funcs/funcgen/fixtures/func_base.txt b/lang/funcs/funcgen/fixtures/func_base.txt index 70b58dee..fd9f1f88 100644 --- a/lang/funcs/funcgen/fixtures/func_base.txt +++ b/lang/funcs/funcgen/fixtures/func_base.txt @@ -12,3 +12,4 @@ func WithErrorButNothingElse(s string) error func WithNothingElse(s string) func Nextafter32(x, y float32) (r float32) func WithInt(s float64, i int, x int64, j, k int, b bool, t string) string +func SuperByte(s []byte, t string) []byte diff --git a/lang/funcs/funcgen/fixtures/func_base.yaml b/lang/funcs/funcgen/fixtures/func_base.yaml index d80616c7..79a6506b 100644 --- a/lang/funcs/funcgen/fixtures/func_base.yaml +++ b/lang/funcs/funcgen/fixtures/func_base.yaml @@ -40,3 +40,11 @@ errorful: false args: [{name: s, type: float64}, {name: i, type: int}, {name: x, type: int64}, {name: j, type: int}, {name: k, type: int}, {name: b, type: bool}, {name: t, type: string}] return: [{type: string}] +- mgmtPackage: golang/testpkg + mclName: super_byte + internalName: TestpkgSuperByte + golangPackage: *pkg + golangFunc: SuperByte + errorful: false + args: [{name: s, type: "[]byte"}, {name: t, type: string}] + return: [{type: "[]byte"}] diff --git a/lang/funcs/funcgen/func.go b/lang/funcs/funcgen/func.go index ca55d457..dd5dfbc1 100644 --- a/lang/funcs/funcgen/func.go +++ b/lang/funcs/funcgen/func.go @@ -109,8 +109,11 @@ func (obj *function) MakeGolangArgs() (string, error) { return "", err } input := fmt.Sprintf("input[%d].%s()", i, gol) - if a.Type == "int" { + switch a.Type { + case "int": input = fmt.Sprintf("int(%s)", input) + case "[]byte": + input = fmt.Sprintf("[]byte(%s)", input) } args = append(args, input) } @@ -152,6 +155,8 @@ func (obj *function) ConvertStart() string { switch t { case "int": return "int64(" + case "[]byte": + return "string(" default: return "" } @@ -161,7 +166,7 @@ func (obj *function) ConvertStart() string { func (obj *function) ConvertStop() string { t := obj.Return[0].Type switch t { - case "int": + case "int", "[]byte": return ")" default: return "" diff --git a/lang/funcs/funcgen/pkg.go b/lang/funcs/funcgen/pkg.go index 0a15199e..35b4215f 100644 --- a/lang/funcs/funcgen/pkg.go +++ b/lang/funcs/funcgen/pkg.go @@ -33,7 +33,7 @@ import ( ) var ( - validSignature = regexp.MustCompile(`^func (?P[A-Z][a-zA-Z0-9]+)\((?P([a-zA-Z]+( (bool|string|int|int64|float64))?(, )?){0,})\) (?P(bool|string|int|int64|float64|)|\((bool|string|int|int64|float64), error\))$`) + validSignature = regexp.MustCompile(`^func (?P[A-Z][a-zA-Z0-9]+)\((?P([a-zA-Z]+( (bool|string|int|int64|float64|\[\]byte))?(, )?){0,})\) (?P(bool|string|int|int64|float64|\[\]byte|)|\((bool|string|int|int64|float64|\[\]byte), error\))$`) errExcluded = errors.New("function is excluded") ) @@ -205,7 +205,7 @@ func parseArgs(str string) []arg { func parseReturn(str string) []arg { var returns []arg - re := regexp.MustCompile(`(bool|string|int|int64|float64)`) + re := regexp.MustCompile(`(bool|string|int|int64|float64|\[\]byte)`) t := string(re.Find([]byte(str))) returns = append(returns, arg{Type: t}) return returns