MCPcopy Index your code
hub / github.com/crowdsecurity/crowdsec / RunExpression

Method RunExpression

pkg/hubtest/parser_assert.go:165–197  ·  view source on GitHub ↗
(expression string)

Source from the content-addressed store, hash-verified

163}
164
165func (p *ParserAssert) RunExpression(expression string) (interface{}, error) {
166 // debug doesn't make much sense with the ability to evaluate "on the fly"
167 // var debugFilter *exprhelpers.ExprDebugger
168 var output any
169
170 logger := log.WithField("file", p.File)
171
172 env := map[string]any{"results": *p.TestData}
173 opts := exprhelpers.GetExprOptions(env)
174 opts = append(opts, expr.Function("basename", basename, new(func (string) string)))
175
176 // wrap with basename() in case of datasource_path, for backward compatibility
177 expression = basenameShim(expression)
178
179 runtimeFilter, err := expr.Compile(expression, opts...)
180 if err != nil {
181 logger.Errorf("failed to compile '%s': %s", expression, err)
182 return output, err
183 }
184
185 // dump opcode in trace level
186 logger.Tracef("%s", runtimeFilter.Disassemble())
187
188 output, err = expr.Run(runtimeFilter, env)
189 if err != nil {
190 logger.Warningf("running : %s", expression)
191 logger.Warningf("runtime error: %s", err)
192
193 return output, fmt.Errorf("while running expression %s: %w", expression, err)
194 }
195
196 return output, nil
197}
198
199func (p *ParserAssert) EvalExpression(expression string) (string, error) {
200 output, err := p.RunExpression(expression)

Callers 2

EvalExpressionMethod · 0.95
RunMethod · 0.95

Calls 5

GetExprOptionsFunction · 0.92
basenameShimFunction · 0.85
TracefMethod · 0.80
RunMethod · 0.65
CompileMethod · 0.45

Tested by

no test coverage detected