Added util.Min and util.Max helpers

This commit is contained in:
Philipp Wolfer 2023-12-11 22:48:08 +01:00
parent be1cfdac9e
commit c4da3a40cc
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
4 changed files with 73 additions and 12 deletions

View file

@ -26,6 +26,7 @@ import (
"go.uploadedlobster.com/scotty/internal/config"
"go.uploadedlobster.com/scotty/internal/i18n"
"go.uploadedlobster.com/scotty/internal/models"
"go.uploadedlobster.com/scotty/internal/util"
"golang.org/x/oauth2"
)
@ -105,10 +106,7 @@ out:
// and continue.
if offset >= result.Total {
p.Total = int64(result.Total)
offset = result.Total - perPage
if offset < 0 {
offset = 0
}
offset = util.Max(result.Total-perPage, 0)
continue
}
@ -177,10 +175,7 @@ out:
if offset >= result.Total {
p.Total = int64(result.Total)
totalCount = result.Total
offset = result.Total - perPage
if offset < 0 {
offset = 0
}
offset = util.Max(result.Total-perPage, 0)
continue
}

View file

@ -28,6 +28,7 @@ import (
"go.uploadedlobster.com/scotty/internal/config"
"go.uploadedlobster.com/scotty/internal/i18n"
"go.uploadedlobster.com/scotty/internal/models"
"go.uploadedlobster.com/scotty/internal/util"
"golang.org/x/oauth2"
"golang.org/x/oauth2/spotify"
)
@ -183,10 +184,7 @@ out:
if offset >= result.Total {
p.Total = int64(result.Total)
totalCount = result.Total
offset = result.Total - perPage
if offset < 0 {
offset = 0
}
offset = util.Max(result.Total-perPage, 0)
continue
}

34
internal/util/util.go Normal file
View file

@ -0,0 +1,34 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package util
import "golang.org/x/exp/constraints"
func Max[T constraints.Ordered](m, n T) T {
if n > m {
return n
} else {
return m
}
}
func Min[T constraints.Ordered](m, n T) T {
if n < m {
return n
} else {
return m
}
}

View file

@ -0,0 +1,34 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package util_test
import (
"fmt"
"go.uploadedlobster.com/scotty/internal/util"
)
func MaxExample() {
v := util.Max(2, 5)
fmt.Print(v)
// Output: 5
}
func MinExample() {
v := util.Min(2, 5)
fmt.Print(v)
// Output: 2
}