fix: compositeSparse handles fields of interface kind

Fixes #776.
This commit is contained in:
Marc Vertes
2020-07-13 17:55:04 +02:00
committed by GitHub
parent 0a79069dfc
commit 5eecbe515b
3 changed files with 47 additions and 0 deletions

39
_test/issue-776.go Normal file
View File

@@ -0,0 +1,39 @@
package main
import "fmt"
// Filter is a filter
type Filter interface {
Foo()
}
// GIFT is a gift
type GIFT struct {
Filters []Filter
}
// New is a new filter list
func New(filters ...Filter) *GIFT {
return &GIFT{
Filters: filters,
}
}
// List lists filters
func (g *GIFT) List() {
fmt.Printf("Hello from List!\n")
}
// MyFilter is one of the filters
type MyFilter struct{}
// Foo is a foo
func (f *MyFilter) Foo() {}
func main() {
g := New(&MyFilter{})
g.List()
}
// Output:
// Hello from List!

View File

@@ -2116,6 +2116,10 @@ func doCompositeSparse(n *node, hasType bool) {
switch {
case c1.typ.cat == funcT:
values[field] = genFunctionWrapper(c1)
case c1.typ.cat == interfaceT:
values[field] = genValueInterfaceValue(c1)
case isArray(c1.typ) && c1.typ.val != nil && c1.typ.val.cat == interfaceT:
values[field] = genValueInterfaceArray(c1)
case isRecursiveType(n.typ.field[field].typ, n.typ.field[field].typ.rtype):
values[field] = genValueRecursiveInterface(c1, n.typ.field[field].typ.rtype)
default:

View File

@@ -1541,6 +1541,10 @@ func isSendChan(t *itype) bool {
return rt.Kind() == reflect.Chan && rt.ChanDir() == reflect.SendDir
}
func isMap(t *itype) bool { return t.TypeOf().Kind() == reflect.Map }
func isArray(t *itype) bool {
k := t.TypeOf().Kind()
return k == reflect.Array || k == reflect.Slice
}
func isInterfaceSrc(t *itype) bool {
return t.cat == interfaceT || (t.cat == aliasT && isInterfaceSrc(t.val))