Skip to content

Commit

Permalink
test(linters): Enable copyloopvar
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel committed Jun 19, 2024
1 parent 80e94f7 commit c33b521
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 59 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters:
- asciicheck
- bidichk
- bodyclose
- copyloopvar
- depguard
- dogsled
- errcheck
Expand Down
1 change: 0 additions & 1 deletion plugins/common/cookie/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ func TestAuthConfig_Start(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
srv := newFakeServer(t)
c := &CookieAuthConfig{
Expand Down
17 changes: 4 additions & 13 deletions plugins/inputs/intel_pmu/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ func (ie *iaEntitiesValuesReader) readCoreEvents(entity *CoreEventEntity) ([]cor
metrics := make([]coreMetric, len(entity.activeEvents))
errGroup := errgroup.Group{}

for i, event := range entity.activeEvents {
id := i
actualEvent := event

if event == nil || event.PerfEvent == nil {
for id, actualEvent := range entity.activeEvents {
if actualEvent == nil || actualEvent.PerfEvent == nil {
return nil, errors.New("active event or corresponding perf event is nil")
}

Expand Down Expand Up @@ -170,10 +167,7 @@ func (ie *iaEntitiesValuesReader) readMultiEventSeparately(multiEvent multiEvent
metrics := make([]uncoreMetric, len(activeEvents))
group := errgroup.Group{}

for i, event := range activeEvents {
id := i
actualEvent := event

for id, actualEvent := range activeEvents {
group.Go(func() error {
values, err := ie.eventReader.readValue(actualEvent)
if err != nil {
Expand Down Expand Up @@ -211,10 +205,7 @@ func (ie *iaEntitiesValuesReader) readMultiEventAgg(multiEvent multiEvent) (unco
values := make([]ia.CounterValue, len(activeEvents))
group := errgroup.Group{}

for i, event := range activeEvents {
id := i
actualEvent := event

for id, actualEvent := range activeEvents {
group.Go(func() error {
value, err := ie.eventReader.readValue(actualEvent)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions plugins/inputs/neptune_apex/neptune_apex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestGather(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
var acc testutil.Accumulator
n.Servers = test.servers
Expand Down Expand Up @@ -360,7 +359,6 @@ func TestParseXML(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
var acc testutil.Accumulator
err := n.parseXML(&acc, test.xmlResponse)
Expand Down Expand Up @@ -403,7 +401,6 @@ func TestSendRequest(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
h := http.HandlerFunc(func(
Expand Down Expand Up @@ -462,7 +459,6 @@ func TestParseTime(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
res, err := parseTime(test.input, test.timeZone)
Expand Down Expand Up @@ -507,7 +503,6 @@ func TestFindProbe(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
index := findProbe(test.probeName, fakeProbes)
Expand Down
3 changes: 1 addition & 2 deletions plugins/inputs/x509_cert/x509_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,7 @@ func TestServerName(t *testing.T) {
{name: "errors", fromCfg: "otherex.com", fromTLS: "example.com", url: "https://other.example.com", err: true},
}

for _, elt := range tests {
test := elt
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
sc := &X509Cert{
Sources: []string{test.url},
Expand Down
3 changes: 1 addition & 2 deletions plugins/parsers/csv/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ func parseCSV(p *Parser, r io.Reader) ([]telegraf.Metric, error) {
continue
}
//concatenate header names
for i, h := range header {
name := h
for i, name := range header {
if p.TrimSpace {
name = strings.Trim(name, " ")
}
Expand Down
7 changes: 3 additions & 4 deletions plugins/parsers/graphite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (c *Config) validateTemplates() error {
// map to keep track of filters we see
filters := map[string]struct{}{}

for i, t := range c.Templates {
parts := strings.Fields(t)
for i, template := range c.Templates {
parts := strings.Fields(template)
// Ensure template string is non-empty
if len(parts) == 0 {
return fmt.Errorf("missing template at position: %d", i)
Expand All @@ -37,10 +37,9 @@ func (c *Config) validateTemplates() error {
}

if len(parts) > 3 {
return fmt.Errorf("invalid template format: %q", t)
return fmt.Errorf("invalid template format: %q", template)
}

template := t
filter := ""
tags := ""
if len(parts) >= 2 {
Expand Down
29 changes: 14 additions & 15 deletions plugins/parsers/json/json_flattener.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (f *JSONFlattener) FlattenJSON(

// FullFlattenJSON flattens nested maps/interfaces into a fields map (including bools and string)
func (f *JSONFlattener) FullFlattenJSON(
fieldname string,
fieldName string,
v interface{},
convertString bool,
convertBool bool,
Expand All @@ -33,45 +33,44 @@ func (f *JSONFlattener) FullFlattenJSON(

switch t := v.(type) {
case map[string]interface{}:
for k, v := range t {
fieldkey := k
if fieldname != "" {
fieldkey = fieldname + "_" + fieldkey
for fieldKey, fieldVal := range t {
if fieldName != "" {
fieldKey = fieldName + "_" + fieldKey
}

err := f.FullFlattenJSON(fieldkey, v, convertString, convertBool)
err := f.FullFlattenJSON(fieldKey, fieldVal, convertString, convertBool)
if err != nil {
return err
}
}
case []interface{}:
for i, v := range t {
fieldkey := strconv.Itoa(i)
if fieldname != "" {
fieldkey = fieldname + "_" + fieldkey
for i, fieldVal := range t {
fieldKey := strconv.Itoa(i)
if fieldName != "" {
fieldKey = fieldName + "_" + fieldKey
}
err := f.FullFlattenJSON(fieldkey, v, convertString, convertBool)
err := f.FullFlattenJSON(fieldKey, fieldVal, convertString, convertBool)
if err != nil {
return err
}
}
case float64:
f.Fields[fieldname] = t
f.Fields[fieldName] = t
case string:
if !convertString {
return nil
}
f.Fields[fieldname] = v.(string)
f.Fields[fieldName] = v.(string)
case bool:
if !convertBool {
return nil
}
f.Fields[fieldname] = v.(bool)
f.Fields[fieldName] = v.(bool)
case nil:
return nil
default:
return fmt.Errorf("JSON Flattener: got unexpected type %T with value %v (%s)",
t, t, fieldname)
t, t, fieldName)
}
return nil
}
15 changes: 2 additions & 13 deletions plugins/processors/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func (c *converter) convertTag(metric telegraf.Metric) {
tags[c.Tag] = tv
}

for key, value := range tags {
dest := key
for dest, value := range tags {
if c.Tag != "*" && c.Dest != "" {
dest = c.Dest
}
Expand Down Expand Up @@ -111,8 +110,7 @@ func (c *converter) convertField(metric telegraf.Metric) {
fields[c.Field] = fv
}

for key, value := range fields {
dest := key
for dest, value := range fields {
if c.Field != "*" && c.Dest != "" {
dest = c.Dest
}
Expand Down Expand Up @@ -194,7 +192,6 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.Trim {
c := c
if c.Cutset != "" {
c.fn = func(s string) string { return strings.Trim(s, c.Cutset) }
} else {
Expand All @@ -203,7 +200,6 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.TrimLeft {
c := c
if c.Cutset != "" {
c.fn = func(s string) string { return strings.TrimLeft(s, c.Cutset) }
} else {
Expand All @@ -212,7 +208,6 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.TrimRight {
c := c
if c.Cutset != "" {
c.fn = func(s string) string { return strings.TrimRight(s, c.Cutset) }
} else {
Expand All @@ -221,17 +216,14 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.TrimPrefix {
c := c
c.fn = func(s string) string { return strings.TrimPrefix(s, c.Prefix) }
s.converters = append(s.converters, c)
}
for _, c := range s.TrimSuffix {
c := c
c.fn = func(s string) string { return strings.TrimSuffix(s, c.Suffix) }
s.converters = append(s.converters, c)
}
for _, c := range s.Replace {
c := c
c.fn = func(s string) string {
newString := strings.ReplaceAll(s, c.Old, c.New)
if newString == "" {
Expand All @@ -243,7 +235,6 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.Left {
c := c
c.fn = func(s string) string {
if len(s) < c.Width {
return s
Expand All @@ -254,7 +245,6 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.Base64Decode {
c := c
c.fn = func(s string) string {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
Expand All @@ -268,7 +258,6 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.ValidUTF8 {
c := c
c.fn = func(s string) string { return strings.ToValidUTF8(s, c.Replacement) }
s.converters = append(s.converters, c)
}
Expand Down
8 changes: 4 additions & 4 deletions tools/config_includer/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ func main() {
}
pwd = string(filepath.Separator) + pwd
for _, iname := range extractIncludes(tmpl) {
ifn := iname
if !strings.HasPrefix(ifn, "/") {
ifn = filepath.Join(pwd, ifn)
if !strings.HasPrefix(iname, "/") {
newUnresolved[iname] = filepath.Join(pwd, iname)
} else {
newUnresolved[iname] = iname
}
newUnresolved[iname] = ifn
}
}
unresolved = newUnresolved
Expand Down

0 comments on commit c33b521

Please sign in to comment.