diff --git a/options.go b/options.go index 5110483..e649466 100644 --- a/options.go +++ b/options.go @@ -71,11 +71,16 @@ const ( OrderDescending order = "desc" ) +// SetOrder is a functional option used to sort the results from an API call. +// The default order is by relevance. +// +// For more information, visit: https://api-docs.igdb.com/#sorting func SetOrder(field string, order order) Option { return func() (apicalypse.Option, error) { if whitespace.IsBlank(field) { return nil, ErrEmptyFields } + return apicalypse.Sort(field, string(order)), nil } } @@ -115,24 +120,45 @@ func SetOffset(off int) Option { // match an IGDB object's JSON field tag exactly, not the Go struct field // name. // -// The default for Get and List functions is set to all available fields. -// The default for Search functions is set to solely the ID field. -// // For more information, visit: https://api-docs.igdb.com/#fields func SetFields(fields ...string) Option { return func() (apicalypse.Option, error) { if len(fields) <= 0 { return nil, ErrEmptyFields } + for _, f := range fields { if whitespace.IsBlank(f) { return nil, ErrEmptyFields } } + return apicalypse.Fields(fields...), nil } } +// SetExclude is a functional option used to specify which fields of the +// requested IGDB object you want the API to exclude. Note that the field +// string must match an IGDB object's JSON field tag exactly, not the Go struct +// name. +// +// For more information, visit: https://api-docs.igdb.com/#exclude +func SetExclude(fields ...string) Option { + return func() (apicalypse.Option, error) { + if len(fields) <= 0 { + return nil, ErrEmptyFields + } + + for _, f := range fields { + if whitespace.IsBlank(f) { + return nil, ErrEmptyFields + } + } + + return apicalypse.Exclude(fields...), nil + } +} + // operator represents the postfix operation used to filter the results from // an API call using the provided field value. For the list of postfix // operators, visit: https://api-docs.igdb.com/#filters @@ -201,6 +227,7 @@ func setSearch(qry string) Option { if whitespace.IsBlank(qry) { return nil, ErrEmptyQry } + return apicalypse.Search("", qry), nil } } diff --git a/options_test.go b/options_test.go index e65c76b..24a0da7 100644 --- a/options_test.go +++ b/options_test.go @@ -194,6 +194,44 @@ func TestSetFields(t *testing.T) { } } +func TestSetExclude(t *testing.T) { + var tests = []struct { + name string + fields []string + wantFields string + wantErr error + }{ + {"Single non-empty field", []string{"name"}, "name", nil}, + {"Multiple non-empty fields", []string{"name", "popularity", "rating"}, "name,popularity,rating", nil}, + {"Empty fields slice", []string{}, "", ErrEmptyFields}, + {"Single empty field", []string{" "}, "", ErrEmptyFields}, + {"Multiple empty fields", []string{"", " ", "", ""}, "", ErrEmptyFields}, + {"Mixed empty and non-empty fields", []string{"", "id", " ", "url"}, "", ErrEmptyFields}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + fn, err := SetExclude(test.fields...)() + if errors.Cause(err) != test.wantErr { + t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr) + } + + if test.wantErr != nil { + return + } + + q, err := apicalypse.Query(fn) + if err != nil { + t.Fatal(err) + } + + if !strings.Contains(q, test.wantFields) { + t.Errorf("got: <%v>, want: <%v>", q, test.wantFields) + } + }) + } +} + func TestSetFilter(t *testing.T) { var tests = []struct { name string