2018-12-25 03:17:14 +01:00
|
|
|
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 {
|
2018-12-25 03:52:53 +01:00
|
|
|
Node expr.Node
|
|
|
|
Expression string
|
2018-12-25 03:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExprValue) String() string {
|
|
|
|
if e.Node != nil {
|
|
|
|
return e.Node.String()
|
|
|
|
} else {
|
|
|
|
return "<Empty Expression>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (e *ExprValue) Set(value string) error {
|
2018-12-25 03:52:53 +01:00
|
|
|
if value == "" {
|
|
|
|
e.Node = nil
|
|
|
|
e.Expression = value
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-25 03:17:14 +01:00
|
|
|
ast, err := expr.ParseExpression(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.Node = ast
|
2018-12-25 03:52:53 +01:00
|
|
|
e.Expression = value
|
2018-12-25 03:17:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExprValue) Validate(line map[string]interface{}) bool {
|
2018-12-25 03:52:53 +01:00
|
|
|
if e.Node == nil {
|
|
|
|
return true
|
|
|
|
}
|
2018-12-25 03:17:14 +01:00
|
|
|
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
|
|
|
|
}
|