diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-02-25 09:24:59 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-02-25 19:53:18 +0100 |
commit | ce524d0b5ebaef05d29fa368465f31358f26dcda (patch) | |
tree | e5df54a5deeefacbff4916d3619f85c2cb341b01 /common | |
parent | 2662faf61ff0240be1ee0d6c496b6b4a6ed55fb4 (diff) |
Add a page template func
Fixes #9339
Diffstat (limited to 'common')
-rw-r--r-- | common/hreflect/helpers.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/common/hreflect/helpers.go b/common/hreflect/helpers.go index 1b7e5acf7..17afbf912 100644 --- a/common/hreflect/helpers.go +++ b/common/hreflect/helpers.go @@ -208,6 +208,23 @@ func AsTime(v reflect.Value, loc *time.Location) (time.Time, bool) { return time.Time{}, false } +func CallMethodByName(cxt context.Context, name string, v reflect.Value) []reflect.Value { + fn := v.MethodByName(name) + var args []reflect.Value + tp := fn.Type() + if tp.NumIn() > 0 { + if tp.NumIn() > 1 { + panic("not supported") + } + first := tp.In(0) + if first.Implements(ContextInterface) { + args = append(args, reflect.ValueOf(cxt)) + } + } + + return fn.Call(args) +} + // Based on: https://github.com/golang/go/blob/178a2c42254166cffed1b25fb1d3c7a5727cada6/src/text/template/exec.go#L931 func indirectInterface(v reflect.Value) reflect.Value { if v.Kind() != reflect.Interface { |