Skip to content

Conversation

@thevilledev
Copy link
Contributor

Previously, passing a pointer to a map (e.g. *map[string]any) to expr.Env() would cause a panic because reflection methods were called on the pointer value instead of the underlying map. With this change the value is first deref'd in the config before any map operations are performed.

Consequently, the vm.Run() method needs to dereference the env map. This ensures compatibility with the optimisations like OpLoadFast where the compiler determines the environment simply as map[string]any.

Regression test added.

Fixes #825.

Previously, passing a pointer to a map (e.g. `*map[string]any`)
to `expr.Env()` would cause a panic because reflection methods
were called on the pointer value instead of the underlying map.
With this change the value is first deref'd in the config before
any map operations are performed.

Consequently, the `vm.Run()` method needs to dereference the env
map. This ensures compatibility with the optimisations like
`OpLoadFast` where the compiler determines the environment simply
as `map[string]any`.

Regression test added.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
@antonmedv
Copy link
Member

But why would *map[string]any pointer to map is needed?


func (vm *VM) Run(program *Program, env any) (_ any, err error) {
if m, ok := env.(*map[string]any); ok && m != nil {
env = *m
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Expr sloud allow to types as incoming env: struct, *struct and map[string]...

@thevilledev
Copy link
Contributor Author

But why would *map[string]any pointer to map is needed?

I guess this would mostly be a quality of life improvement. Tried to think of an example. Maybe you unmarshal the env from JSON and compile a rule, and you use a reference to the map by thinking "well why not, I have it right here". One possible example (which panics): https://go.dev/play/p/WsuJJtq-DwL

Also @yawnBright feel free to chime in with your use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic when using pointer-type map as environment

2 participants