Skip to content

Commit 6273296

Browse files
committed
fix: support ConstExpr with functions defined via expr.Function
ConstExpr panics when the named function was defined using expr.Function() instead of being a method on the environment struct. This is because ConstExpr only looks in the environment object via runtime.Fetch, not in the Functions table. Check c.Functions first and extract the appropriate function implementation (Func, Fast, or Safe) before falling back to the environment object lookup. Fixes #809
1 parent 3a46b19 commit 6273296

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

conf/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ func (c *Config) WithEnv(env any) {
7474
}
7575

7676
func (c *Config) ConstExpr(name string) {
77+
// Check if the function is defined via expr.Function first.
78+
if fn, ok := c.Functions[name]; ok {
79+
if fn.Func != nil {
80+
c.ConstFns[name] = reflect.ValueOf(fn.Func)
81+
return
82+
}
83+
if fn.Fast != nil {
84+
c.ConstFns[name] = reflect.ValueOf(fn.Fast)
85+
return
86+
}
87+
if fn.Safe != nil {
88+
c.ConstFns[name] = reflect.ValueOf(fn.Safe)
89+
return
90+
}
91+
panic(fmt.Errorf("const expression %q must have a function implementation", name))
92+
}
7793
if c.EnvObject == nil {
7894
panic("no environment is specified for ConstExpr()")
7995
}

0 commit comments

Comments
 (0)