-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Henry-Sarabia/update-options
Remove Options Object
- Loading branch information
Showing
7 changed files
with
229 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package apicalypse | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"strings" | ||
) | ||
|
||
// newFilters returns a filter map mutated by the provided FuncOption arguments. | ||
// If no FuncOption's are provided, an empty map is returned. | ||
func newFilters(funcOpts ...FuncOption) (map[string]string, error) { | ||
filters := map[string]string{} | ||
|
||
for _, f := range funcOpts { | ||
if err := f(filters); err != nil { | ||
return nil, errors.Wrap(err, "cannot create new options") | ||
} | ||
} | ||
|
||
return filters, nil | ||
} | ||
|
||
// toString returns the filters as a single string. | ||
func toString(f map[string]string) string { | ||
if len(f) <= 0 { | ||
return "" | ||
} | ||
|
||
b := strings.Builder{} | ||
for k, v := range f { | ||
b.WriteString(k + " " + v + "; ") | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
// reader returns the filters as a *strings.Reader | ||
// to satisfy the io.Reader interface. | ||
func toReader(f map[string]string) *strings.Reader { | ||
s := toString(f) | ||
return strings.NewReader(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package apicalypse | ||
|
||
import ( | ||
"bytes" | ||
"github.com/pkg/errors" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNewFilters(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
funcOpts []FuncOption | ||
wantFilters map[string]string | ||
wantErr error | ||
}{ | ||
{"Empty option", []FuncOption{}, map[string]string{}, nil}, | ||
{"Single option", []FuncOption{Limit(15)}, map[string]string{"limit": "15"}, nil}, | ||
{"Multiple options", []FuncOption{Limit(15), Offset(10), Fields("name", "rating")}, map[string]string{"limit": "15", "offset": "10", "fields": "name,rating"}, nil}, | ||
{"Single error option", []FuncOption{Limit(-99)}, nil, ErrNegativeInput}, | ||
{"Multiple error options", []FuncOption{Fields(), Exclude(), Where()}, nil, ErrMissingInput}, | ||
{"Mixed options", []FuncOption{Limit(10), Offset(-99)}, nil, ErrNegativeInput}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
filters, err := newFilters(test.funcOpts...) | ||
if !reflect.DeepEqual(errors.Cause(err), test.wantErr) { | ||
t.Errorf("got: <%v>, want: <%v>", err, test.wantErr) | ||
} | ||
|
||
if !reflect.DeepEqual(filters, test.wantFilters) { | ||
t.Errorf("got: <%v>, want: <%v>", filters, test.wantFilters) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestToString(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filters map[string]string | ||
wants []string | ||
}{ | ||
{"Zero filters", map[string]string{}, nil}, | ||
{"Single filter", map[string]string{"limit": "15"}, []string{"limit 15; "}}, | ||
{"Multiple filters", map[string]string{"limit": "15", "fields": "id,name,rating"}, []string{"limit 15; ", "fields id,name,rating; "}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got := toString(test.filters) | ||
|
||
for _, want := range test.wants { | ||
if !strings.Contains(got, want) { | ||
t.Errorf("got: <%v>, want: <%v>", got, want) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestToReader(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filters map[string]string | ||
wants []string | ||
}{ | ||
{"Zero filters", map[string]string{}, nil}, | ||
{"Single filter", map[string]string{"limit": "15"}, []string{"limit 15; "}}, | ||
{"Multiple filters", map[string]string{"limit": "15", "fields": "id,name,rating"}, []string{"limit 15; ", "fields id,name,rating; "}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
buf := bytes.Buffer{} | ||
buf.ReadFrom(toReader(test.filters)) | ||
got := buf.String() | ||
|
||
for _, want := range test.wants { | ||
if !strings.Contains(got, want) { | ||
t.Errorf("got: <%v>, want: <%v>", got, want) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.