circolog/filtering/filter.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

// +build !nofilter
package filtering
import (
"fmt"
"os"
2019-03-25 02:46:03 +01:00
"git.lattuga.net/boyska/circolog/data"
"github.com/araddon/qlbridge/datasource"
"github.com/araddon/qlbridge/expr"
"github.com/araddon/qlbridge/value"
"github.com/araddon/qlbridge/vm"
)
type ExprValue struct {
node expr.Node
expression string
}
func (e *ExprValue) String() string {
if e.node != nil {
return e.node.String()
} else {
return "<Empty Expression>"
}
}
func (e *ExprValue) Set(value string) error {
if value == "" {
e.node = nil
e.expression = value
return nil
}
ast, err := expr.ParseExpression(value)
if err != nil {
return err
}
e.node = ast
e.expression = value
return nil
}
2019-01-09 16:55:58 +01:00
// Validate answers the question whether to include a log line or not.
2019-03-25 02:46:03 +01:00
func (e *ExprValue) Validate(line data.Message) bool {
if e.node == nil {
return true
}
context := datasource.NewContextSimpleNative(line)
val, ok := vm.Eval(context, e.node)
if !ok || val == nil { // errors when evaluating
return false
}
2018-12-26 01:54:30 +01:00
if bv, isBool := val.(value.BoolValue); isBool {
return bv.Val()
}
2018-12-26 01:54:30 +01:00
fmt.Fprintln(os.Stderr, "WARNING: The 'where' expression doesn't return a boolean")
return false
}