-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not import github.com/apache/thrift/lib/go/thrift
#935
Comments
According to discussion in golang/go#15924 reflect.StructOf might be taught to create wrapper methods for "special cases that allow reusing the existing methods of embedded fields" which is exactly the special case that yaegi generally runs into. |
Recently I learned about the github.com/cosmos72/gomacro/xreflect package that works around many limitations in reflect. Unsuccessfully, I tried using it to work around the "reflect: embedded type with methods not implemented if there is more than one field" error as follows. It has helped me import other packages that otherwise ran into similar errors, but no luck with thrift. diff --git a/interp/type.go b/interp/type.go
index a2c9b6c..39dd51d 100644
--- a/interp/type.go
+++ b/interp/type.go
@@ -7,6 +7,7 @@ import (
"reflect"
"strconv"
"sync"
+ "github.com/cosmos72/gomacro/xreflect"
)
// tcat defines interpreter type categories.
@@ -1494,7 +1495,7 @@ func (t *itype) refType(defined map[string]*itype, wrapRecursive bool) reflect.T
if recursive && wrapRecursive {
t.rtype = interf
} else {
- t.rtype = reflect.StructOf(fields)
+ t.rtype = xStructOf(fields)
}
default:
if z, _ := t.zero(); z.IsValid() {
@@ -1504,6 +1505,28 @@ func (t *itype) refType(defined map[string]*itype, wrapRecursive bool) reflect.T
return t.rtype
}
+func xStructOf(fields []reflect.StructField) (t reflect.Type) {
+ // Handle reflect.StructOf panic by trying with xreflect
+ defer func() {
+ if r := recover(); r != nil {
+ universe := xreflect.NewUniverse()
+ var xfields []xreflect.StructField
+ for _, field := range fields {
+ xfield := xreflect.StructField{
+ Name: field.Name,
+ Type: universe.FromReflectType(field.Type),
+ Tag: field.Tag,
+ Anonymous: field.Anonymous,
+ }
+ xfields = append(xfields, xfield)
+ }
+ t = universe.StructOf(xfields).ReflectType()
+ }
+ }()
+ t = reflect.StructOf(fields)
+ return
+}
+
// TypeOf returns the reflection type of dynamic interpreter type t.
func (t *itype) TypeOf() reflect.Type {
return t.refType(map[string]*itype{}, false) Unfortunately, running the sample program with this patch then runs into another error: $ yaegi -syscall ./thrift_script.go
run: ./thrift_script.go:5:2: import "github.com/apache/thrift/lib/go/thrift" error: /go/src/workspace/src/vendor/github.com/apache/thrift/lib/go/thrift/binary_protocol.go:218:25: too many arguments Note: I have absolutely no idea if this is the correct way to use xreflect. |
I got an error at traefik/traefik#9114
which might also related with this one. |
looks like this is the same issue... if I vendor aws-go-v2 SDK I get this
$ yaegi version |
This comment was marked as off-topic.
This comment was marked as off-topic.
Getting the same issue using aws-go-v2 SDK
|
I still encounter the same issue when I import "github.com/bytedance/sonic" in my plugins.
|
The following program
thrift_script.go
triggers a panic:Expected result:
Got:
Seems that it triggers an unsupported path of reflect.go
The text was updated successfully, but these errors were encountered: