refactor: run Go modernize (#5002)

This commit is contained in:
Maximilian 2026-02-08 08:57:30 -06:00 • committed by GitHub
commit a704e86ac1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
102 changed files with 322 additions and 352 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"slices"
"strings"
"github.com/navidrome/navidrome/conf"
@ -35,9 +36,9 @@ var sensitiveFieldsFullMask = []string{
}
type configResponse struct {
ID string `json:"id"`
ConfigFile string `json:"configFile"`
Config map[string]interface{} `json:"config"`
ID string `json:"id"`
ConfigFile string `json:"configFile"`
Config map[string]any `json:"config"`
}
func redactValue(key string, value string) string {
@ -47,10 +48,8 @@ func redactValue(key string, value string) string {
}
// Check if this field should be fully masked
for _, field := range sensitiveFieldsFullMask {
if field == key {
return "****"
}
if slices.Contains(sensitiveFieldsFullMask, key) {
return "****"
}
// Check if this field should be partially masked
@ -69,7 +68,7 @@ func redactValue(key string, value string) string {
}
// applySensitiveFieldMasking recursively applies masking to sensitive fields in the configuration map
func applySensitiveFieldMasking(ctx context.Context, config map[string]interface{}, prefix string) {
func applySensitiveFieldMasking(ctx context.Context, config map[string]any, prefix string) {
for key, value := range config {
fullKey := key
if prefix != "" {
@ -77,7 +76,7 @@ func applySensitiveFieldMasking(ctx context.Context, config map[string]interface
}
switch v := value.(type) {
case map[string]interface{}:
case map[string]any:
// Recursively process nested maps
applySensitiveFieldMasking(ctx, v, fullKey)
case string:
@ -108,7 +107,7 @@ func getConfig(w http.ResponseWriter, r *http.Request) {
}
// Unmarshal back to map to get the structure with proper field names
var configMap map[string]interface{}
var configMap map[string]any
err = json.Unmarshal(configBytes, &configMap)
if err != nil {
log.Error(ctx, "Error unmarshaling config to map", err)

View file

@ -93,12 +93,12 @@ var _ = Describe("Config API", func() {
Expect(json.Unmarshal(w.Body.Bytes(), &resp)).To(Succeed())
// Check LastFM.ApiKey (partially masked)
lastfm, ok := resp.Config["LastFM"].(map[string]interface{})
lastfm, ok := resp.Config["LastFM"].(map[string]any)
Expect(ok).To(BeTrue())
Expect(lastfm["ApiKey"]).To(Equal("s*************3"))
// Check Spotify.Secret (partially masked)
spotify, ok := resp.Config["Spotify"].(map[string]interface{})
spotify, ok := resp.Config["Spotify"].(map[string]any)
Expect(ok).To(BeTrue())
Expect(spotify["Secret"]).To(Equal("s**************6"))
@ -109,7 +109,7 @@ var _ = Describe("Config API", func() {
Expect(resp.Config["DevAutoCreateAdminPassword"]).To(Equal("****"))
// Check Prometheus.Password (fully masked)
prometheus, ok := resp.Config["Prometheus"].(map[string]interface{})
prometheus, ok := resp.Config["Prometheus"].(map[string]any)
Expect(ok).To(BeTrue())
Expect(prometheus["Password"]).To(Equal("****"))
})
@ -128,7 +128,7 @@ var _ = Describe("Config API", func() {
Expect(json.Unmarshal(w.Body.Bytes(), &resp)).To(Succeed())
// Check LastFM.ApiKey - should be preserved because it's sensitive
lastfm, ok := resp.Config["LastFM"].(map[string]interface{})
lastfm, ok := resp.Config["LastFM"].(map[string]any)
Expect(ok).To(BeTrue())
Expect(lastfm["ApiKey"]).To(Equal(""))

View file

@ -95,7 +95,7 @@ func (api *Router) routes() http.Handler {
return r
}
func (api *Router) R(r chi.Router, pathPrefix string, model interface{}, persistable bool) {
func (api *Router) R(r chi.Router, pathPrefix string, model any, persistable bool) {
constructor := func(ctx context.Context) rest.Repository {
return api.ds.Resource(ctx, model)
}

View file

@ -218,7 +218,7 @@ func reorderItem(ds model.DataStore) http.HandlerFunc {
return
}
_, err = w.Write([]byte(fmt.Sprintf(`{"id":"%d"}`, id)))
_, err = w.Write(fmt.Appendf(nil, `{"id":"%d"}`, id))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

View file

@ -28,7 +28,7 @@ func newTranslationRepository(context.Context) rest.Repository {
type translationRepository struct{}
func (r *translationRepository) Read(id string) (interface{}, error) {
func (r *translationRepository) Read(id string) (any, error) {
translations, _ := loadTranslations()
if t, ok := translations[id]; ok {
return t, nil
@ -43,7 +43,7 @@ func (r *translationRepository) Count(...rest.QueryOptions) (int64, error) {
}
// ReadAll simple implementation, only returns IDs. Does not support any `options`
func (r *translationRepository) ReadAll(...rest.QueryOptions) (interface{}, error) {
func (r *translationRepository) ReadAll(...rest.QueryOptions) (any, error) {
translations, _ := loadTranslations()
var result []translation
for _, t := range translations {
@ -57,7 +57,7 @@ func (r *translationRepository) EntityName() string {
return "translation"
}
func (r *translationRepository) NewInstance() interface{} {
func (r *translationRepository) NewInstance() any {
return &translation{}
}
@ -103,7 +103,7 @@ func loadTranslation(fsys fs.FS, fileName string) (translation translation, err
if err != nil {
return
}
var out map[string]interface{}
var out map[string]any
if err = json.Unmarshal(data, &out); err != nil {
return
}

View file

@ -24,7 +24,7 @@ var _ = Describe("Translations", func() {
filePath := filepath.Join(consts.I18nFolder, name)
file, _ := fsys.Open(filePath)
data, _ := io.ReadAll(file)
var out map[string]interface{}
var out map[string]any
Expect(filepath.Ext(filePath)).To(Equal(".json"), filePath)
Expect(json.Unmarshal(data, &out)).To(BeNil(), filePath)
@ -40,7 +40,7 @@ var _ = Describe("Translations", func() {
Expect(err).To(BeNil())
Expect(tr.ID).To(Equal("en"))
Expect(tr.Name).To(Equal("English"))
var out map[string]interface{}
var out map[string]any
Expect(json.Unmarshal([]byte(tr.Data), &out)).To(BeNil())
})
})