From 7fc92752157af09bb6527a68214061d8332fbf9d Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Fri, 3 May 2024 12:54:11 +0200 Subject: [PATCH] Add WithEmptyCondition Co-authored-by: Koen Bollen <213502+koenbollen@users.noreply.github.com> --- examples/basic_test.go | 2 +- filter/converter.go | 7 +++++-- filter/converter_test.go | 16 +++++++++++++++- filter/options.go | 10 ++++++++++ integration/postgres_test.go | 4 ++-- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/examples/basic_test.go b/examples/basic_test.go index 06d0014..d7a3374 100644 --- a/examples/basic_test.go +++ b/examples/basic_test.go @@ -30,7 +30,7 @@ func ExampleNewConverter() { } func ExampleNewConverter_emptyfilter() { - converter := filter.NewConverter() + converter := filter.NewConverter(filter.WithEmptyCondition("TRUE")) // The default is FALSE if you don't change it. mongoFilterQuery := `{}` conditions, _, err := converter.Convert([]byte(mongoFilterQuery), 1) diff --git a/filter/converter.go b/filter/converter.go index f5a5efb..08ebf4f 100644 --- a/filter/converter.go +++ b/filter/converter.go @@ -26,13 +26,16 @@ type Converter struct { driver.Valuer sql.Scanner } + emptyCondition string } // NewConverter creates a new Converter with optional nested JSONB field mapping. // // Note: When using github.com/lib/pq, the filter.WithArrayDriver should be set to pq.Array. func NewConverter(options ...Option) *Converter { - converter := &Converter{} + converter := &Converter{ + emptyCondition: "FALSE", + } for _, option := range options { if option != nil { option(converter) @@ -57,7 +60,7 @@ func (c *Converter) Convert(query []byte, startAtParameterIndex int) (conditions } if len(mongoFilter) == 0 { - return "TRUE", []any{}, nil + return c.emptyCondition, []any{}, nil } conditions, values, err = c.convertFilter(mongoFilter, startAtParameterIndex) diff --git a/filter/converter_test.go b/filter/converter_test.go index 94d9a0e..8777060 100644 --- a/filter/converter_test.go +++ b/filter/converter_test.go @@ -197,7 +197,7 @@ func TestConverter_Convert(t *testing.T) { "empty filter", nil, `{}`, - `TRUE`, + `FALSE`, []any{}, nil, }, { @@ -260,3 +260,17 @@ func TestConverter_Convert_startAtParameterIndex(t *testing.T) { t.Errorf("Converter.Convert(..., 1234551231231231231) error = %v, want nil", err) } } + +func TestConverter_WithEmptyCondition(t *testing.T) { + c := filter.NewConverter(filter.WithEmptyCondition("TRUE")) + conditions, values, err := c.Convert([]byte(`{}`), 1) + if err != nil { + t.Fatal(err) + } + if want := "TRUE"; conditions != want { + t.Errorf("Converter.Convert() conditions = %v, want %v", conditions, want) + } + if len(values) != 0 { + t.Errorf("Converter.Convert() values = %v, want nil", values) + } +} diff --git a/filter/options.go b/filter/options.go index 399cb9a..769b004 100644 --- a/filter/options.go +++ b/filter/options.go @@ -39,3 +39,13 @@ func WithArrayDriver(f func(a any) interface { c.arrayDriver = f } } + +// WithEmptyCondition is an option to specify the condition to be used when the +// input query filter is empty. (e.g. you have a query with no conditions) +// +// The default value is `FALSE`, because it's the safer choice in most cases. +func WithEmptyCondition(condition string) Option { + return func(c *Converter) { + c.emptyCondition = condition + } +} diff --git a/integration/postgres_test.go b/integration/postgres_test.go index 6a22143..0a56768 100644 --- a/integration/postgres_test.go +++ b/integration/postgres_test.go @@ -293,8 +293,8 @@ func TestIntegration_BasicOperators(t *testing.T) { }, { `empty object`, - `{}`, - []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + `{}`, // Should return FALSE as the condition. + []int{}, nil, }, }