From de51a791dfee354f6146c1ad7f355116a2b64452 Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Thu, 24 Jul 2025 09:39:53 -0500 Subject: [PATCH] Apply fixes suggested by modernize The "modernize" analyzer of "gopls" also has a tool to apply fixes en masse. These are the changes produced by: modernize -category=-omitzero -fix -test ./... See: https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize --- internal/collector/config.go | 5 ++--- internal/controller/postgrescluster/controller_test.go | 6 +++--- internal/controller/postgrescluster/instance.go | 5 ++--- internal/controller/standalone_pgadmin/configmap.go | 9 +++------ internal/kubeapi/patch_test.go | 6 +++--- internal/logging/logrus.go | 8 ++++---- internal/patroni/config.go | 5 ++--- internal/pgadmin/config.go | 4 ++-- internal/pgadmin/config_test.go | 4 ++-- internal/pgadmin/reconcile_test.go | 2 +- internal/pgadmin/users.go | 2 +- internal/pgbouncer/config.go | 5 ++--- 12 files changed, 27 insertions(+), 34 deletions(-) diff --git a/internal/collector/config.go b/internal/collector/config.go index 758c5d3c11..dc1e579a70 100644 --- a/internal/collector/config.go +++ b/internal/collector/config.go @@ -8,6 +8,7 @@ import ( "context" _ "embed" "fmt" + "maps" "math" "strings" "time" @@ -168,9 +169,7 @@ func NewConfig(spec *v1beta1.InstrumentationSpec) *Config { // If there are exporters defined in the spec, add them to the config. if spec != nil && spec.Config != nil && spec.Config.Exporters != nil { - for k, v := range spec.Config.Exporters { - config.Exporters[k] = v - } + maps.Copy(config.Exporters, spec.Config.Exporters) } return config diff --git a/internal/controller/postgrescluster/controller_test.go b/internal/controller/postgrescluster/controller_test.go index 4bba89b56c..36759cd784 100644 --- a/internal/controller/postgrescluster/controller_test.go +++ b/internal/controller/postgrescluster/controller_test.go @@ -349,7 +349,7 @@ spec: MatchFields(IgnoreExtras, Fields{ "Manager": Equal(string(test.Reconciler.Owner)), "FieldsV1": PointTo(MatchAllFields(Fields{ - "Raw": WithTransform(func(in []byte) (out map[string]interface{}) { + "Raw": WithTransform(func(in []byte) (out map[string]any) { Expect(yaml.Unmarshal(in, &out)).To(Succeed()) return out }, MatchAllKeys(Keys{ @@ -367,7 +367,7 @@ spec: MatchFields(IgnoreExtras, Fields{ "Manager": Equal(string(test.Reconciler.Owner)), "FieldsV1": PointTo(MatchAllFields(Fields{ - "Raw": WithTransform(func(in []byte) (out map[string]interface{}) { + "Raw": WithTransform(func(in []byte) (out map[string]any) { Expect(yaml.Unmarshal(in, &out)).To(Succeed()) return out }, MatchAllKeys(Keys{ @@ -380,7 +380,7 @@ spec: MatchFields(IgnoreExtras, Fields{ "Manager": Equal(string(test.Reconciler.Owner)), "FieldsV1": PointTo(MatchAllFields(Fields{ - "Raw": WithTransform(func(in []byte) (out map[string]interface{}) { + "Raw": WithTransform(func(in []byte) (out map[string]any) { Expect(yaml.Unmarshal(in, &out)).To(Succeed()) return out }, MatchAllKeys(Keys{ diff --git a/internal/controller/postgrescluster/instance.go b/internal/controller/postgrescluster/instance.go index b3bf0b6f75..0c91ca7157 100644 --- a/internal/controller/postgrescluster/instance.go +++ b/internal/controller/postgrescluster/instance.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "io" + "maps" "sort" "strings" "time" @@ -321,9 +322,7 @@ func (r *Reconciler) observeInstances( if autogrow { for _, statusIS := range cluster.Status.InstanceSets { if statusIS.DesiredPGDataVolume != nil { - for k, v := range statusIS.DesiredPGDataVolume { - previousDesiredRequests[k] = v - } + maps.Copy(previousDesiredRequests, statusIS.DesiredPGDataVolume) } } } diff --git a/internal/controller/standalone_pgadmin/configmap.go b/internal/controller/standalone_pgadmin/configmap.go index ad0da80dfa..d2378802c3 100644 --- a/internal/controller/standalone_pgadmin/configmap.go +++ b/internal/controller/standalone_pgadmin/configmap.go @@ -9,6 +9,7 @@ import ( "context" "encoding/json" "fmt" + "maps" "slices" "sort" "strconv" @@ -124,9 +125,7 @@ func generateConfig(pgadmin *v1beta1.PGAdmin, } // Copy any specified settings over the defaults. - for k, v := range pgadmin.Spec.Config.Settings { - settings[k] = v - } + maps.Copy(settings, pgadmin.Spec.Config.Settings) // Write mandatory settings over any specified ones. // SERVER_MODE must always be enabled when running on a webserver. @@ -246,9 +245,7 @@ func generateGunicornConfig(pgadmin *v1beta1.PGAdmin, } // Copy any specified settings over the defaults. - for k, v := range pgadmin.Spec.Config.Gunicorn { - settings[k] = v - } + maps.Copy(settings, pgadmin.Spec.Config.Gunicorn) // Write mandatory settings over any specified ones. // - https://docs.gunicorn.org/en/latest/settings.html#workers diff --git a/internal/kubeapi/patch_test.go b/internal/kubeapi/patch_test.go index 91f6bdebd8..05bd140066 100644 --- a/internal/kubeapi/patch_test.go +++ b/internal/kubeapi/patch_test.go @@ -13,10 +13,10 @@ import ( "k8s.io/apimachinery/pkg/types" ) -func assertJSON(t testing.TB, expected interface{}, actual []byte) { +func assertJSON(t testing.TB, expected any, actual []byte) { t.Helper() - var e, a interface{} + var e, a any var err error if b, ok := expected.([]byte); ok { @@ -248,7 +248,7 @@ func TestMerge7386Equivalence(t *testing.T) { // one call using other types NewMergePatch(). - Add("metadata")(map[string]interface{}{ + Add("metadata")(map[string]any{ "labels": labels.Set{"lk": "lv"}, "annotations": map[string]string{"ak1": "av1", "ak2": "av2"}, }), diff --git a/internal/logging/logrus.go b/internal/logging/logrus.go index 19ca3e2aa3..6cdea3b06e 100644 --- a/internal/logging/logrus.go +++ b/internal/logging/logrus.go @@ -38,7 +38,7 @@ func Logrus(out io.Writer, version string, debug, verbosity int) logr.LogSink { return &sink{ verbosity: verbosity, - fnError: func(err error, message string, kv ...interface{}) { + fnError: func(err error, message string, kv ...any) { entry := root.WithField("version", version) entry = logrusFields(entry, kv...) @@ -57,7 +57,7 @@ func Logrus(out io.Writer, version string, debug, verbosity int) logr.LogSink { entry.Log(logrus.ErrorLevel, message) }, - fnInfo: func(level int, message string, kv ...interface{}) { + fnInfo: func(level int, message string, kv ...any) { entry := root.WithField("version", version) entry = logrusFields(entry, kv...) @@ -72,7 +72,7 @@ func Logrus(out io.Writer, version string, debug, verbosity int) logr.LogSink { // logrusFields structures and adds the key/value interface to the logrus.Entry; // for instance, if a key is not a string, this formats the key as a string. -func logrusFields(entry *logrus.Entry, kv ...interface{}) *logrus.Entry { +func logrusFields(entry *logrus.Entry, kv ...any) *logrus.Entry { if len(kv) == 0 { return entry } @@ -80,7 +80,7 @@ func logrusFields(entry *logrus.Entry, kv ...interface{}) *logrus.Entry { kv = append(kv, nil) } - m := make(map[string]interface{}, len(kv)/2) + m := make(map[string]any, len(kv)/2) for i := 0; i < len(kv); i += 2 { key, ok := kv[i].(string) diff --git a/internal/patroni/config.go b/internal/patroni/config.go index 3e6f7b6c83..61d3721ec2 100644 --- a/internal/patroni/config.go +++ b/internal/patroni/config.go @@ -6,6 +6,7 @@ package patroni import ( "fmt" + "maps" "path" "strings" @@ -237,9 +238,7 @@ func DynamicConfiguration( // Copy the "postgresql" section over the above defaults. if section, ok := root["postgresql"].(map[string]any); ok { - for k, v := range section { - postgresql[k] = v - } + maps.Copy(postgresql, section) } if m := parameters.AsMap(); m != nil { postgresql["parameters"] = m diff --git a/internal/pgadmin/config.go b/internal/pgadmin/config.go index d6ba5ce228..1715ee19bc 100644 --- a/internal/pgadmin/config.go +++ b/internal/pgadmin/config.go @@ -153,10 +153,10 @@ if os.path.isfile('` + ldapPasswordAbsolutePath + `'): } // systemSettings returns pgAdmin settings as a value that can be marshaled to JSON. -func systemSettings(spec *v1beta1.PGAdminPodSpec) map[string]interface{} { +func systemSettings(spec *v1beta1.PGAdminPodSpec) map[string]any { settings := spec.Config.Settings.DeepCopy() if settings == nil { - settings = make(map[string]interface{}) + settings = make(map[string]any) } // SERVER_MODE must always be enabled when running on a webserver. diff --git a/internal/pgadmin/config_test.go b/internal/pgadmin/config_test.go index 0e659c7070..7d072e1b5e 100644 --- a/internal/pgadmin/config_test.go +++ b/internal/pgadmin/config_test.go @@ -106,8 +106,8 @@ func TestSystemSettings(t *testing.T) { SERVER_MODE: true `)) - spec.Config.Settings = map[string]interface{}{ - "ALLOWED_HOSTS": []interface{}{"225.0.0.0/8", "226.0.0.0/7", "228.0.0.0/6"}, + spec.Config.Settings = map[string]any{ + "ALLOWED_HOSTS": []any{"225.0.0.0/8", "226.0.0.0/7", "228.0.0.0/6"}, } assert.Assert(t, cmp.MarshalMatches(systemSettings(spec), ` ALLOWED_HOSTS: diff --git a/internal/pgadmin/reconcile_test.go b/internal/pgadmin/reconcile_test.go index 6e4cccc73a..b9091edf37 100644 --- a/internal/pgadmin/reconcile_test.go +++ b/internal/pgadmin/reconcile_test.go @@ -47,7 +47,7 @@ pgadmin-settings.json: | t.Run("Customizations", func(t *testing.T) { cluster.Spec.UserInterface = new(v1beta1.UserInterfaceSpec) cluster.Spec.UserInterface.PGAdmin = new(v1beta1.PGAdminPodSpec) - cluster.Spec.UserInterface.PGAdmin.Config.Settings = map[string]interface{}{ + cluster.Spec.UserInterface.PGAdmin.Config.Settings = map[string]any{ "some": "thing", "UPPER_CASE": false, } diff --git a/internal/pgadmin/users.go b/internal/pgadmin/users.go index ef51978e8f..5e9c07a934 100644 --- a/internal/pgadmin/users.go +++ b/internal/pgadmin/users.go @@ -237,7 +237,7 @@ with create_app().app_context():`, spec := users[i] if err == nil { - err = encoder.Encode(map[string]interface{}{ + err = encoder.Encode(map[string]any{ "username": spec.Name, "password": passwords[spec.Name], }) diff --git a/internal/pgbouncer/config.go b/internal/pgbouncer/config.go index 99bcac0399..1c08e94803 100644 --- a/internal/pgbouncer/config.go +++ b/internal/pgbouncer/config.go @@ -7,6 +7,7 @@ package pgbouncer import ( "context" "fmt" + "maps" "sort" "strings" @@ -138,9 +139,7 @@ func clusterINI(ctx context.Context, cluster *v1beta1.PostgresCluster) string { } // Override the above with any specified settings. - for k, v := range cluster.Spec.Proxy.PGBouncer.Config.Global { - global[k] = v - } + maps.Copy(global, cluster.Spec.Proxy.PGBouncer.Config.Global) // Prevent the user from bypassing the main configuration file. global["conffile"] = iniFileAbsolutePath