fix: correct range on arrays of interface objects

This commit is contained in:
Marc Vertes
2020-07-23 12:25:04 +02:00
committed by GitHub
parent 589b2a0cd2
commit 9c51f6bb69
2 changed files with 66 additions and 8 deletions

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

@@ -0,0 +1,39 @@
package main
import "fmt"
// Filter is a filter interface
type Filter interface {
Bounds(srcBounds string) (dstBounds string)
}
// GIFT type
type GIFT struct {
Filters []Filter
}
// New creates a new filter list and initializes it with the given slice of filters.
func New(filters ...Filter) *GIFT {
return &GIFT{
Filters: filters,
}
}
// Bounds calculates the appropriate bounds for the result image after applying all the added filters.
func (g *GIFT) Bounds(srcBounds string) (dstBounds string) {
dstBounds = srcBounds
for _, f := range g.Filters {
dstBounds = f.Bounds(dstBounds)
}
return dstBounds
}
func main() {
var filters []Filter
bounds := "foo"
g := New(filters...)
fmt.Println(g.Bounds(bounds))
}
// Output:
// foo

View File

@@ -155,17 +155,36 @@ func genValueArray(n *node) func(*frame) reflect.Value {
func genValueRangeArray(n *node) func(*frame) reflect.Value {
value := genValue(n)
// dereference array pointer, to support array operations on array pointer
if n.typ.TypeOf().Kind() == reflect.Ptr {
switch {
case n.typ.TypeOf().Kind() == reflect.Ptr:
// dereference array pointer, to support array operations on array pointer
return func(f *frame) reflect.Value {
return value(f).Elem()
}
}
return func(f *frame) reflect.Value {
// This is necessary to prevent changes in the returned
// reflect.Value being reflected back to the value used
// for the range expression.
return reflect.ValueOf(value(f).Interface())
case n.typ.val.cat == interfaceT:
return func(f *frame) reflect.Value {
val := value(f)
v := []valueInterface{}
for i := 0; i < val.Len(); i++ {
switch av := val.Index(i).Interface().(type) {
case []valueInterface:
v = append(v, av...)
case valueInterface:
v = append(v, av)
default:
panic(n.cfgErrorf("invalid type %v", val.Index(i).Type()))
}
}
return reflect.ValueOf(v)
}
default:
return func(f *frame) reflect.Value {
// This is necessary to prevent changes in the returned
// reflect.Value being reflected back to the value used
// for the range expression.
return reflect.ValueOf(value(f).Interface())
}
}
}