Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor cleanups in selector #2118

Merged
merged 3 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 83 additions & 25 deletions libbeat/outputs/outil/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Selector struct {
sel selector
sel SelectorExpr
}

type Settings struct {
Expand All @@ -26,18 +26,18 @@ type Settings struct {
FailEmpty bool
}

type selector interface {
type SelectorExpr interface {
sel(evt common.MapStr) (string, error)
}

type emptySelector struct{}

type listSelector struct {
selectors []selector
selectors []SelectorExpr
}

type condSelector struct {
s selector
s SelectorExpr
cond *processors.Condition
}

Expand All @@ -51,12 +51,23 @@ type fmtSelector struct {
}

type mapSelector struct {
from selector
from SelectorExpr
otherwise string
to map[string]string
}

var nilSelector selector = &emptySelector{}
var nilSelector SelectorExpr = &emptySelector{}

func MakeSelector(es ...SelectorExpr) Selector {
switch len(es) {
case 0:
return Selector{nilSelector}
case 1:
return Selector{es[0]}
default:
return Selector{ConcatSelectorExpr(es...)}
}
}

// Select runs configured selector against the current event.
// If no matching selector is found, an empty string is returned.
Expand All @@ -66,8 +77,15 @@ func (s Selector) Select(evt common.MapStr) (string, error) {
return s.sel.sel(evt)
}

func BuildSelector(cfg *common.Config, settings Settings) (Selector, error) {
var sel []selector
func (s Selector) IsEmpty() bool {
return s.sel == nilSelector || s.sel == nil
}

func BuildSelectorFromConfig(
cfg *common.Config,
settings Settings,
) (Selector, error) {
var sel []SelectorExpr

key := settings.Key
multiKey := settings.MultiKey
Expand Down Expand Up @@ -111,7 +129,18 @@ func BuildSelector(cfg *common.Config, settings Settings) (Selector, error) {
return Selector{}, fmt.Errorf("%v in %v", err, cfg.PathOf(key))
}

sel = append(sel, &fmtSelector{f: *fmtstr})
if fmtstr.IsConst() {
str, err := fmtstr.Run(common.MapStr{})
if err != nil {
return Selector{}, err
}

if str != "" {
sel = append(sel, ConstSelectorExpr(str))
}
} else {
sel = append(sel, FmtSelectorExpr(fmtstr, ""))
}
}

if settings.FailEmpty && !found {
Expand All @@ -124,20 +153,44 @@ func BuildSelector(cfg *common.Config, settings Settings) (Selector, error) {
multiKey, cfg.Path())
}

switch len(sel) {
case 0:
return Selector{nilSelector}, nil
case 1:
return Selector{sel[0]}, nil
default:
return Selector{&listSelector{sel}}, nil
}
return MakeSelector(sel...), nil
}

func EmptySelectorExpr() SelectorExpr {
return nilSelector
}

func ConstSelectorExpr(s string) SelectorExpr {
return &constSelector{s}
}

func FmtSelectorExpr(fmt *fmtstr.EventFormatString, fallback string) SelectorExpr {
return &fmtSelector{*fmt, fallback}
}

func ConcatSelectorExpr(s ...SelectorExpr) SelectorExpr {
return &listSelector{s}
}

func buildSingle(cfg *common.Config, key string) (selector, error) {
func ConditionalSelectorExpr(
s SelectorExpr,
cond *processors.Condition,
) SelectorExpr {
return &condSelector{s, cond}
}

func LookupSelectorExpr(
s SelectorExpr,
table map[string]string,
fallback string,
) SelectorExpr {
return &mapSelector{s, fallback, table}
}

func buildSingle(cfg *common.Config, key string) (SelectorExpr, error) {
// TODO: check for unknown fields

// 3. extract required key-word handler
// 1. extract required key-word handler
if !cfg.HasField(key) {
return nil, fmt.Errorf("missing %v", cfg.PathOf(key))
}
Expand Down Expand Up @@ -194,7 +247,7 @@ func buildSingle(cfg *common.Config, key string) (selector, error) {
}

// 5. build selector from available fields
var sel selector
var sel SelectorExpr
if len(mapping.Table) > 0 {
if evtfmt.IsConst() {
str, err := evtfmt.Run(common.MapStr{})
Expand All @@ -210,11 +263,11 @@ func buildSingle(cfg *common.Config, key string) (selector, error) {
if str == "" {
sel = nilSelector
} else {
sel = &constSelector{str}
sel = ConstSelectorExpr(str)
}
} else {
sel = &mapSelector{
from: &fmtSelector{f: *evtfmt},
from: FmtSelectorExpr(evtfmt, ""),
to: mapping.Table,
otherwise: otherwise,
}
Expand All @@ -225,13 +278,18 @@ func buildSingle(cfg *common.Config, key string) (selector, error) {
if err != nil {
return nil, err
}
sel = &constSelector{str}

if str == "" {
sel = nilSelector
} else {
sel = ConstSelectorExpr(str)
}
} else {
sel = &fmtSelector{f: *evtfmt, otherwise: otherwise}
sel = FmtSelectorExpr(evtfmt, otherwise)
}
}
if cond != nil && sel != nilSelector {
sel = &condSelector{s: sel, cond: cond}
sel = ConditionalSelectorExpr(sel, cond)
}

return sel, nil
Expand Down
4 changes: 2 additions & 2 deletions libbeat/outputs/outil/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestSelector(t *testing.T) {
continue
}

sel, err := BuildSelector(cfg, Settings{
sel, err := BuildSelectorFromConfig(cfg, Settings{
Key: "key",
MultiKey: "keys",
EnableSingleOnly: true,
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestSelectorInitFail(t *testing.T) {
continue
}

_, err = BuildSelector(cfg, Settings{
_, err = BuildSelectorFromConfig(cfg, Settings{
Key: "key",
MultiKey: "keys",
EnableSingleOnly: true,
Expand Down