7
7
"fmt"
8
8
"sort"
9
9
"strings"
10
+ "sync"
10
11
)
11
12
12
13
var basicOperatorMap = map [string ]string {
@@ -33,23 +34,22 @@ type Converter struct {
33
34
}
34
35
emptyCondition string
35
36
placeholderName string
37
+
38
+ once sync.Once
36
39
}
37
40
38
41
// NewConverter creates a new Converter with optional nested JSONB field mapping.
39
42
//
40
43
// Note: When using github.com/lib/pq, the filter.WithArrayDriver should be set to pq.Array.
41
44
func NewConverter (options ... Option ) * Converter {
42
45
converter := & Converter {
43
- emptyCondition : "FALSE" ,
46
+ // don't set defaults, use the once.Do in #Convert()
44
47
}
45
48
for _ , option := range options {
46
49
if option != nil {
47
50
option (converter )
48
51
}
49
52
}
50
- if converter .placeholderName == "" {
51
- converter .placeholderName = DefaultPlaceholderName
52
- }
53
53
return converter
54
54
}
55
55
@@ -58,6 +58,15 @@ func NewConverter(options ...Option) *Converter {
58
58
// startAtParameterIndex is the index to start the parameter numbering at.
59
59
// Passing X will make the first indexed parameter $X, the second $X+1, and so on.
60
60
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
+
61
70
if startAtParameterIndex < 1 {
62
71
return "" , nil , fmt .Errorf ("startAtParameterIndex must be greater than 0" )
63
72
}
0 commit comments