Skip to content

Commit

Permalink
Add WithEmptyCondition
Browse files Browse the repository at this point in the history
Co-authored-by: Koen Bollen <[email protected]>
  • Loading branch information
erikdubbelboer and koenbollen committed May 3, 2024
1 parent 9bd7a5f commit 7fc9275
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions filter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion filter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestConverter_Convert(t *testing.T) {
"empty filter",
nil,
`{}`,
`TRUE`,
`FALSE`,
[]any{},
nil,
}, {
Expand Down Expand Up @@ -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)
}
}
10 changes: 10 additions & 0 deletions filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions integration/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down

0 comments on commit 7fc9275

Please sign in to comment.