add sort sanitization

This commit is contained in:
Dawid Wysokiński 2020-06-04 15:06:32 +02:00
parent 1f153b3fe5
commit 87f1b61367
10 changed files with 94 additions and 2 deletions

3
go.mod
View File

@ -7,12 +7,13 @@ require (
github.com/gin-gonic/gin v1.6.3
github.com/go-pg/pg/v10 v10.0.0-beta.1
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/go-redis/redis/v8 v8.0.0-beta.2 // indirect
github.com/joho/godotenv v1.3.0
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.9.1
github.com/segmentio/encoding v0.1.13 // indirect
github.com/tribalwarshelp/shared v0.0.0-20200603143626-e91530ae0889
github.com/tribalwarshelp/shared v0.0.0-20200603152200-61e5804c01ae
github.com/vektah/gqlparser/v2 v2.0.1
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
golang.org/x/sys v0.0.0-20200602100848-8d3cce7afc34 // indirect

2
go.sum
View File

@ -162,6 +162,8 @@ github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYm
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/tribalwarshelp/shared v0.0.0-20200603143626-e91530ae0889 h1:6wXJ4Hdh7NlLlTiAtQknHIZYoTZfYtU8nBXQvhUMRik=
github.com/tribalwarshelp/shared v0.0.0-20200603143626-e91530ae0889/go.mod h1:Ej+yG8aDKHPLRUVYxn7bT9zA0sdZZoGGgVJAAPGCK/o=
github.com/tribalwarshelp/shared v0.0.0-20200603152200-61e5804c01ae h1:1fMU6i/EIDZafe1Ae93rt+gbVkNf/xX/REU015CtUOI=
github.com/tribalwarshelp/shared v0.0.0-20200603152200-61e5804c01ae/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=

View File

@ -4,6 +4,8 @@ import (
"context"
"fmt"
"github.com/tribalwarshelp/api/utils"
"github.com/tribalwarshelp/api/langversion"
"github.com/tribalwarshelp/shared/models"
)
@ -25,6 +27,7 @@ func (ucase *usecase) Fetch(ctx context.Context, filter *models.LangVersionFilte
if filter.Limit > langversion.PaginationLimit || filter.Limit <= 0 {
filter.Limit = langversion.PaginationLimit
}
filter.Sort = utils.SanitizeSort(filter.Sort)
return ucase.repo.Fetch(ctx, filter)
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/tribalwarshelp/api/player"
"github.com/tribalwarshelp/api/utils"
"github.com/tribalwarshelp/shared/models"
)
@ -23,6 +24,7 @@ func (ucase *usecase) Fetch(ctx context.Context, server string, filter *models.P
if filter.Limit > player.PaginationLimit || filter.Limit <= 0 {
filter.Limit = player.PaginationLimit
}
filter.Sort = utils.SanitizeSort(filter.Sort)
return ucase.repo.Fetch(ctx, server, filter)
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/tribalwarshelp/api/server"
"github.com/tribalwarshelp/api/utils"
"github.com/tribalwarshelp/shared/models"
)
@ -23,6 +24,7 @@ func (ucase *usecase) Fetch(ctx context.Context, filter *models.ServerFilter) ([
if filter.Limit > server.PaginationLimit || filter.Limit <= 0 {
filter.Limit = server.PaginationLimit
}
filter.Sort = utils.SanitizeSort(filter.Sort)
return ucase.repo.Fetch(ctx, filter)
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/tribalwarshelp/api/tribe"
"github.com/tribalwarshelp/api/utils"
"github.com/tribalwarshelp/shared/models"
)
@ -23,6 +24,7 @@ func (ucase *usecase) Fetch(ctx context.Context, server string, filter *models.T
if filter.Limit > tribe.PaginationLimit || filter.Limit <= 0 {
filter.Limit = tribe.PaginationLimit
}
filter.Sort = utils.SanitizeSort(filter.Sort)
return ucase.repo.Fetch(ctx, server, filter)
}

17
utils/sanitize_sort.go Normal file
View File

@ -0,0 +1,17 @@
package utils
import "strings"
func SanitizeSort(sort string) string {
trimmed := strings.TrimSpace(sort)
splitted := strings.Split(trimmed, " ")
length := len(splitted)
if length < 1 {
return ""
}
keyword := "ASC"
if length == 2 && strings.ToUpper(splitted[1]) == "DESC" {
keyword = "DESC"
}
return strings.ToLower(Underscore(splitted[0])) + " " + keyword
}

61
utils/underscore.go Normal file
View File

@ -0,0 +1,61 @@
package utils
import (
"unicode"
"unicode/utf8"
)
type buffer struct {
r []byte
runeBytes [utf8.UTFMax]byte
}
func (b *buffer) write(r rune) {
if r < utf8.RuneSelf {
b.r = append(b.r, byte(r))
return
}
n := utf8.EncodeRune(b.runeBytes[0:], r)
b.r = append(b.r, b.runeBytes[0:n]...)
}
func (b *buffer) indent() {
if len(b.r) > 0 {
b.r = append(b.r, '_')
}
}
func Underscore(s string) string {
b := buffer{
r: make([]byte, 0, len(s)),
}
var m rune
var w bool
for _, ch := range s {
if unicode.IsUpper(ch) {
if m != 0 {
if !w {
b.indent()
w = true
}
b.write(m)
}
m = unicode.ToLower(ch)
} else {
if m != 0 {
b.indent()
b.write(m)
m = 0
w = false
}
b.write(ch)
}
}
if m != 0 {
if !w {
b.indent()
}
b.write(m)
}
return string(b.r)
}

View File

@ -1,5 +1,5 @@
package village
const (
PaginationLimit = 100
PaginationLimit = 1000
)

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/tribalwarshelp/api/utils"
"github.com/tribalwarshelp/api/village"
"github.com/tribalwarshelp/shared/models"
)
@ -23,6 +24,7 @@ func (ucase *usecase) Fetch(ctx context.Context, server string, filter *models.V
if filter.Limit > village.PaginationLimit || filter.Limit <= 0 {
filter.Limit = village.PaginationLimit
}
filter.Sort = utils.SanitizeSort(filter.Sort)
return ucase.repo.Fetch(ctx, server, filter)
}