1
0
Fork 0
forked from boyska/circolog
circolog/filtering/filter.go
boyska 658a4bbb1e filtering can be disabled with -tags nofilter
it will make your binaries way smaller
2018-12-26 01:29:39 +01:00

59 lines
1.1 KiB
Go

// +build !nofilter
package filtering
import (
"fmt"
"os"
"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
}
func (e *ExprValue) Validate(line map[string]interface{}) 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
}
if val.Type() != value.BoolType {
fmt.Fprintln(os.Stderr, "WARNING: The 'where' expression doesn't return a boolean")
return false
}
if val.Value().(bool) != true {
return false
}
return true
}