github.com/expr-lang/expr@v1.16.9/patcher/value/value_example_test.go (about)

     1  package value_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/expr-lang/expr"
     7  	"github.com/expr-lang/expr/patcher/value"
     8  	"github.com/expr-lang/expr/vm"
     9  )
    10  
    11  type myInt struct {
    12  	Int int
    13  }
    14  
    15  func (v *myInt) AsInt() int {
    16  	return v.Int
    17  }
    18  
    19  func (v *myInt) AsAny() any {
    20  	return v.Int
    21  }
    22  
    23  func ExampleAnyValuer() {
    24  	env := make(map[string]any)
    25  	env["ValueOne"] = &myInt{1}
    26  	env["ValueTwo"] = &myInt{2}
    27  
    28  	program, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), value.ValueGetter)
    29  
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	out, err := vm.Run(program, env)
    35  
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  
    40  	fmt.Println(out)
    41  	// Output: 3
    42  }