circolog/filtering/filter.go
boyska 8735ad2c21 refactor filtering code
goal: use filters server-side, too
2018-12-25 03:17:14 +01:00

47 lines
941 B
Go

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
}
func (e *ExprValue) String() string {
if e.Node != nil {
return e.Node.String()
} else {
return "<Empty Expression>"
}
}
func (e *ExprValue) Set(value string) error {
ast, err := expr.ParseExpression(value)
if err != nil {
return err
}
e.Node = ast
return nil
}
func (e *ExprValue) Validate(line map[string]interface{}) bool {
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
}