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

Add support for condition on bool type #5954

Merged
merged 6 commits into from
Jan 25, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
*Packetbeat*

- Configure good defaults for `add_kubernetes_metadata`. {pull}5707[5707]
- Add support for condition on bool type {issue}5659[5659] {pull}5954[5954]

*Winlogbeat*

Expand Down
48 changes: 36 additions & 12 deletions libbeat/processors/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type RangeValue struct {
}

type EqualsValue struct {
Int uint64
Str string
Int uint64
Str string
Bool bool
}

type Condition struct {
Expand Down Expand Up @@ -118,13 +119,22 @@ func (c *Condition) setEquals(cfg *ConditionFields) error {
uintValue, err := extractInt(value)
if err == nil {
c.equals[field] = EqualsValue{Int: uintValue}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should use continue in the code to have less nested if / else? Same on line 125.

} else {
sValue, err := extractString(value)
if err != nil {
return err
}
continue
}

sValue, err := extractString(value)
if err == nil {
c.equals[field] = EqualsValue{Str: sValue}
continue
}

bValue, err := extractBool(value)
if err == nil {
c.equals[field] = EqualsValue{Bool: bValue}
continue
}

return fmt.Errorf("unexpected type %T in equals condition", value)
}

return nil
Expand Down Expand Up @@ -257,16 +267,30 @@ func (c *Condition) checkEquals(event ValuesMap) bool {
if intValue != equalValue.Int {
return false
}
} else {
sValue, err := extractString(value)
if err != nil {
logp.Warn("unexpected type %T in equals condition as it accepts only integers and strings. ", value)

continue
}

sValue, err := extractString(value)
if err == nil {
if sValue != equalValue.Str {
return false
}
if sValue != equalValue.Str {

continue
}

bValue, err := extractBool(value)
if err == nil {
if bValue != equalValue.Bool {
return false
}

continue
}

logp.Err("unexpected type %T in equals condition as it accepts only integers, strings or bools. ", value)
return false
}

return true
Expand Down
19 changes: 18 additions & 1 deletion libbeat/processors/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ func TestCondition(t *testing.T) {
},
result: true,
},
{
config: ConditionConfig{
Equals: &ConditionFields{fields: map[string]interface{}{
"final": true,
}},
},
result: false,
},
{
config: ConditionConfig{
Equals: &ConditionFields{fields: map[string]interface{}{
"final": false,
}},
},
result: true,
},
}

event := &beat.Event{
Expand All @@ -150,7 +166,8 @@ func TestCondition(t *testing.T) {
"username": "monica",
"keywords": []string{"foo", "bar"},
},
"type": "process",
"type": "process",
"final": false,
},
}

Expand Down
9 changes: 9 additions & 0 deletions libbeat/processors/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ func extractString(unk interface{}) (string, error) {
return "", fmt.Errorf("unknown type %T passed to extractString", unk)
}
}

func extractBool(unk interface{}) (bool, error) {
switch b := unk.(type) {
case bool:
return b, nil
default:
return false, fmt.Errorf("unknown type %T passed to extractBool", unk)
}
}
10 changes: 10 additions & 0 deletions libbeat/processors/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ func TestExtractString(t *testing.T) {
}
assert.Equal(t, input, v)
}

func TestExtractBool(t *testing.T) {
input := true

v, err := extractBool(input)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, input, v)
}