Skip to content

Commit

Permalink
Refactor utils to take options as argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesfabio committed Oct 21, 2015
1 parent 133a988 commit e17d379
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,35 @@ func ValidateLimitOffset(limit int64, offset int64) bool {
return true
}

// GenerateURI generates the pagination URI.
func GenerateURI(limit int64, offset int64) string {
return fmt.Sprintf(
"?%s=%d&%s=%d",
LimitKeyName,
limit,
OffsetKeyName,
offset)
}

// GetLimitFromRequest returns current limit.
func GetLimitFromRequest(request *http.Request, defaultLimit int64) int64 {
func GetLimitFromRequest(request *http.Request, options *Options) int64 {
var (
limit int64
err error
)

requestLimit := request.URL.Query().Get(LimitKeyName)
requestLimit := request.URL.Query().Get(options.LimitKeyName)

if requestLimit != "" {
limit, err = strconv.ParseInt(requestLimit, 10, 64)
if err != nil {
limit = defaultLimit
limit = options.DefaultLimit
}
} else {
limit = defaultLimit
limit = options.DefaultLimit
}

return limit
}

// GetOffsetFromRequest returns current offset.
func GetOffsetFromRequest(request *http.Request) int64 {
func GetOffsetFromRequest(request *http.Request, options *Options) int64 {
var (
offset int64
err error
)

requestOffset := request.URL.Query().Get(OffsetKeyName)
requestOffset := request.URL.Query().Get(options.OffsetKeyName)

if requestOffset != "" {
offset, err = strconv.ParseInt(requestOffset, 10, 64)
Expand All @@ -70,3 +60,13 @@ func GetOffsetFromRequest(request *http.Request) int64 {

return offset
}

// GenerateURI generates the pagination URI.
func GenerateURI(limit int64, offset int64, options *Options) string {
return fmt.Sprintf(
"?%s=%d&%s=%d",
options.LimitKeyName,
limit,
options.OffsetKeyName,
offset)
}

0 comments on commit e17d379

Please sign in to comment.