From 04688ca746d421f076c0ce32dcb14bca0535150d Mon Sep 17 00:00:00 2001 From: Nishant Sharma Date: Mon, 6 Jun 2022 19:42:36 +0530 Subject: [PATCH] Variadic functions with variable number of struct inputs now working. --- _examples/variadic/test.py | 10 +++++++-- _examples/variadic/variadic.go | 39 +++++++++++++++++++++++++++------- bind/gen_func.go | 6 +++++- main_test.go | 5 +++-- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/_examples/variadic/test.py b/_examples/variadic/test.py index ffbd5a5..53bc69b 100644 --- a/_examples/variadic/test.py +++ b/_examples/variadic/test.py @@ -3,11 +3,17 @@ # license that can be found in the LICENSE file. import variadic, go +############### Non Variadic ############## +nonvarResult = variadic.NonVariFunc(1, go.Slice_int([2,3,4]),5) +print("NonVariadic 1+[2+3+4]+5 = %d" % nonvarResult) + +############### Variadic Over Int ############## varResult = variadic.VariFunc(1,2,3,4,5) print("Variadic 1+2+3+4+5 = %d" % varResult) -nonvarResult = variadic.NonVariFunc(1, go.Slice_int([2,3,4]),5) -print("NonVariadic 1+[2+3+4]+5 = %d" % nonvarResult) +############### Variadic Over Struct ############## +varStructResult = variadic.VariStructFunc(variadic.NewIntStrUct(1), variadic.NewIntStrUct(2), variadic.NewIntStrUct(3)) +print("Variadic Struct s(1)+s(2)+s(3) = %d" % varStructResult) if isinstance(varResult, int): print("Type OK") diff --git a/_examples/variadic/variadic.go b/_examples/variadic/variadic.go index 72a02be..103c21d 100644 --- a/_examples/variadic/variadic.go +++ b/_examples/variadic/variadic.go @@ -1,13 +1,6 @@ package variadic -func VariFunc(vargs ...int) int{ - total := 0 - for _, num := range vargs { - total += num - } - return total -} - +/////////////// Non Variadic ////////////// func NonVariFunc(arg1 int, arg2 []int, arg3 int) int{ total := arg1 for _, num := range arg2 { @@ -17,3 +10,33 @@ func NonVariFunc(arg1 int, arg2 []int, arg3 int) int{ return total } + +/////////////// Variadic Over Int ////////////// +func VariFunc(vargs ...int) int{ + total := 0 + for _, num := range vargs { + total += num + } + return total +} + +/////////////// Variadic Over Struct ////////////// +type IntStrUct struct { + p int +} + +func NewIntStrUct(n int) IntStrUct { + return IntStrUct { + p:n, + } +} + +func VariStructFunc(vargs ...IntStrUct) int{ + total := 0 + for _, inst := range vargs { + total += inst.p + } + return total +} + +/////////////// Variadic Over Interface ////////////// diff --git a/bind/gen_func.go b/bind/gen_func.go index aabb7b5..d5518e1 100644 --- a/bind/gen_func.go +++ b/bind/gen_func.go @@ -322,7 +322,11 @@ if __err != nil { // To support variadic args, we add *args at the end. if fsym.isVariadic && i == len(args)-1 { - g.pywrap.Printf("%s = go.Slice_int(args)\n", anm) + packagePrefix := "" + if arg.sym.gopkg.Name() != fsym.pkg.Name() { + packagePrefix = arg.sym.gopkg.Name() + "." + } + g.pywrap.Printf("%s = %s%s(args)\n", anm, packagePrefix, arg.sym.id) } } diff --git a/main_test.go b/main_test.go index b88c02c..9006ffc 100644 --- a/main_test.go +++ b/main_test.go @@ -820,8 +820,9 @@ func TestBindVariadic(t *testing.T) { lang: features[path], cmd: "build", extras: nil, - want: []byte(`Variadic 1+2+3+4+5 = 15 -NonVariadic 1+[2+3+4]+5 = 15 + want: []byte(`NonVariadic 1+[2+3+4]+5 = 15 +Variadic 1+2+3+4+5 = 15 +Variadic Struct s(1)+s(2)+s(3) = 6 Type OK `), })