diff --git a/server/middlewares.go b/server/middlewares.go index 745f34577..62e5bb51e 100644 --- a/server/middlewares.go +++ b/server/middlewares.go @@ -12,11 +12,9 @@ import ( "time" "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/cors" "github.com/navidrome/navidrome/conf" - . "github.com/navidrome/navidrome/utils/gg" - - "github.com/go-chi/chi/v5/middleware" "github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model/request" @@ -139,7 +137,7 @@ func clientUniqueIDMiddleware(next http.Handler) http.Handler { HttpOnly: true, Secure: true, SameSite: http.SameSiteStrictMode, - Path: If(conf.Server.BasePath, "/"), + Path: cmp.Or(conf.Server.BasePath, "/"), } http.SetCookie(w, c) } else { diff --git a/server/server.go b/server/server.go index b0474f86d..aedb91950 100644 --- a/server/server.go +++ b/server/server.go @@ -1,6 +1,7 @@ package server import ( + "cmp" "context" "errors" "fmt" @@ -23,7 +24,6 @@ import ( "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/server/events" "github.com/navidrome/navidrome/ui" - . "github.com/navidrome/navidrome/utils/gg" ) type Server struct { @@ -237,7 +237,7 @@ func AbsoluteURL(r *http.Request, u string, params url.Values) string { if strings.HasPrefix(u, "/") { buildUrl.Path = path.Join(conf.Server.BasePath, buildUrl.Path) if conf.Server.BaseHost != "" { - buildUrl.Scheme = If(conf.Server.BaseScheme, "http") + buildUrl.Scheme = cmp.Or(conf.Server.BaseScheme, "http") buildUrl.Host = conf.Server.BaseHost } else { buildUrl.Scheme = r.URL.Scheme diff --git a/server/subsonic/middlewares.go b/server/subsonic/middlewares.go index 50d878b25..2c47893e4 100644 --- a/server/subsonic/middlewares.go +++ b/server/subsonic/middlewares.go @@ -1,6 +1,7 @@ package subsonic import ( + "cmp" "crypto/md5" "encoding/hex" "errors" @@ -20,7 +21,6 @@ import ( "github.com/navidrome/navidrome/model/request" "github.com/navidrome/navidrome/server" "github.com/navidrome/navidrome/server/subsonic/responses" - . "github.com/navidrome/navidrome/utils/gg" "github.com/navidrome/navidrome/utils/req" ) @@ -183,7 +183,7 @@ func getPlayer(players core.Players) func(next http.Handler) http.Handler { MaxAge: consts.CookieExpiry, HttpOnly: true, SameSite: http.SameSiteStrictMode, - Path: If(conf.Server.BasePath, "/"), + Path: cmp.Or(conf.Server.BasePath, "/"), } http.SetCookie(w, cookie) } diff --git a/utils/gg/gg.go b/utils/gg/gg.go index 2b9ebb9e6..5bb0990ca 100644 --- a/utils/gg/gg.go +++ b/utils/gg/gg.go @@ -1,18 +1,6 @@ // Package gg implements simple "extensions" to Go language. Based on https://github.com/icza/gog package gg -// If returns v if it is a non-zero value, orElse otherwise. -// -// This is similar to elvis operator (?:) in Groovy and other languages. -// Note: Different from the real elvis operator, the orElse expression will always get evaluated. -func If[T comparable](v T, orElse T) T { - var zero T - if v != zero { - return v - } - return orElse -} - // P returns a pointer to the input value func P[T any](v T) *T { return &v diff --git a/utils/gg/gg_test.go b/utils/gg/gg_test.go index 94c997eb0..511eb26c1 100644 --- a/utils/gg/gg_test.go +++ b/utils/gg/gg_test.go @@ -16,33 +16,6 @@ func TestGG(t *testing.T) { } var _ = Describe("GG", func() { - Describe("If", func() { - DescribeTable("string", - func(v, orElse, expected string) { - Expect(gg.If(v, orElse)).To(Equal(expected)) - }, - Entry("zero value", "", "default", "default"), - Entry("non-zero value", "anything", "default", "anything"), - ) - DescribeTable("numeric", - func(v, orElse, expected int) { - Expect(gg.If(v, orElse)).To(Equal(expected)) - }, - Entry("zero value", 0, 2, 2), - Entry("non-zero value", -1, 2, -1), - ) - type testStruct struct { - field1 int - } - DescribeTable("struct", - func(v, orElse, expected testStruct) { - Expect(gg.If(v, orElse)).To(Equal(expected)) - }, - Entry("zero value", testStruct{}, testStruct{123}, testStruct{123}), - Entry("non-zero value", testStruct{456}, testStruct{123}, testStruct{456}), - ) - }) - Describe("P", func() { It("returns a pointer to the input value", func() { v := 123