Fix field check for method calls
This commit is contained in:
parent
0040828d2d
commit
110f050c1c
16
salix.go
16
salix.go
@ -495,10 +495,18 @@ func (t *Template) execMethodCall(mc ast.MethodCall, local map[string]any) (any,
|
|||||||
if mtd.IsValid() {
|
if mtd.IsValid() {
|
||||||
return t.execFunc(mtd, mc, mc.Params, local)
|
return t.execFunc(mtd, mc, mc.Params, local)
|
||||||
}
|
}
|
||||||
// If the method doesn't exist, also check for a field storing a function.
|
// If the method doesn't exist, we need to check for fields, so dereference any pointers
|
||||||
field := rval.FieldByName(mc.Name.Value)
|
// because pointers can't have fields
|
||||||
if field.IsValid() && field.Kind() == reflect.Func {
|
for rval.Kind() == reflect.Pointer {
|
||||||
return t.execFunc(field, mc, mc.Params, local)
|
rval = rval.Elem()
|
||||||
|
}
|
||||||
|
// Make sure we actually have a struct
|
||||||
|
if rval.Kind() == reflect.Struct {
|
||||||
|
// If the method doesn't exist, also check for a field storing a function.
|
||||||
|
field := rval.FieldByName(mc.Name.Value)
|
||||||
|
if field.IsValid() && field.Kind() == reflect.Func {
|
||||||
|
return t.execFunc(field, mc, mc.Params, local)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// If neither of those exist, return an error
|
// If neither of those exist, return an error
|
||||||
return nil, ast.PosError(mc, "no such method: %s", mc.Name.Value)
|
return nil, ast.PosError(mc, "no such method: %s", mc.Name.Value)
|
||||||
|
Loading…
Reference in New Issue
Block a user