Skip to content

Commit e9ebb28

Browse files
committed
Set converter defaults once in Convert method
This will make the converter also work as expected when the filter.Converter struct is created without the NewConverter function.
1 parent 289c9a4 commit e9ebb28

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

filter/converter.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"sort"
99
"strings"
10+
"sync"
1011
)
1112

1213
var basicOperatorMap = map[string]string{
@@ -33,23 +34,22 @@ type Converter struct {
3334
}
3435
emptyCondition string
3536
placeholderName string
37+
38+
once sync.Once
3639
}
3740

3841
// NewConverter creates a new Converter with optional nested JSONB field mapping.
3942
//
4043
// Note: When using github.com/lib/pq, the filter.WithArrayDriver should be set to pq.Array.
4144
func NewConverter(options ...Option) *Converter {
4245
converter := &Converter{
43-
emptyCondition: "FALSE",
46+
// don't set defaults, use the once.Do in #Convert()
4447
}
4548
for _, option := range options {
4649
if option != nil {
4750
option(converter)
4851
}
4952
}
50-
if converter.placeholderName == "" {
51-
converter.placeholderName = DefaultPlaceholderName
52-
}
5353
return converter
5454
}
5555

@@ -58,6 +58,15 @@ func NewConverter(options ...Option) *Converter {
5858
// startAtParameterIndex is the index to start the parameter numbering at.
5959
// Passing X will make the first indexed parameter $X, the second $X+1, and so on.
6060
func (c *Converter) Convert(query []byte, startAtParameterIndex int) (conditions string, values []any, err error) {
61+
c.once.Do(func() {
62+
if c.emptyCondition == "" {
63+
c.emptyCondition = "FALSE"
64+
}
65+
if c.placeholderName == "" {
66+
c.placeholderName = DefaultPlaceholderName
67+
}
68+
})
69+
6170
if startAtParameterIndex < 1 {
6271
return "", nil, fmt.Errorf("startAtParameterIndex must be greater than 0")
6372
}

filter/converter_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,28 @@ func TestConverter_WithEmptyCondition(t *testing.T) {
402402
t.Errorf("Converter.Convert() values = %v, want nil", values)
403403
}
404404
}
405+
406+
func TestConverter_NoConstructor(t *testing.T) {
407+
c := &filter.Converter{}
408+
conditions, values, err := c.Convert([]byte(`{"name": "John"}`), 1)
409+
if err != nil {
410+
t.Fatal(err)
411+
}
412+
if want := `("name" = $1)`; conditions != want {
413+
t.Errorf("Converter.Convert() conditions = %v, want %v", conditions, want)
414+
}
415+
if !reflect.DeepEqual(values, []any{"John"}) {
416+
t.Errorf("Converter.Convert() values = %v, want %v", values, []any{"John"})
417+
}
418+
419+
conditions, values, err = c.Convert([]byte(``), 1)
420+
if err != nil {
421+
t.Fatal(err)
422+
}
423+
if want := "FALSE"; conditions != want {
424+
t.Errorf("Converter.Convert() conditions = %v, want %v", conditions, want)
425+
}
426+
if len(values) != 0 {
427+
t.Errorf("Converter.Convert() values = %v, want nil", values)
428+
}
429+
}

0 commit comments

Comments
 (0)