Some refactorings

This commit is contained in:
Deluan 2023-03-26 21:26:55 -04:00
parent 05d381c26f
commit b998c05ca0
5 changed files with 66 additions and 43 deletions

View File

@ -135,7 +135,7 @@ func clientUniqueIDMiddleware(next http.Handler) http.Handler {
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteStrictMode,
Path: IfZero(conf.Server.BasePath, "/"),
Path: If(conf.Server.BasePath, "/"),
}
http.SetCookie(w, c)
} else {
@ -210,11 +210,11 @@ func serverAddress(r *http.Request) (scheme, host string) {
}
xfh = xfh[:i]
}
host = firstOr(r.Host, xfh)
host = FirstOr(r.Host, xfh)
// Determine the protocol and scheme of the request based on the presence of
// X-Forwarded-* headers or the scheme of the request URL.
scheme = firstOr(
scheme = FirstOr(
protocol,
r.Header.Get(xForwardedProto),
r.Header.Get(xForwardedScheme),
@ -231,17 +231,6 @@ func serverAddress(r *http.Request) (scheme, host string) {
return scheme, host
}
// firstOr is a helper function that returns the first non-empty string from a list
// of strings, or a default value if all the strings are empty.
func firstOr(or string, strings ...string) string {
for _, s := range strings {
if s != "" {
return s
}
}
return or
}
// URLParamsMiddleware is a middleware function that decodes the query string of
// the incoming HTTP request, adds the URL parameters from the routing context,
// and re-encodes the modified query string.

View File

@ -171,7 +171,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 = IfZero(conf.Server.BaseScheme, "http")
buildUrl.Scheme = If(conf.Server.BaseScheme, "http")
buildUrl.Host = conf.Server.BaseHost
} else {
buildUrl.Scheme = r.URL.Scheme

View File

@ -166,7 +166,7 @@ func getPlayer(players core.Players) func(next http.Handler) http.Handler {
MaxAge: consts.CookieExpiry,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Path: IfZero(conf.Server.BasePath, "/"),
Path: If(conf.Server.BasePath, "/"),
}
http.SetCookie(w, cookie)
}

View File

@ -1,14 +1,32 @@
// Package gg implements simple "extensions" to Go language. Based on https://github.com/icza/gog
package gg
// IfZero returns v if it is a non-zero value, orElse otherwise.
// 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 IfZero[T comparable](v T, orElse T) T {
func If[T comparable](v T, orElse T) T {
var zero T
if v != zero {
return v
}
return orElse
}
// FirstOr is a generic helper function that returns the first non-zero value from
// a list of comparable values, or a default value if all the values are zero.
func FirstOr[T comparable](or T, values ...T) T {
// Initialize a zero value of the same type as the input values.
var zero T
// Loop through each input value and check if it is non-zero. If a non-zero value
// is found, return it immediately.
for _, v := range values {
if v != zero {
return v
}
}
// If all the input values are zero, return the default value.
return or
}

View File

@ -15,29 +15,45 @@ func TestGG(t *testing.T) {
RunSpecs(t, "GG Suite")
}
var _ = Describe("IfZero", func() {
DescribeTable("string",
func(v, orElse, expected string) {
Expect(gg.IfZero(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.IfZero(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.IfZero(v, orElse)).To(Equal(expected))
},
Entry("zero value", testStruct{}, testStruct{123}, testStruct{123}),
Entry("non-zero value", testStruct{456}, testStruct{123}, testStruct{456}),
)
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("FirstOr", func() {
Context("when given a list of strings", func() {
It("returns the first non-empty value", func() {
Expect(gg.FirstOr("default", "foo", "bar", "baz")).To(Equal("foo"))
Expect(gg.FirstOr("default", "", "", "qux")).To(Equal("qux"))
})
It("returns the default value if all values are empty", func() {
Expect(gg.FirstOr("default", "", "", "")).To(Equal("default"))
Expect(gg.FirstOr("", "", "", "")).To(Equal(""))
})
})
})
})