1
0
Fork 0
forked from boyska/circolog

filtering can be disabled with -tags nofilter

it will make your binaries way smaller
This commit is contained in:
boyska 2018-12-26 01:29:39 +01:00
parent 14e97dd43e
commit 658a4bbb1e
4 changed files with 31 additions and 12 deletions

View file

@ -75,7 +75,7 @@ func main() {
log.Println("invalid BSON", err) log.Println("invalid BSON", err)
continue continue
} }
if filter.Node != nil && !filter.Validate(parsed) { if !filter.Validate(parsed) {
continue continue
} }
if err := formatter.WriteFormatted(os.Stdout, formatter.FormatSyslog, parsed); err != nil { if err := formatter.WriteFormatted(os.Stdout, formatter.FormatSyslog, parsed); err != nil {

View file

@ -1,3 +1,5 @@
// +build !nofilter
package filtering package filtering
import ( import (
@ -11,38 +13,38 @@ import (
) )
type ExprValue struct { type ExprValue struct {
Node expr.Node node expr.Node
Expression string expression string
} }
func (e *ExprValue) String() string { func (e *ExprValue) String() string {
if e.Node != nil { if e.node != nil {
return e.Node.String() return e.node.String()
} else { } else {
return "<Empty Expression>" return "<Empty Expression>"
} }
} }
func (e *ExprValue) Set(value string) error { func (e *ExprValue) Set(value string) error {
if value == "" { if value == "" {
e.Node = nil e.node = nil
e.Expression = value e.expression = value
return nil return nil
} }
ast, err := expr.ParseExpression(value) ast, err := expr.ParseExpression(value)
if err != nil { if err != nil {
return err return err
} }
e.Node = ast e.node = ast
e.Expression = value e.expression = value
return nil return nil
} }
func (e *ExprValue) Validate(line map[string]interface{}) bool { func (e *ExprValue) Validate(line map[string]interface{}) bool {
if e.Node == nil { if e.node == nil {
return true return true
} }
context := datasource.NewContextSimpleNative(line) context := datasource.NewContextSimpleNative(line)
val, ok := vm.Eval(context, e.Node) val, ok := vm.Eval(context, e.node)
if !ok || val == nil { // errors when evaluating if !ok || val == nil { // errors when evaluating
return false return false
} }

17
filtering/filter_fake.go Normal file
View file

@ -0,0 +1,17 @@
// +build nofilter
package filtering
type ExprValue struct {
}
func (e *ExprValue) String() string {
return "<filtering disabled>"
}
func (e *ExprValue) Set(value string) error {
return nil
}
func (e *ExprValue) Validate(line map[string]interface{}) bool {
return true
}

2
hub.go
View file

@ -144,7 +144,7 @@ func (h *Hub) Run() {
cmd.Response <- CommandResponse{Value: map[string]interface{}{ cmd.Response <- CommandResponse{Value: map[string]interface{}{
"size": h.circbuf.Len(), "size": h.circbuf.Len(),
"paused": !active, "paused": !active,
"filter": filter.Expression, "filter": filter.String(),
}} }}
case CommandNewFilter: case CommandNewFilter:
if err := filter.Set(cmd.Parameters["where"].(string)); err != nil { if err := filter.Set(cmd.Parameters["where"].(string)); err != nil {