eval will evaluate a given program `prg` against a set of variables `vars` and return the output, eval details (optional), or error that results from evaluation.
(prg cel.Program, vars any)
| 161 | // and return the output, eval details (optional), or error that results from |
| 162 | // evaluation. |
| 163 | func eval(prg cel.Program, |
| 164 | vars any) (out ref.Val, det *cel.EvalDetails, err error) { |
| 165 | varMap, isMap := vars.(map[string]any) |
| 166 | fmt.Println("------ input ------") |
| 167 | if !isMap { |
| 168 | fmt.Printf("(%T)\n", vars) |
| 169 | } else { |
| 170 | for k, v := range varMap { |
| 171 | switch val := v.(type) { |
| 172 | case proto.Message: |
| 173 | bytes, err := prototext.Marshal(val) |
| 174 | if err != nil { |
| 175 | glog.Exitf("failed to marshal proto to text: %v", val) |
| 176 | } |
| 177 | fmt.Printf("%s = %s", k, string(bytes)) |
| 178 | case map[string]any: |
| 179 | b, _ := json.MarshalIndent(v, "", " ") |
| 180 | fmt.Printf("%s = %v\n", k, string(b)) |
| 181 | case uint64: |
| 182 | fmt.Printf("%s = %vu\n", k, v) |
| 183 | default: |
| 184 | fmt.Printf("%s = %v\n", k, v) |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | fmt.Println() |
| 189 | out, det, err = prg.Eval(vars) |
| 190 | report(out, det, err) |
| 191 | fmt.Println() |
| 192 | return |
| 193 | } |
| 194 | |
| 195 | // report prints out the result of evaluation in human-friendly terms. |
| 196 | func report(result ref.Val, details *cel.EvalDetails, err error) { |