-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: filter can messages via command line "--filter"
- Loading branch information
Showing
4 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package filter | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// imports | ||
// --------------------------------------------------------------------------------------- | ||
|
||
import ( | ||
"github.com/robertkrimen/otto" | ||
) | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// constants | ||
// --------------------------------------------------------------------------------------- | ||
|
||
const ( | ||
InputVariableName = "f" | ||
) | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// types | ||
// --------------------------------------------------------------------------------------- | ||
|
||
type Filter struct { | ||
Expr string | ||
} | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// public functions | ||
// --------------------------------------------------------------------------------------- | ||
|
||
func NewFilter(expr string) *Filter { | ||
if expr == "" { | ||
expr = "true" | ||
} | ||
|
||
return &Filter{Expr: expr} | ||
} | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// public members | ||
// --------------------------------------------------------------------------------------- | ||
|
||
func (f *Filter) Eval(v interface{}) (bool, error) { | ||
vm := otto.New() | ||
v, err := vm.ToValue(v) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
err = vm.Set(InputVariableName, v) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
ret, err := vm.Run(f.Expr) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return ret.ToBoolean() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff h1:+6NUiITWwE5q1KO6SAfUX918c+Tab0+tGAM/mtdlUyA= | ||
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY= | ||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU= | ||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= | ||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo= | ||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI= | ||
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters