Skip to content

Commit

Permalink
Fixed: string representation for "not in" filter comparator should sh…
Browse files Browse the repository at this point in the history
…ow array (#133)

* Fix: string representation for "not in" filter comparator to show array

* exclude dot-imports and superfluous-else linters
  • Loading branch information
kcarrasco-panw authored Nov 16, 2023
1 parent 2fe0520 commit 3d09d6d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ lint:
--disable-all \
--skip-files data_test.go \
--exclude-use-default=false \
--exclude=dot-imports \
--exclude=package-comments \
--exclude=unused-parameter \
--exclude=superfluous-else \
--enable=errcheck \
--enable=goimports \
--enable=ineffassign \
Expand Down
5 changes: 4 additions & 1 deletion filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ func translateOperator(operator FilterOperator) string {
func translateValue(comparator FilterComparator, value any) string {

v := reflect.ValueOf(value)
if comparator != ContainComparator && comparator != InComparator && comparator != MatchComparator {
if comparator != ContainComparator &&
comparator != InComparator &&
comparator != NotInComparator &&
comparator != MatchComparator {
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
v = reflect.ValueOf(v.Index(0).Interface())
}
Expand Down
60 changes: 58 additions & 2 deletions filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,62 @@ func TestFilter_NewComposer(t *testing.T) {
})
})

Convey("When I add a new NotIn statement", func() {

f.WithKey("notin").NotIn("a", "b", "c")

Convey("Then the filter should be correctly populated", func() {
So(f.keys, ShouldResemble, FilterKeys{
"hello",
"notin",
})
So(f.Values(), ShouldResemble, FilterValues{
FilterValue{1},
FilterValue{"a", "b", "c"},
})
So(f.Operators(), ShouldResemble, FilterOperators{
AndOperator,
AndOperator,
})
So(f.Comparators(), ShouldResemble, FilterComparators{
EqualComparator,
NotInComparator,
})

Convey("When I add a new NotContains statement", func() {

f.WithKey("notctn").NotContains(false)

Convey("Then the filter should be correctly populated", func() {
So(f.Keys(), ShouldResemble, FilterKeys{
"hello",
"notin",
"notctn",
})
So(f.Values(), ShouldResemble, FilterValues{
FilterValue{1},
FilterValue{"a", "b", "c"},
FilterValue{false},
})
So(f.Operators(), ShouldResemble, FilterOperators{
AndOperator,
AndOperator,
AndOperator,
})
So(f.Comparators(), ShouldResemble, FilterComparators{
EqualComparator,
NotInComparator,
NotContainComparator,
})

Convey("Then the string representation should be correct", func() {
So(f.String(), ShouldEqual, `hello == 1 and notin not in ["a", "b", "c"] and notctn not contains false`)
})
})
})
})
})

Convey("When I add a new difference comparator", func() {
f.WithKey("x").NotEquals(true)

Expand Down Expand Up @@ -442,15 +498,15 @@ func TestFilter_SubFilters(t *testing.T) {
Done(),
NewFilterComposer().
WithKey("size").In(true, false).
WithKey("size").NotIn(1).
WithKey("size").NotIn(1, 2).
Done(),
).
Done(),
).
Done()

Convey("When I call string it should be correct ", func() {
So(f.String(), ShouldEqual, `namespace == "coucou" and number == 32.900000 and ((name == "toto" and value == 1) and (color contains ["red", "green", "blue", 43] and something not contains "stuff" or ((size matches [".*"]) or (size == "medium" and fat == false) or (size in [true, false] and size not in 1))))`)
So(f.String(), ShouldEqual, `namespace == "coucou" and number == 32.900000 and ((name == "toto" and value == 1) and (color contains ["red", "green", "blue", 43] and something not contains "stuff" or ((size matches [".*"]) or (size == "medium" and fat == false) or (size in [true, false] and size not in [1, 2]))))`)
})
})
}
Expand Down

0 comments on commit 3d09d6d

Please # to comment.