Use ListOptions for parameters instead of functional options

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-08-05 13:25:09 -04:00
parent 23d572046c
commit 23ed51efcc
11 changed files with 325 additions and 307 deletions
+62
View File
@@ -360,6 +360,68 @@ type ListOptions struct {
Before string `url:"before,omitempty"`
}
// Value returns the value stored in the pointer.
func (o *ListOptions) Value() ListOptions {
if o == nil {
return ListOptions{}
}
return *o
}
// GetLimit returns the limit option.
func (o *ListOptions) GetLimit() int {
if o == nil {
return 0
}
return o.Limit
}
// GetAfter returns the after option.
func (o *ListOptions) GetAfter() string {
if o == nil {
return ""
}
return o.After
}
// GetBefore returns the before option.
func (o *ListOptions) GetBefore() string {
if o == nil {
return ""
}
return o.Before
}
// ListSubredditOptions defines possible options used when searching for subreddits.
type ListSubredditOptions struct {
ListOptions
// One of: relevance, activity.
Sort string `url:"sort,omitempty"`
}
// ListPostOptions defines possible options used when getting posts from a subreddit.
type ListPostOptions struct {
ListOptions
// One of: hour, day, week, month, year, all.
Time string `url:"t,omitempty"`
}
// ListPostSearchOptions defines possible options used when searching for posts within a subreddit.
type ListPostSearchOptions struct {
ListPostOptions
// One of: relevance, hot, top, new, comments.
Sort string `url:"sort,omitempty"`
}
// ListUserOverviewOptions defines possible options used when getting a user's post and/or comments.
type ListUserOverviewOptions struct {
ListOptions
// One of: hot, new, top, controversial.
Sort string `url:"sort,omitempty"`
// One of: hour, day, week, month, year, all.
Time string `url:"t,omitempty"`
}
func addOptions(s string, opt interface{}) (string, error) {
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Ptr && v.IsNil() {