lang: funcs: Support for []byte

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
This commit is contained in:
Julien Pivotto
2019-10-30 22:14:08 +01:00
parent 631124e658
commit 0cbfaf98f3
6 changed files with 33 additions and 9 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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

View File

@@ -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"}]

View File

@@ -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 ""

View File

@@ -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))?(, )?){0,})\) (?P<return>(bool|string|int|int64|float64|)|\((bool|string|int|int64|float64), error\))$`)
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\))$`)
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