Skip to content

ValueGetter is not supported inmap #886

@veezhang

Description

@veezhang

ValueGetter is not supported inmap:

https://go.dev/play/p/cutaKAgIxHj

package main

import (
	"fmt"

	"github.com/expr-lang/expr"
	"github.com/expr-lang/expr/patcher/value"
	"github.com/expr-lang/expr/vm"
)

type customInt struct {
	Int int
}

func (v *customInt) AsInt() int {
	return v.Int
}

func (v *customInt) AsAny() any {
	return v.Int
}

func main() {
	env := map[string]any{
		"params": map[string]any{
			"ValueOne": &customInt{1},
			"ValueTwo": &customInt{2},
		},
	}

	program, err := expr.Compile(
		"params.ValueOne + params.ValueTwo",
		expr.Env(env), 
		value.ValueGetter,
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	out, err := vm.Run(program, env)
	if err != nil {
		panic(err)
	}

	fmt.Println(out)
}

The code above produced the following error:

panic: invalid operation: main.customInt + main.customInt (1:17)
	 | params.ValueOne + params.ValueTwo
	 | ................^

goroutine 1 [running]:
main.main()
	/tmp/sandbox3933238168/prog.go:43 +0x249

Program exited.

In my scenario, it's not possible to define a specific struct. Is there any way to solve this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions