Skip to content

Commit

Permalink
chore: Fix linter findings for revive:enforce-slice-style in `plugi…
Browse files Browse the repository at this point in the history
…ns/parsers`, `plugins/processors`, `plugins/secretstores` and `plugins/serializers`
  • Loading branch information
zak-pawel committed Oct 4, 2024
1 parent 40d7961 commit bff4b53
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 83 deletions.
2 changes: 1 addition & 1 deletion plugins/parsers/collectd/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
return nil, fmt.Errorf("collectd parser error: %w", err)
}

metrics := []telegraf.Metric{}
metrics := make([]telegraf.Metric, 0, len(valueLists))
for _, valueList := range valueLists {
metrics = append(metrics, p.unmarshalValueList(valueList)...)
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/parsers/collectd/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestParse_SignSecurityLevel(t *testing.T) {

metrics, err = parser.Parse(bytes)
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Equal(t, make([]telegraf.Metric, 0), metrics)

// Wrong password error
buf, err = writeValueList(singleMetric.vl)
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestParse_EncryptSecurityLevel(t *testing.T) {

metrics, err := parser.Parse(bytes)
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Equal(t, make([]telegraf.Metric, 0), metrics)

// Encrypted data
buf, err = writeValueList(singleMetric.vl)
Expand All @@ -271,7 +271,7 @@ func TestParse_EncryptSecurityLevel(t *testing.T) {

metrics, err = parser.Parse(bytes)
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Equal(t, make([]telegraf.Metric, 0), metrics)

// Wrong password error
buf, err = writeValueList(singleMetric.vl)
Expand Down
4 changes: 2 additions & 2 deletions plugins/parsers/csv/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (record metadataPattern) Less(i, j int) bool {
func (p *Parser) initializeMetadataSeparators() error {
// initialize metadata
p.metadataTags = map[string]string{}
p.metadataSeparatorList = []string{}
p.metadataSeparatorList = make([]string, 0)

if p.MetadataRows <= 0 {
return nil
Expand All @@ -94,7 +94,7 @@ func (p *Parser) initializeMetadataSeparators() error {
return errors.New("csv_metadata_separators required when specifying csv_metadata_rows")
}

p.metadataSeparatorList = metadataPattern{}
p.metadataSeparatorList = make(metadataPattern, 0, len(p.MetadataSeparators))
patternList := map[string]bool{}
for _, pattern := range p.MetadataSeparators {
if patternList[pattern] {
Expand Down
6 changes: 3 additions & 3 deletions plugins/parsers/csv/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestHeaderOverride(t *testing.T) {
require.NoError(t, err)
metrics, err = p.Parse([]byte(testCSVRows[0]))
require.NoError(t, err)
require.Equal(t, []telegraf.Metric{}, metrics)
require.Equal(t, make([]telegraf.Metric, 0), metrics)
m, err := p.ParseLine(testCSVRows[1])
require.NoError(t, err)
require.Equal(t, "test_name", m.Name())
Expand Down Expand Up @@ -849,14 +849,14 @@ func TestParseMetadataSeparators(t *testing.T) {
p := &Parser{
ColumnNames: []string{"a", "b"},
MetadataRows: 0,
MetadataSeparators: []string{},
MetadataSeparators: make([]string, 0),
}
err := p.Init()
require.NoError(t, err)
p = &Parser{
ColumnNames: []string{"a", "b"},
MetadataRows: 1,
MetadataSeparators: []string{},
MetadataSeparators: make([]string, 0),
}
err = p.Init()
require.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/influx/influx_upstream/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (sp *StreamParser) Next() (telegraf.Metric, error) {

m, err := nextMetric(sp.decoder, sp.precision, sp.defaultTime, false)
if err != nil {
return nil, convertToParseError([]byte{}, err)
return nil, convertToParseError(make([]byte, 0), err)
}

return m, nil
Expand Down
6 changes: 3 additions & 3 deletions plugins/parsers/influx/influx_upstream/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func TestSeriesParser(t *testing.T) {
{
name: "empty",
input: []byte(""),
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
},
{
name: "minimal",
Expand Down Expand Up @@ -717,7 +717,7 @@ func TestSeriesParser(t *testing.T) {
{
name: "missing tag value",
input: []byte("cpu,a="),
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
err: &ParseError{
DecodeError: &lineprotocol.DecodeError{
Line: 1,
Expand All @@ -730,7 +730,7 @@ func TestSeriesParser(t *testing.T) {
{
name: "error with carriage return in long line",
input: []byte("cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b"),
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
err: &ParseError{
DecodeError: &lineprotocol.DecodeError{
Line: 1,
Expand Down
6 changes: 3 additions & 3 deletions plugins/parsers/influx/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ func TestSeriesParser(t *testing.T) {
{
name: "empty",
input: []byte(""),
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
},
{
name: "minimal",
Expand Down Expand Up @@ -795,7 +795,7 @@ func TestSeriesParser(t *testing.T) {
{
name: "missing tag value",
input: []byte("cpu,a="),
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
err: &ParseError{
Offset: 6,
LineNumber: 1,
Expand All @@ -807,7 +807,7 @@ func TestSeriesParser(t *testing.T) {
{
name: "error with carriage return in long line",
input: []byte("cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b"),
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
err: &ParseError{
Offset: 1031,
LineNumber: 1,
Expand Down
8 changes: 4 additions & 4 deletions plugins/parsers/json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func TestJSONQueryErrorOnArray(t *testing.T) {

parser := &Parser{
MetricName: "json_test",
TagKeys: []string{},
TagKeys: make([]string, 0),
Query: "shares.myArr",
}
require.NoError(t, parser.Init())
Expand Down Expand Up @@ -913,19 +913,19 @@ func TestParse(t *testing.T) {
name: "parse empty array",
parser: &Parser{},
input: []byte(`[]`),
expected: []telegraf.Metric{},
expected: make([]telegraf.Metric, 0),
},
{
name: "parse null",
parser: &Parser{},
input: []byte(`null`),
expected: []telegraf.Metric{},
expected: make([]telegraf.Metric, 0),
},
{
name: "parse null with query",
parser: &Parser{Query: "result.data"},
input: []byte(`{"error":null,"result":{"data":null,"items_per_page":10,"total_items":0,"total_pages":0}}`),
expected: []telegraf.Metric{},
expected: make([]telegraf.Metric, 0),
},
{
name: "parse simple array",
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/json_v2/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestMultipleConfigs(t *testing.T) {

func TestParserEmptyConfig(t *testing.T) {
plugin := &json_v2.Parser{
Configs: []json_v2.Config{},
Configs: make([]json_v2.Config, 0),
}

require.ErrorContains(t, plugin.Init(), "no configuration provided")
Expand Down
10 changes: 5 additions & 5 deletions plugins/parsers/logfmt/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestParse(t *testing.T) {
}{
{
name: "no bytes returns no metrics",
want: []telegraf.Metric{},
want: make([]telegraf.Metric, 0),
},
{
name: "test without trailing end",
Expand Down Expand Up @@ -104,27 +104,27 @@ func TestParse(t *testing.T) {
{
name: "keys without = or values are ignored",
bytes: []byte(`i am no data.`),
want: []telegraf.Metric{},
want: make([]telegraf.Metric, 0),
wantErr: false,
},
{
name: "keys without values are ignored",
bytes: []byte(`foo="" bar=`),
want: []telegraf.Metric{},
want: make([]telegraf.Metric, 0),
wantErr: false,
},
{
name: "unterminated quote produces error",
measurement: "testlog",
bytes: []byte(`bar=baz foo="bar`),
want: []telegraf.Metric{},
want: make([]telegraf.Metric, 0),
wantErr: true,
},
{
name: "malformed key",
measurement: "testlog",
bytes: []byte(`"foo=" bar=baz`),
want: []telegraf.Metric{},
want: make([]telegraf.Metric, 0),
wantErr: true,
},
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/nagios/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestTryAddState(t *testing.T) {
runErrF: func() error {
return nil
},
metrics: []telegraf.Metric{},
metrics: make([]telegraf.Metric, 0),
assertF: func(t *testing.T, metrics []telegraf.Metric) {
require.Len(t, metrics, 1)
m := metrics[0]
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/value/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (v *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
if v.DataType != "string" {
values := strings.Fields(vStr)
if len(values) < 1 {
return []telegraf.Metric{}, nil
return make([]telegraf.Metric, 0), nil
}
vStr = values[len(values)-1]
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/xpath/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func splitLastPathElement(query string) []string {

// Nothing left
if query == "" || query == "/" || query == "//" || query == "." {
return []string{}
return make([]string, 0)
}

separatorIdx := strings.LastIndex(query, "/")
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/xpath/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ func TestProtobufImporting(t *testing.T) {
ProtobufMessageDef: "person.proto",
ProtobufMessageType: "importtest.Person",
ProtobufImportPaths: []string{"testcases/protos"},
Configs: []Config{},
Configs: make([]Config, 0),
Log: testutil.Logger{Name: "parsers.protobuf"},
}
require.NoError(t, parser.Init())
Expand Down
2 changes: 1 addition & 1 deletion plugins/processors/aws_ec2/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestBasicStartupWithTagCacheSize(t *testing.T) {
func TestBasicInitNoTagsReturnAnError(t *testing.T) {
p := newAwsEc2Processor()
p.Log = &testutil.Logger{}
p.ImdsTags = []string{}
p.ImdsTags = make([]string, 0)
err := p.Init()
require.Error(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/processors/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestNoMetric(t *testing.T) {
}
require.NoError(t, plugin.Init())

input := []telegraf.Metric{}
input := make([]telegraf.Metric, 0)
require.Empty(t, plugin.Apply(input...))
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/processors/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (p *Parser) SetParser(parser telegraf.Parser) {
}

func (p *Parser) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
results := []telegraf.Metric{}
results := make([]telegraf.Metric, 0, len(metrics))
for _, metric := range metrics {
newMetrics := []telegraf.Metric{}
newMetrics := make([]telegraf.Metric, 0)
if !p.DropOriginal {
newMetrics = append(newMetrics, metric)
} else {
Expand Down
4 changes: 2 additions & 2 deletions plugins/processors/reverse_dns/rdnscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewReverseDNSCache(ttl, lookupTimeout time.Duration, workerPoolSize int) *R
ttl: ttl,
lookupTimeout: lookupTimeout,
cache: map[string]*dnslookup{},
expireList: []*dnslookup{},
expireList: make([]*dnslookup, 0),
maxWorkers: workerPoolSize,
sem: semaphore.NewWeighted(int64(workerPoolSize)),
cancelCleanupWorker: cancel,
Expand Down Expand Up @@ -272,7 +272,7 @@ func (d *ReverseDNSCache) cleanup() {
d.expireListLock.Unlock()
return
}
ipsToDelete := []string{}
ipsToDelete := make([]string, 0, len(d.expireList))
for i := 0; i < len(d.expireList); i++ {
if !d.expireList[i].expiresAt.Before(now) {
break // done. Nothing after this point is expired.
Expand Down
2 changes: 1 addition & 1 deletion plugins/processors/split/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Split) Init() error {
}

func (s *Split) Apply(in ...telegraf.Metric) []telegraf.Metric {
newMetrics := []telegraf.Metric{}
newMetrics := make([]telegraf.Metric, 0, len(in)*(len(s.Templates)+1))

for _, point := range in {
if s.DropOriginal {
Expand Down
Loading

0 comments on commit bff4b53

Please sign in to comment.