Skip to content

Commit

Permalink
extract: process interface wrapper method with variadic parameter
Browse files Browse the repository at this point in the history
Fixes #1055
  • Loading branch information
For-ACGN authored Mar 24, 2021
1 parent 84ad467 commit 451c754
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 51 deletions.
10 changes: 9 additions & 1 deletion extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
if args[j] = v.Name(); args[j] == "" {
args[j] = fmt.Sprintf("a%d", j)
}
params[j] = args[j] + " " + types.TypeString(v.Type(), qualify)
// process interface method variadic parameter
if sign.Variadic() && j == len(args)-1 { // check is last arg
// only replace the first "[]" to "..."
at := types.TypeString(v.Type(), qualify)[2:]
params[j] = args[j] + " ..." + at
args[j] += "..."
} else {
params[j] = args[j] + " " + types.TypeString(v.Type(), qualify)
}
}
arg := "(" + strings.Join(args, ", ") + ")"
param := "(" + strings.Join(params, ", ") + ")"
Expand Down
35 changes: 35 additions & 0 deletions extract/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,41 @@ func TestPackages(t *testing.T) {
importPath: "guthib.com/baz",
expected: expectedOutput,
},
{
desc: "using relative path, interface method parameter is variadic",
wd: "./testdata/7/src/guthib.com/variadic",
arg: "../variadic",
importPath: "guthib.com/variadic",
expected: `
// Code generated by 'yaegi extract guthib.com/variadic'. DO NOT EDIT.
package variadic
import (
"guthib.com/variadic"
"reflect"
)
func init() {
Symbols["guthib.com/variadic"] = map[string]reflect.Value{
// type definitions
"Variadic": reflect.ValueOf((*variadic.Variadic)(nil)),
// interface wrapper definitions
"_Variadic": reflect.ValueOf((*_guthib_com_variadic_Variadic)(nil)),
}
}
// _guthib_com_variadic_Variadic is an interface wrapper for Variadic type
type _guthib_com_variadic_Variadic struct {
WCall func(method string, args ...[]interface{}) (interface{}, error)
}
func (W _guthib_com_variadic_Variadic) Call(method string, args ...[]interface{}) (interface{}, error) {
return W.WCall(method, args...)
}
`[1:],
},
}

for _, test := range testCases {
Expand Down
1 change: 1 addition & 0 deletions extract/testdata/7/src/guthib.com/variadic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module guthib.com/baz-baz/variadic
5 changes: 5 additions & 0 deletions extract/testdata/7/src/guthib.com/variadic/variadic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package variadic

type Variadic interface {
Call(method string, args ...[]interface{}) (interface{}, error)
}
50 changes: 25 additions & 25 deletions stdlib/go1_15_testing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions stdlib/go1_16_testing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 451c754

Please sign in to comment.