From 3679b6215d0a4c99377aa0bbc9e8f511b969a2fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deluan=20Quint=C3=A3o?= Date: Sun, 15 Nov 2026 17:40:12 -0500 Subject: [PATCH] refactor(agents): remove typed-nil workarounds from Last.fm and ListenBrainz (#6476) --- adapters/lastfm/agent.go | 25 ++++++++++--------------- adapters/lastfm/agent_test.go | 31 ++++++++++++++----------------- adapters/listenbrainz/agent.go | 17 ++--------------- 3 files changed, 26 insertions(+), 47 deletions(-) diff --git a/adapters/lastfm/agent.go b/adapters/lastfm/agent.go index 4a61baa71..362801c80 100644 --- a/adapters/lastfm/agent.go +++ b/adapters/lastfm/agent.go @@ -49,10 +49,11 @@ type lastfmAgent struct { getInfoMutex sync.Mutex } +func lastFMEnabled() bool { + return conf.Server().LastFM.Enabled && conf.Server().LastFM.ApiKey != "" && conf.Server().LastFM.Secret != "" +} + func lastFMConstructor(ds model.DataStore) *lastfmAgent { - if !conf.Server().LastFM.Enabled || conf.Server().LastFM.ApiKey == "" || conf.Server().LastFM.Secret == "" { - return nil - } l := &lastfmAgent{ ds: ds, languages: conf.Server().LastFM.Languages, @@ -431,22 +432,16 @@ func (l *lastfmAgent) PlaybackReport(context.Context, scrobbler.PlaybackSession) func init() { conf.AddHook(func() { agents.Register(lastFMAgentName, func(ds model.DataStore) agents.Interface { - // This is a workaround for the fact that a (Interface)(nil) is not the same as a (*lastfmAgent)(nil) - // See https://go.dev/doc/faq#nil_error - a := lastFMConstructor(ds) - if a != nil { - return a + if !lastFMEnabled() { + return nil } - return nil + return lastFMConstructor(ds) }) scrobbler.Register(lastFMAgentName, func(ds model.DataStore) scrobbler.Scrobbler { - // Same as above - this is a workaround for the fact that a (Scrobbler)(nil) is not the same as a (*lastfmAgent)(nil) - // See https://go.dev/doc/faq#nil_error - a := lastFMConstructor(ds) - if a != nil { - return a + if !lastFMEnabled() { + return nil } - return nil + return lastFMConstructor(ds) }) }) } diff --git a/adapters/lastfm/agent_test.go b/adapters/lastfm/agent_test.go index bc8ef3608..fbfaefe47 100644 --- a/adapters/lastfm/agent_test.go +++ b/adapters/lastfm/agent_test.go @@ -47,24 +47,21 @@ var _ = Describe("lastfmAgent", func() { Expect(agent.languages).To(Equal([]string{"pt", "en"})) }) }) - When("Agent is disabled", func() { - It("returns nil", func() { - conf.Update(func(c *conf.Options) { c.LastFM.Enabled = false }) - Expect(lastFMConstructor(ds)).To(BeNil()) - }) - }) - When("ApiKey is empty", func() { - It("returns nil", func() { - conf.Update(func(c *conf.Options) { c.LastFM.ApiKey = "" }) - Expect(lastFMConstructor(ds)).To(BeNil()) - }) - }) - When("Secret is empty", func() { - It("returns nil", func() { - conf.Update(func(c *conf.Options) { c.LastFM.Secret = "" }) - Expect(lastFMConstructor(ds)).To(BeNil()) - }) + }) + + Describe("lastFMEnabled", func() { + It("is true when enabled with an api key and secret", func() { + Expect(lastFMEnabled()).To(BeTrue()) }) + DescribeTable("is false when the configuration is incomplete", + func(change func(*conf.Options)) { + conf.Update(change) + Expect(lastFMEnabled()).To(BeFalse()) + }, + Entry("disabled", func(c *conf.Options) { c.LastFM.Enabled = false }), + Entry("no api key", func(c *conf.Options) { c.LastFM.ApiKey = "" }), + Entry("no secret", func(c *conf.Options) { c.LastFM.Secret = "" }), + ) }) Describe("GetArtistBiography", func() { diff --git a/adapters/listenbrainz/agent.go b/adapters/listenbrainz/agent.go index ac2c94187..a52dd6205 100644 --- a/adapters/listenbrainz/agent.go +++ b/adapters/listenbrainz/agent.go @@ -231,23 +231,10 @@ func init() { conf.AddHook(func() { if conf.Server().ListenBrainz.Enabled { scrobbler.Register(listenBrainzAgentName, func(ds model.DataStore) scrobbler.Scrobbler { - // This is a workaround for the fact that a (Interface)(nil) is not the same as a (*listenBrainzAgent)(nil) - // See https://go.dev/doc/faq#nil_error - a := listenBrainzConstructor(ds) - if a != nil { - return a - } - return nil + return listenBrainzConstructor(ds) }) - agents.Register(listenBrainzAgentName, func(ds model.DataStore) agents.Interface { - // This is a workaround for the fact that a (Interface)(nil) is not the same as a (*listenBrainzAgent)(nil) - // See https://go.dev/doc/faq#nil_error - a := listenBrainzConstructor(ds) - if a != nil { - return a - } - return nil + return listenBrainzConstructor(ds) }) } })