I was struggling to create expressions for slices. I tried to write a bunch of expressions but couldn't able to end with a success compilation. What I want to do is:
- Given
env is an []
- Enforce
Name: E is existed in slice (any)
- If exists, must be
Active: true (skip if it does not exist)
But I couldn't able to match E against a given Name field: any(#, {Name matches 'E'}) (tried both of . and #, fails with cannot use pointer accessor outside closure). Also tried Name in ['E'] but fails: invalid operation: int(string).
Here is an example snippet:
package main
import (
"fmt"
"github.com/antonmedv/expr"
)
type Inputs []Input
type Input struct {
Name string
Active bool
}
func main() {
inputs := []Input{
{Name: "A", Active: true},
{Name: "B", Active: false},
{Name: "C", Active: true},
{Name: "D", Active: false},
{Name: "E", Active: true},
{Name: "F", Active: false},
}
code := `WHAT HERE TO EVAL: Name = 'E' is exist and Active = 'true'?`
program, err := expr.Compile(code, expr.Env(inputs))
if err != nil {
panic(err)
}
output, err := expr.Run(program, inputs)
if err != nil {
panic(err)
}
fmt.Println(output)
}
Can you please help me on this? Thanks!
I was struggling to create expressions for slices. I tried to write a bunch of expressions but couldn't able to end with a success compilation. What I want to do is:
envis an[]Name: Eis existed in slice (any)Active: true(skip if it does not exist)But I couldn't able to match
Eagainst a givenNamefield:any(#, {Name matches 'E'})(tried both of.and#, fails withcannot use pointer accessor outside closure). Also triedName in ['E']but fails:invalid operation: int(string).Here is an example snippet:
Can you please help me on this? Thanks!