From c8a30655cb43e19141db2f9f1a7613b954548b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Mon, 28 Oct 2024 13:59:26 +0100 Subject: [PATCH] chore: Enable `revive:enforce-map-style` rule (#16077) --- .golangci.yml | 3 +++ config/config.go | 2 +- internal/snmp/table.go | 10 +++++----- internal/snmp/translator_gosmi.go | 6 +++--- internal/templating/matcher.go | 2 +- models/buffer.go | 4 ++-- models/running_input.go | 6 +++--- plugins/inputs/win_eventlog/util.go | 2 +- plugins/inputs/win_eventlog/win_eventlog.go | 8 ++++---- plugins/inputs/win_wmi/method.go | 2 +- plugins/inputs/win_wmi/query.go | 2 +- selfstat/selfstat.go | 2 +- testutil/accumulator.go | 4 ++-- tools/custom_builder/config.go | 2 +- tools/license_checker/main.go | 4 ++-- 15 files changed, 31 insertions(+), 28 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 31859c348..b89b62b44 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -262,6 +262,9 @@ linters-settings: - name: early-return - name: empty-block - name: empty-lines + - name: enforce-map-style + arguments: ["make"] + exclude: [ "TEST" ] - name: error-naming - name: error-return - name: error-strings diff --git a/config/config.go b/config/config.go index 876f7d4f4..4f6da0c9f 100644 --- a/config/config.go +++ b/config/config.go @@ -115,7 +115,7 @@ func (op OrderedPlugins) Less(i, j int) bool { return op[i].Line < op[j].Line } // once the configuration is parsed. func NewConfig() *Config { c := &Config{ - UnusedFields: map[string]bool{}, + UnusedFields: make(map[string]bool), unusedFieldsMutex: &sync.Mutex{}, // Agent defaults: diff --git a/internal/snmp/table.go b/internal/snmp/table.go index 495ef16ad..6f7eed51d 100644 --- a/internal/snmp/table.go +++ b/internal/snmp/table.go @@ -103,7 +103,7 @@ func (t *Table) initBuild() error { t.Name = oidText } - knownOIDs := map[string]bool{} + knownOIDs := make(map[string]bool, len(t.Fields)) for _, f := range t.Fields { knownOIDs[f.Oid] = true } @@ -118,7 +118,7 @@ func (t *Table) initBuild() error { // Build retrieves all the fields specified in the table and constructs the RTable. func (t Table) Build(gs Connection, walk bool) (*RTable, error) { - rows := map[string]RTableRow{} + rows := make(map[string]RTableRow) // translation table for secondary index (when performing join on two tables) secIdxTab := make(map[string]string) @@ -151,7 +151,7 @@ func (t Table) Build(gs Connection, walk bool) (*RTable, error) { } // ifv contains a mapping of table OID index to field value - ifv := map[string]interface{}{} + ifv := make(map[string]interface{}) if !walk { // This is used when fetching non-table fields. Fields configured a the top @@ -240,8 +240,8 @@ func (t Table) Build(gs Connection, walk bool) (*RTable, error) { rtr, ok := rows[idx] if !ok { rtr = RTableRow{} - rtr.Tags = map[string]string{} - rtr.Fields = map[string]interface{}{} + rtr.Tags = make(map[string]string) + rtr.Fields = make(map[string]interface{}) rows[idx] = rtr } if t.IndexAsTag && idx != "" { diff --git a/internal/snmp/translator_gosmi.go b/internal/snmp/translator_gosmi.go index 88e4c2ae9..f85751649 100644 --- a/internal/snmp/translator_gosmi.go +++ b/internal/snmp/translator_gosmi.go @@ -92,10 +92,10 @@ func (g *gosmiTranslator) SnmpFormatDisplayHint(oid string, value interface{}) ( func getIndex(mibPrefix string, node gosmi.SmiNode) (col []string, tagOids map[string]struct{}) { // first attempt to get the table's tags - tagOids = map[string]struct{}{} - // mimcks grabbing INDEX {} that is returned from snmptranslate -Td MibName - for _, index := range node.GetIndex() { + indices := node.GetIndex() + tagOids = make(map[string]struct{}, len(indices)) + for _, index := range indices { tagOids[mibPrefix+index.Name] = struct{}{} } diff --git a/internal/templating/matcher.go b/internal/templating/matcher.go index 9e1770b55..c3c0b4289 100644 --- a/internal/templating/matcher.go +++ b/internal/templating/matcher.go @@ -21,7 +21,7 @@ func newMatcher(defaultTemplate *Template) *matcher { func (m *matcher) addSpec(tmplt templateSpec) error { // Parse out the default tags specific to this template - tags := map[string]string{} + tags := make(map[string]string) if tmplt.tagstring != "" { for _, kv := range strings.Split(tmplt.tagstring, ",") { parts := strings.Split(kv, "=") diff --git a/models/buffer.go b/models/buffer.go index 2f0aac7ce..7f4d679f9 100644 --- a/models/buffer.go +++ b/models/buffer.go @@ -10,8 +10,8 @@ import ( ) var ( - AgentMetricsWritten = selfstat.Register("agent", "metrics_written", map[string]string{}) - AgentMetricsDropped = selfstat.Register("agent", "metrics_dropped", map[string]string{}) + AgentMetricsWritten = selfstat.Register("agent", "metrics_written", make(map[string]string)) + AgentMetricsDropped = selfstat.Register("agent", "metrics_dropped", make(map[string]string)) registerGob = sync.OnceFunc(func() { metric.Init() }) ) diff --git a/models/running_input.go b/models/running_input.go index 7858d0e3e..d0e7ae64f 100644 --- a/models/running_input.go +++ b/models/running_input.go @@ -12,9 +12,9 @@ import ( ) var ( - GlobalMetricsGathered = selfstat.Register("agent", "metrics_gathered", map[string]string{}) - GlobalGatherErrors = selfstat.Register("agent", "gather_errors", map[string]string{}) - GlobalGatherTimeouts = selfstat.Register("agent", "gather_timeouts", map[string]string{}) + GlobalMetricsGathered = selfstat.Register("agent", "metrics_gathered", make(map[string]string)) + GlobalGatherErrors = selfstat.Register("agent", "gather_errors", make(map[string]string)) + GlobalGatherTimeouts = selfstat.Register("agent", "gather_timeouts", make(map[string]string)) ) type RunningInput struct { diff --git a/plugins/inputs/win_eventlog/util.go b/plugins/inputs/win_eventlog/util.go index 257a79c48..3f8639a8d 100644 --- a/plugins/inputs/win_eventlog/util.go +++ b/plugins/inputs/win_eventlog/util.go @@ -135,7 +135,7 @@ func walkXML(nodes []xmlnode, parents []string, separator string, f func(xmlnode // UniqueFieldNames forms unique field names // by adding _ if there are several of them func UniqueFieldNames(fields []EventField, fieldsUsage map[string]int, separator string) []EventField { - var fieldsCounter = map[string]int{} + var fieldsCounter = make(map[string]int, len(fields)) fieldsUnique := make([]EventField, 0, len(fields)) for _, field := range fields { fieldName := field.Name diff --git a/plugins/inputs/win_eventlog/win_eventlog.go b/plugins/inputs/win_eventlog/win_eventlog.go index b0eb3fed0..db812bb9c 100644 --- a/plugins/inputs/win_eventlog/win_eventlog.go +++ b/plugins/inputs/win_eventlog/win_eventlog.go @@ -156,10 +156,10 @@ func (w *WinEventLog) Gather(acc telegraf.Accumulator) error { for i := range events { // Prepare fields names usage counter - var fieldsUsage = map[string]int{} + fieldsUsage := make(map[string]int) - tags := map[string]string{} - fields := map[string]interface{}{} + tags := make(map[string]string) + fields := make(map[string]interface{}) event := events[i] evt := reflect.ValueOf(&event).Elem() timeStamp := time.Now() @@ -168,7 +168,7 @@ func (w *WinEventLog) Gather(acc telegraf.Accumulator) error { fieldName := evt.Type().Field(i).Name fieldType := evt.Field(i).Type().String() fieldValue := evt.Field(i).Interface() - computedValues := map[string]interface{}{} + computedValues := make(map[string]interface{}) switch fieldName { case "Source": fieldValue = event.Source.Name diff --git a/plugins/inputs/win_wmi/method.go b/plugins/inputs/win_wmi/method.go index 424775654..12ed60490 100644 --- a/plugins/inputs/win_wmi/method.go +++ b/plugins/inputs/win_wmi/method.go @@ -164,7 +164,7 @@ func (m *Method) execute(acc telegraf.Accumulator) error { defer outputPropertiesRaw.Clear() // Convert the results to fields and tags - tags, fields := map[string]string{}, map[string]interface{}{} + tags, fields := make(map[string]string), make(map[string]interface{}) // Add a source tag if we use remote queries if m.host != "" { diff --git a/plugins/inputs/win_wmi/query.go b/plugins/inputs/win_wmi/query.go index ae8d90195..89a664951 100644 --- a/plugins/inputs/win_wmi/query.go +++ b/plugins/inputs/win_wmi/query.go @@ -144,7 +144,7 @@ func (q *Query) execute(acc telegraf.Accumulator) error { } func (q *Query) extractProperties(acc telegraf.Accumulator, itemRaw *ole.VARIANT) error { - tags, fields := map[string]string{}, map[string]interface{}{} + tags, fields := make(map[string]string), make(map[string]interface{}) if q.host != "" { tags["source"] = q.host diff --git a/selfstat/selfstat.go b/selfstat/selfstat.go index 2cbcbf516..e88f1bd3f 100644 --- a/selfstat/selfstat.go +++ b/selfstat/selfstat.go @@ -84,7 +84,7 @@ func Metrics() []telegraf.Metric { if len(stats) > 0 { var tags map[string]string var name string - fields := map[string]interface{}{} + fields := make(map[string]interface{}, len(stats)) j := 0 for fieldname, stat := range stats { if j == 0 { diff --git a/testutil/accumulator.go b/testutil/accumulator.go index 5360e57a8..660520775 100644 --- a/testutil/accumulator.go +++ b/testutil/accumulator.go @@ -109,12 +109,12 @@ func (a *Accumulator) addMeasurement( return } - tagsCopy := map[string]string{} + tagsCopy := make(map[string]string, len(tags)) for k, v := range tags { tagsCopy[k] = v } - fieldsCopy := map[string]interface{}{} + fieldsCopy := make(map[string]interface{}, len(fields)) for k, v := range fields { fieldsCopy[k] = v } diff --git a/tools/custom_builder/config.go b/tools/custom_builder/config.go index 80025a3c0..ca01f4079 100644 --- a/tools/custom_builder/config.go +++ b/tools/custom_builder/config.go @@ -60,7 +60,7 @@ func ImportConfigurations(files, dirs []string) (*selection, int, error) { func (s *selection) Filter(p packageCollection) (*packageCollection, error) { enabled := packageCollection{ - packages: map[string][]packageInfo{}, + packages: make(map[string][]packageInfo), } implicitlyConfigured := make(map[string]bool) diff --git a/tools/license_checker/main.go b/tools/license_checker/main.go index b846d013f..1fa5d26de 100644 --- a/tools/license_checker/main.go +++ b/tools/license_checker/main.go @@ -178,8 +178,8 @@ func main() { // Get the superset of licenses if debug { - licenseSet := map[string]bool{} - licenseNames := []string{} + licenseSet := make(map[string]bool, len(packageInfos)) + licenseNames := make([]string, 0, len(packageInfos)) for _, info := range packageInfos { if found := licenseSet[info.license]; !found { licenseNames = append(licenseNames, info.license)