From 57e99d8ee3cde3ddaf5dde304ecebba6df49d45f Mon Sep 17 00:00:00 2001 From: Swan Date: Thu, 1 Aug 2024 15:21:13 -0400 Subject: [PATCH] Add generic function to bind multiple slices w/ default --- db/elasticsearch_mapsets.go | 30 +++++++++++++++++++++++------- handlers/mapsets.go | 4 +--- sliceutil/slices.go | 12 ++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/db/elasticsearch_mapsets.go b/db/elasticsearch_mapsets.go index 9e659d8..2863ac0 100644 --- a/db/elasticsearch_mapsets.go +++ b/db/elasticsearch_mapsets.go @@ -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" @@ -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"` @@ -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, @@ -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"` diff --git a/handlers/mapsets.go b/handlers/mapsets.go index 837e1cd..92b5849 100644 --- a/handlers/mapsets.go +++ b/handlers/mapsets.go @@ -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) diff --git a/sliceutil/slices.go b/sliceutil/slices.go index 6966644..fc2cb93 100644 --- a/sliceutil/slices.go +++ b/sliceutil/slices.go @@ -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 +}