Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run gopls modernize tool #34507

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions cmd/agent/common/autodiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"path/filepath"
"slices"
"time"

"go.uber.org/atomic"
Expand Down Expand Up @@ -277,12 +278,7 @@ func waitForConfigsFromAD(ctx context.Context,
} else {
// match configs with names in checkNames
match = func(cfg integration.Config) bool {
for _, checkName := range checkNames {
if cfg.Name == checkName {
return true
}
}
return false
return slices.Contains(checkNames, cfg.Name)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package v1

import (
"encoding/json"
"maps"
"net/http"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -67,9 +68,7 @@ func getCLCRunnerStats(w http.ResponseWriter, _ *http.Request, ac autodiscovery.
func flattenCLCStats(stats status.CLCChecks) map[string]status.CLCStats {
flatened := make(map[string]status.CLCStats)
for _, checks := range stats.Checks {
for checkID, checkStats := range checks {
flatened[checkID] = checkStats
}
maps.Copy(flatened, checks)
}

return flatened
Expand Down
5 changes: 2 additions & 3 deletions cmd/serverless-init/cloudservice/appservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package cloudservice

import (
"maps"
"os"

"github.com/DataDog/datadog-agent/pkg/trace/traceutil"
Expand Down Expand Up @@ -38,9 +39,7 @@ func (a *AppService) GetTags() map[string]string {
"_dd.origin": a.GetOrigin(),
}

for key, value := range traceutil.GetAppServicesTags() {
tags[key] = value
}
maps.Copy(tags, traceutil.GetAppServicesTags())

return tags
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/serverless-init/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package tag

import (
"maps"
"os"
"strings"

Expand Down Expand Up @@ -52,9 +53,7 @@ func GetBaseTagsMapWithMetadata(metadata map[string]string, versionMode string)
}
}

for key, value := range metadata {
tagsMap[key] = value
}
maps.Copy(tagsMap, metadata)

tagsMap[versionMode] = tags.GetExtensionVersion()
tagsMap[tags.ComputeStatsKey] = tags.ComputeStatsValue
Expand Down
3 changes: 2 additions & 1 deletion cmd/trace-agent/test/testsuite/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package testsuite

import (
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -97,7 +98,7 @@ func TestTraces(t *testing.T) {
t.Fatal(err)
}
waitForTrace(t, &r, func(v *pb.AgentPayload) {
payloadsEqual(t, append(p[:2], p[3:]...), v)
payloadsEqual(t, slices.Delete(p, 2, 3), v)
})
})

Expand Down
5 changes: 2 additions & 3 deletions comp/core/autodiscovery/autodiscoveryimpl/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package autodiscoveryimpl

import (
"expvar"
"maps"
"sync"

"github.com/mohae/deepcopy"
Expand Down Expand Up @@ -63,9 +64,7 @@ func (es *acErrorStats) getConfigErrors() map[string]string {
defer es.m.RUnlock()

configCopy := make(map[string]string)
for k, v := range es.config {
configCopy[k] = v
}
maps.Copy(configCopy, es.config)

return configCopy
}
Expand Down
5 changes: 2 additions & 3 deletions comp/core/autodiscovery/autodiscoveryimpl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package autodiscoveryimpl

import (
"maps"
"sync"

"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
Expand Down Expand Up @@ -157,9 +158,7 @@ func (s *store) setIDsOfChecksWithSecrets(checkIDs map[checkid.ID]checkid.ID) {
s.m.Lock()
defer s.m.Unlock()

for idWithResolvedSecrets, idWithEncryptedSecrets := range checkIDs {
s.idsOfChecksWithSecrets[idWithResolvedSecrets] = idWithEncryptedSecrets
}
maps.Copy(s.idsOfChecksWithSecrets, checkIDs)
}

func (s *store) getIDOfCheckWithEncryptedSecrets(idCheckWithResolvedSecrets checkid.ID) checkid.ID {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package autodiscoveryimpl

import (
"fmt"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -111,7 +112,7 @@ func (cache *templateCache) del(tpl integration.Config) error {
} else {
for i, digest := range digests {
if digest == d {
cache.adIDToDigests[id] = append(digests[:i], digests[i+1:]...)
cache.adIDToDigests[id] = slices.Delete(digests, i, i+1)
break
}
}
Expand Down
5 changes: 2 additions & 3 deletions comp/core/autodiscovery/listeners/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package listeners
import (
"fmt"
"hash/fnv"
"maps"
"strconv"

"github.com/DataDog/datadog-agent/comp/core/autodiscovery/common/types"
Expand Down Expand Up @@ -116,9 +117,7 @@ func getPrometheusIncludeAnnotations() types.PrometheusAnnotations {
log.Errorf("Couldn't init check configuration: %v", err)
continue
}
for k, v := range check.AD.GetIncludeAnnotations() {
annotations[k] = v
}
maps.Copy(annotations, check.AD.GetIncludeAnnotations())
}
return annotations
}
5 changes: 2 additions & 3 deletions comp/core/autodiscovery/listeners/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package listeners

import (
"errors"
"maps"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -151,9 +152,7 @@ func (l *ContainerListener) createContainerService(entity workloadmeta.Entity) {
}

hosts := make(map[string]string)
for host, ip := range container.NetworkIPs {
hosts[host] = ip
}
maps.Copy(hosts, container.NetworkIPs)

if rancherIP, ok := docker.FindRancherIPInLabels(container.Labels); ok {
hosts["rancher"] = rancherIP
Expand Down
8 changes: 2 additions & 6 deletions comp/core/autodiscovery/providers/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -454,12 +455,7 @@ func GetIntegrationConfigFromFile(name, fpath string) (integration.Config, error
}

func containsString(slice []string, str string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
return slices.Contains(slice, str)
}

// ResetReader is only for unit tests
Expand Down
8 changes: 2 additions & 6 deletions comp/core/autodiscovery/providers/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"math"
"net/url"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -287,12 +288,7 @@ func (p *ConsulConfigProvider) getJSONValue(ctx context.Context, key string) ([]
func isTemplateField(key string) bool {
tplKeys := []string{instancePath, checkNamePath, initConfigPath}

for _, tpl := range tplKeys {
if key == tpl {
return true
}
}
return false
return slices.Contains(tplKeys, key)
}

// GetConfigErrors is not implemented for the ConsulConfigProvider
Expand Down
9 changes: 3 additions & 6 deletions comp/core/autodiscovery/providers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package providers
import (
"context"
"fmt"
"maps"
"strings"
"sync"

Expand Down Expand Up @@ -113,9 +114,7 @@ func (k *ContainerConfigProvider) processEvents(evBundle workloadmeta.EventBundl
}

configsToUnschedule := make(map[string]integration.Config)
for digest, config := range configCache {
configsToUnschedule[digest] = config
}
maps.Copy(configsToUnschedule, configCache)

for _, config := range configs {
digest := config.Digest()
Expand Down Expand Up @@ -282,9 +281,7 @@ func (k *ContainerConfigProvider) GetConfigErrors() map[string]ErrorMsgSet {

errors := make(map[string]ErrorMsgSet, len(k.configErrors))

for entity, errset := range k.configErrors {
errors[entity] = errset
}
maps.Copy(errors, k.configErrors)

return errors
}
Expand Down
5 changes: 2 additions & 3 deletions comp/core/autodiscovery/providers/remote_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -83,9 +84,7 @@ func (rc *RemoteConfigProvider) GetConfigErrors() map[string]ErrorMsgSet {

errors := make(map[string]ErrorMsgSet, len(rc.configErrors))

for entity, errset := range rc.configErrors {
errors[entity] = errset
}
maps.Copy(errors, rc.configErrors)

return errors
}
Expand Down
13 changes: 3 additions & 10 deletions comp/core/secrets/secretsimpl/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/json"
"fmt"
"io"
stdmaps "maps"
"net/http"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -405,13 +406,7 @@ func (r *secretResolver) matchesAllowlist(handle string) bool {
if !allowlistEnabled {
return true
}
for _, secretCtx := range r.origin[handle] {
if secretMatchesAllowlist(secretCtx) {
return true
}
}
// the handle does not appear for a setting that is in the allowlist
return false
return slices.ContainsFunc(r.origin[handle], secretMatchesAllowlist)
}

// for all secrets returned by the backend command, notify subscribers (if allowlist lets them),
Expand Down Expand Up @@ -451,9 +446,7 @@ func (r *secretResolver) processSecretResponse(secretResponse map[string]string,
handleInfoList = append(handleInfoList, handleInfo{Name: handle, Places: places})
}
// add results to the cache
for handle, secretValue := range secretResponse {
r.cache[handle] = secretValue
}
stdmaps.Copy(r.cache, secretResponse)
// return info about the handles sorted by their name
sort.Slice(handleInfoList, func(i, j int) bool {
return handleInfoList[i].Name < handleInfoList[j].Name
Expand Down
5 changes: 2 additions & 3 deletions comp/core/settings/settingsimpl/settingsimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package settingsimpl

import (
"html"
"maps"
"net/http"
"sync"

Expand Down Expand Up @@ -63,9 +64,7 @@ func (s *settingsRegistry) RuntimeSettings() map[string]settings.RuntimeSetting
s.rwMutex.RLock()
defer s.rwMutex.RUnlock()
settingsCopy := map[string]settings.RuntimeSetting{}
for k, v := range s.settings {
settingsCopy[k] = v
}
maps.Copy(settingsCopy, s.settings)
return settingsCopy
}

Expand Down
4 changes: 1 addition & 3 deletions comp/core/status/statusimpl/common_header_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ func (h *headerProvider) Name() string {
}

func (h *headerProvider) JSON(_ bool, stats map[string]interface{}) error {
for k, v := range h.data() {
stats[k] = v
}
maps.Copy(stats, h.data())

return nil
}
Expand Down
9 changes: 3 additions & 6 deletions comp/core/status/statusimpl/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"io"
"maps"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -50,9 +51,7 @@ func (m mockProvider) JSON(_ bool, stats map[string]interface{}) error {
return fmt.Errorf("JSON error")
}

for key, value := range m.data {
stats[key] = value
}
maps.Copy(stats, m.data)

return nil
}
Expand Down Expand Up @@ -97,9 +96,7 @@ func (m mockHeaderProvider) JSON(_ bool, stats map[string]interface{}) error {
return fmt.Errorf("JSON error")
}

for key, value := range m.data {
stats[key] = value
}
maps.Copy(stats, m.data)

return nil
}
Expand Down
Loading
Loading