MCPcopy
hub / github.com/expr-lang/expr / ExampleEnv

Function ExampleEnv

expr_test.go:97–146  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

95}
96
97func ExampleEnv() {
98 type Segment struct {
99 Origin string
100 }
101 type Passengers struct {
102 Adults int
103 }
104 type Meta struct {
105 Tags map[string]string
106 }
107 type Env struct {
108 Meta
109 Segments []*Segment
110 Passengers *Passengers
111 Marker string
112 }
113
114 code := `all(Segments, {.Origin == "MOW"}) && Passengers.Adults > 0 && Tags["foo"] startsWith "bar"`
115
116 program, err := expr.Compile(code, expr.Env(Env{}))
117 if err != nil {
118 fmt.Printf("%v", err)
119 return
120 }
121
122 env := Env{
123 Meta: Meta{
124 Tags: map[string]string{
125 "foo": "bar",
126 },
127 },
128 Segments: []*Segment{
129 {Origin: "MOW"},
130 },
131 Passengers: &Passengers{
132 Adults: 2,
133 },
134 Marker: "test",
135 }
136
137 output, err := expr.Run(program, env)
138 if err != nil {
139 fmt.Printf("%v", err)
140 return
141 }
142
143 fmt.Printf("%v", output)
144
145 // Output: true
146}
147
148func ExampleEnv_tagged_field_names() {
149 env := struct {

Callers

nothing calls this directly

Calls 4

CompileFunction · 0.92
EnvStruct · 0.92
RunFunction · 0.92
PrintfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…