Skip to content

Commit 24ec5c4

Browse files
committed
addresses issue #252
ignore fields with no values Signed-off-by: Tim Bray <[email protected]>
1 parent 25e8bd7 commit 24ec5c4

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

PATTERNS.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ The following pattern will not match:
105105

106106
We may be able to change this in future.
107107

108-
The case of empty arrays is interesting. Consider this event:
108+
The case of empty arrays is interesting, both in Patterns and Events. Consider this event:
109109

110110
```json
111111
{ "a": [] }
@@ -123,6 +123,16 @@ I.e., only the first of the two sample patterns below matches.
123123
This makes sense in the context of the leaf-node semantics; there
124124
really is no value for the `"a"` field.
125125

126+
In Patterns, the following never matches any Event:
127+
128+
```json
129+
{ "a": [] }
130+
```
131+
132+
Once again, there is nothing in the array of candidate values in the Pattern that can match any value of an `"a"`
133+
field in an Event.
134+
135+
126136

127137
### Anything-But Pattern
128138

core_matcher.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ func (m *coreMatcher) addPattern(x X, patternJSON string) error {
8080
// combo.
8181
states := []*fieldMatcher{currentFields.state}
8282
for _, field := range patternFields {
83+
84+
// if the field has no values, this is a no-op
85+
if len(field.vals) == 0 {
86+
continue
87+
}
88+
8389
var nextStates []*fieldMatcher
8490

8591
// separate handling for field exists:true/false and regular field name/val matches. Since the exists

pattern_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,60 @@ func TestPatternErrorHandling(t *testing.T) {
4141
}
4242
}
4343

44+
// test that adding an empty pattern doesn't screw anything up
45+
func TestEmptyValueArray(t *testing.T) {
46+
var err error
47+
empties := []string{
48+
`{"data": {"field": []}}`,
49+
`{"data": {"field": [], "field2": [23]}}`,
50+
`{"data": {"field2": [], "field": [23]}}`,
51+
}
52+
wanted := map[string][]X{
53+
`{"data": {"field": 23}}`: {empties[2]},
54+
`{"data": {"field2": 23}}`: {empties[1]},
55+
`{"data": {"field": []}}`: {},
56+
}
57+
cm := newCoreMatcher()
58+
defer func() {
59+
if r := recover(); r != nil {
60+
t.Errorf("addPattern panicked")
61+
}
62+
}()
63+
for _, empty := range empties {
64+
err = cm.addPattern(empty, empty)
65+
if err != nil {
66+
t.Error("addPattern: " + err.Error())
67+
}
68+
}
69+
70+
for want, _ := range wanted {
71+
matches, err := cm.matchesForJSONEvent([]byte(want))
72+
if err != nil {
73+
t.Errorf("m4je err on %s", want)
74+
}
75+
checkXEqual(wanted[want], matches, t)
76+
}
77+
}
78+
func checkXEqual(x1s []X, x2s []X, t *testing.T) {
79+
x2size := len(x2s)
80+
for _, x1 := range x1s {
81+
count := 0
82+
for _, x2 := range x2s {
83+
if x1 == x2 {
84+
count++
85+
}
86+
}
87+
if count != 1 {
88+
t.Errorf("for %s in X1, %d", x1, count)
89+
} else {
90+
x2size--
91+
}
92+
}
93+
if x2size != 0 {
94+
t.Error("Extra elements in X2")
95+
}
96+
}
97+
4498
func TestPatternFromJSON(t *testing.T) {
4599
bads := []string{
46100
`x`,

0 commit comments

Comments
 (0)