From c4da3a40cce53bf3bef955c48b66244d6c2c46d2 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Mon, 11 Dec 2023 22:48:08 +0100 Subject: [PATCH] Added util.Min and util.Max helpers --- internal/backends/deezer/deezer.go | 11 +++------ internal/backends/spotify/spotify.go | 6 ++--- internal/util/util.go | 34 ++++++++++++++++++++++++++++ internal/util/util_test.go | 34 ++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 internal/util/util.go create mode 100644 internal/util/util_test.go diff --git a/internal/backends/deezer/deezer.go b/internal/backends/deezer/deezer.go index c796af3..896b348 100644 --- a/internal/backends/deezer/deezer.go +++ b/internal/backends/deezer/deezer.go @@ -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 } diff --git a/internal/backends/spotify/spotify.go b/internal/backends/spotify/spotify.go index 9b623cb..b9a51e2 100644 --- a/internal/backends/spotify/spotify.go +++ b/internal/backends/spotify/spotify.go @@ -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 } diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 0000000..99826a1 --- /dev/null +++ b/internal/util/util.go @@ -0,0 +1,34 @@ +/* +Copyright © 2023 Philipp Wolfer + +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 . +*/ + +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 + } +} diff --git a/internal/util/util_test.go b/internal/util/util_test.go new file mode 100644 index 0000000..638e3a6 --- /dev/null +++ b/internal/util/util_test.go @@ -0,0 +1,34 @@ +/* +Copyright © 2023 Philipp Wolfer + +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 . +*/ + +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 +}