Skip to content
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

using reflection, cannot get name of type declared inside Eval'uated code #947

Closed
en-vee opened this issue Nov 5, 2020 · 7 comments
Closed
Labels
area/core bug Something isn't working duplicate This issue or pull request already exists

Comments

@en-vee
Copy link

en-vee commented Nov 5, 2020

Hi,
My aim is to provide a plugin interface for my application where the plugin source code looks like below :

plugin.go

package my

type M1 struct {
    X int
}

func (m *M1) Key() int {
    return 100
}

func F1() (interface{}, error) {
   m1 := &M1{}
   m1.X = 20
   return m1,nil
}

In my application code which reads the above plugin code, I do the following :

package main

type Keyer interface {
     Key() int
}

func main() {
     ip := interp.New(interp.Options{})
     ip.Use(stdlib.Symbols)
     _, err := ip.Eval(src) // Note : src is the contents of the above plugin.go file
     v, err := ip.Eval("my.F1")
     F1 := v.Interface().(func() (interface{}, error))
     m1, err = F1()
     keyer, ok := m1.(Keyer)     
    fmt.Println(ok)     // At this point ok = false
}

Expected result: Expected ok = true as m1 implements the Key() method as shown in plugin.go

Got: ok=false

Also, when I do fmt.Println(reflect.TypeOf(m1)), I just get *struct { X int }

Why do I not get the name *my.M printed using the reflection API ?

I guess if I am able to type-assert on the Keyer interface successfully, even that should solve my problems.

I see great potential in using yaegi as an embedded plugin framework.

Please help me achieve my goal of being able to invoke methods on the type defined in my interpreted/plugin Go source code above.

Or alternatively, any way of determining the plugin/interperted code defined type name using reflection.

@en-vee
Copy link
Author

en-vee commented Nov 6, 2020

Basically, it appears that any reflect.Value which is returned by the interpreter.Eval method does not retain the type information for custom types. It only does so for the ones which are part of stdlib.

@en-vee
Copy link
Author

en-vee commented Nov 6, 2020

Any comments friends ?

@mvertes
Copy link
Member

mvertes commented Nov 9, 2020

Similar as #939.

@en-vee
Copy link
Author

en-vee commented Nov 10, 2020

thank you very much @mvertes
any estimate by when this will be fixed ? or how complex this is ?

@mpl
Copy link
Collaborator

mpl commented Dec 3, 2020

@en-vee btw, aside from the problems of making the interfaces cross boundaries between the evaled code, and the caller, there were some unsupported cases regarding type assertions so far (which have just been fixed).
So what you're trying to do is still hard/not possible for now, but at least it won't fail because of these cases.

@en-vee
Copy link
Author

en-vee commented Dec 6, 2020

@mpl thanks for that. I'll give this a go.

@mvertes mvertes added bug Something isn't working duplicate This issue or pull request already exists area/core labels Mar 22, 2023
@mvertes
Copy link
Member

mvertes commented Mar 22, 2023

closing in favour of #939

@mvertes mvertes closed this as completed Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants