Skip to content

Commit

Permalink
Add generic function to bind multiple slices w/ default
Browse files Browse the repository at this point in the history
  • Loading branch information
Swan committed Aug 1, 2024
1 parent 25a65c0 commit 57e99d8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
30 changes: 23 additions & 7 deletions db/elasticsearch_mapsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"github.com/Quaver/api2/enums"
"github.com/Quaver/api2/sliceutil"
"github.com/elastic/go-elasticsearch/v8/esutil"
"github.com/sirupsen/logrus"
"io"
Expand All @@ -17,11 +18,13 @@ import (
)

type ElasticMapsetSearchOptions struct {
Search string `form:"search" json:"search"`
RankedStatus []enums.RankedStatus `form:"ranked_status[]" json:"ranked_status"`
Mode []enums.GameMode `form:"mode[]" json:"mode"`
Page int `form:"page" json:"page"`
Limit int `form:"limit" json:"limit"`
Search string `form:"search" json:"search"`
RankedStatus []enums.RankedStatus `form:"ranked_status" json:"ranked_status"`
RankedStatusArray []enums.RankedStatus `form:"ranked_status[]" json:"ranked_status[]"`
Mode []enums.GameMode `form:"mode" json:"mode"`
ModeArray []enums.GameMode `form:"mode[]" json:"mode[]"`
Page int `form:"page" json:"page"`
Limit int `form:"limit" json:"limit"`

MinDifficultyRating float64 `form:"min_difficulty_rating" json:"min_difficulty_rating"`
MaxDifficultyRating float64 `form:"max_difficulty_rating" json:"max_difficulty_rating"`
Expand All @@ -45,8 +48,6 @@ type ElasticMapsetSearchOptions struct {
func NewElasticMapsetSearchOptions() *ElasticMapsetSearchOptions {
return &ElasticMapsetSearchOptions{
Search: "",
RankedStatus: []enums.RankedStatus{enums.RankedStatusRanked},
Mode: []enums.GameMode{enums.GameModeKeys4, enums.GameModeKeys7},
Limit: 50,
MinDifficultyRating: 0,
MaxDifficultyRating: math.MaxInt32,
Expand All @@ -68,6 +69,21 @@ func NewElasticMapsetSearchOptions() *ElasticMapsetSearchOptions {
}
}

func (options *ElasticMapsetSearchOptions) BindAndValidate() {
if options.Limit > 50 {
options.Limit = 50
}

options.Mode = sliceutil.BindMultiple(options.Mode, options.ModeArray, []enums.GameMode{
enums.GameModeKeys4,
enums.GameModeKeys7,
})

options.RankedStatus = sliceutil.BindMultiple(options.RankedStatus, options.RankedStatusArray, []enums.RankedStatus{
enums.RankedStatusRanked,
})
}

type ElasticMap struct {
*MapQua
Explicit bool `json:"explicit"`
Expand Down
4 changes: 1 addition & 3 deletions handlers/mapsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ func GetMapsetsSearch(c *gin.Context) *APIError {
return APIErrorBadRequest("Invalid request body")
}

if body.Limit > 50 {
body.Limit = 50
}
body.BindAndValidate()

mapsets, err := db.SearchElasticMapsets(body)

Expand Down
12 changes: 12 additions & 0 deletions sliceutil/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ func Keys[M ~map[K]V, K comparable, V any](m M) []K {
}
return r
}

func BindMultiple[T any](primary []T, secondary []T, defaults []T) []T {
if len(primary) > 0 {
return primary
}

if len(secondary) > 0 {
return secondary
}

return defaults
}

0 comments on commit 57e99d8

Please sign in to comment.