@@ -3,8 +3,10 @@ package postgrest
33import (
44 "context"
55 "encoding/json"
6+ "errors"
67 "fmt"
78 "regexp"
9+ "slices"
810 "strconv"
911 "strings"
1012)
@@ -17,43 +19,44 @@ type FilterBuilder struct {
1719 tableName string
1820 headers map [string ]string
1921 params map [string ]string
22+ err error
2023}
2124
2225// ExecuteString runs the PostgREST query, returning the result as a JSON
2326// string.
2427func (f * FilterBuilder ) ExecuteString () (string , int64 , error ) {
25- return executeString (context .Background (), f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params )
28+ return executeString (context .Background (), f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params , f . err )
2629}
2730
2831// ExecuteStringWithContext runs the PostgREST query, returning the result as
2932// a JSON string.
3033func (f * FilterBuilder ) ExecuteStringWithContext (ctx context.Context ) (string , int64 , error ) {
31- return executeString (ctx , f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params )
34+ return executeString (ctx , f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params , f . err )
3235}
3336
3437// Execute runs the PostgREST query, returning the result as a byte slice.
3538func (f * FilterBuilder ) Execute () ([]byte , int64 , error ) {
36- return execute (context .Background (), f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params )
39+ return execute (context .Background (), f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params , f . err )
3740}
3841
3942// ExecuteWithContext runs the PostgREST query with the given context,
4043// returning the result as a byte slice.
4144func (f * FilterBuilder ) ExecuteWithContext (ctx context.Context ) ([]byte , int64 , error ) {
42- return execute (ctx , f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params )
45+ return execute (ctx , f .client , f .method , f .body , []string {f .tableName }, f .headers , f .params , f . err )
4346}
4447
4548// ExecuteTo runs the PostgREST query, encoding the result to the supplied
4649// interface. Note that the argument for the to parameter should always be a
4750// reference to a slice.
4851func (f * FilterBuilder ) ExecuteTo (to interface {}) (countType , error ) {
49- return executeTo (context .Background (), f .client , f .method , f .body , to , []string {f .tableName }, f .headers , f .params )
52+ return executeTo (context .Background (), f .client , f .method , f .body , to , []string {f .tableName }, f .headers , f .params , f . err )
5053}
5154
5255// ExecuteToWithContext runs the PostgREST query with the given context,
5356// encoding the result to the supplied interface. Note that the argument for
5457// the to parameter should always be a reference to a slice.
5558func (f * FilterBuilder ) ExecuteToWithContext (ctx context.Context , to interface {}) (countType , error ) {
56- return executeTo (ctx , f .client , f .method , f .body , to , []string {f .tableName }, f .headers , f .params )
59+ return executeTo (ctx , f .client , f .method , f .body , to , []string {f .tableName }, f .headers , f .params , f . err )
5760}
5861
5962var filterOperators = []string {"eq" , "neq" , "gt" , "gte" , "lt" , "lte" , "like" , "ilike" , "is" , "in" , "cs" , "cd" , "sl" , "sr" , "nxl" , "nxr" , "adj" , "ov" , "fts" , "plfts" , "phfts" , "wfts" }
@@ -74,19 +77,16 @@ func (f *FilterBuilder) appendFilter(column, filterValue string) *FilterBuilder
7477}
7578
7679func isOperator (value string ) bool {
77- for _ , operator := range filterOperators {
78- if value == operator {
79- return true
80- }
81- }
82- return false
80+ return slices .Contains (filterOperators , value )
8381}
8482
8583// Filter adds a filtering operator to the query. For a list of available
8684// operators, see: https://postgrest.org/en/stable/api.html#operators
8785func (f * FilterBuilder ) Filter (column , operator , value string ) * FilterBuilder {
8886 if ! isOperator (operator ) {
89- f .client .ClientError = fmt .Errorf ("invalid filter operator" )
87+ err := fmt .Errorf ("invalid Filter operator: %s" , operator )
88+ f .client .ClientError = err
89+ f .err = errors .Join (f .err , err )
9090 return f
9191 }
9292 return f .appendFilter (column , fmt .Sprintf ("%s.%s" , operator , value ))
@@ -200,6 +200,7 @@ func (f *FilterBuilder) ContainsObject(column string, value interface{}) *Filter
200200 sum , err := json .Marshal (value )
201201 if err != nil {
202202 f .client .ClientError = err
203+ f .err = errors .Join (f .err , fmt .Errorf ("error marshaling value for ContainsObject: %w" , err ))
203204 return f
204205 }
205206 return f .appendFilter (column , "cs." + string (sum ))
@@ -208,7 +209,9 @@ func (f *FilterBuilder) ContainsObject(column string, value interface{}) *Filter
208209func (f * FilterBuilder ) ContainedByObject (column string , value interface {}) * FilterBuilder {
209210 sum , err := json .Marshal (value )
210211 if err != nil {
212+ err := fmt .Errorf ("error marshaling value for ContainedByObject: %w" , err )
211213 f .client .ClientError = err
214+ f .err = errors .Join (f .err , err )
212215 return f
213216 }
214217 return f .appendFilter (column , "cd." + string (sum ))
@@ -257,7 +260,9 @@ func (f *FilterBuilder) TextSearch(column, userQuery, config, tsType string) *Fi
257260 } else if tsType == "" {
258261 typePart = ""
259262 } else {
260- f .client .ClientError = fmt .Errorf ("invalid text search type" )
263+ err := fmt .Errorf ("invalid text search type: %s" , tsType )
264+ f .client .ClientError = err
265+ f .err = errors .Join (f .err , err )
261266 return f
262267 }
263268 if config != "" {
0 commit comments