Skip to content

Commit

Permalink
Use slices.Clone for copying slices
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmi committed Nov 22, 2024
1 parent 48268f8 commit 9b6f1dc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
8 changes: 6 additions & 2 deletions group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "github.com/hansmi/prometheus-lvm-exporter/lvmreport"
import (
"slices"

"github.com/hansmi/prometheus-lvm-exporter/lvmreport"
)

type group struct {
name lvmreport.GroupName
Expand All @@ -13,7 +17,7 @@ type group struct {
}

func (r *group) allDescriptors() []*descriptor {
d := append([]*descriptor(nil), r.keyFields...)
d := slices.Clone(r.keyFields)
d = append(d, r.infoFields...)
d = append(d, r.metricFields...)
return d
Expand Down
2 changes: 1 addition & 1 deletion group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func checkSingleDescriptor(t *testing.T, g *group, d *descriptor) {
func checkReportFields(t *testing.T, g *group, fields []*descriptor) {
t.Helper()

sortedFields := append([]*descriptor(nil), fields...)
sortedFields := slices.Clone(fields)

slices.SortFunc(sortedFields, func(a, b *descriptor) int {
return strings.Compare(a.fieldName, b.fieldName)
Expand Down
4 changes: 2 additions & 2 deletions groupcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newGroupCollector(g *group) *groupCollector {
keyLabelNames = append(keyLabelNames, d.metricName)
}

infoLabelNames := append([]string(nil), keyLabelNames...)
infoLabelNames := slices.Clone(keyLabelNames)

for _, d := range g.infoFields {
c.infoFields = append(c.infoFields, d.fieldName)
Expand Down Expand Up @@ -103,7 +103,7 @@ func (c *groupCollector) collect(ch chan<- prometheus.Metric, data *lvmreport.Re
keyValues = append(keyValues, row[name])
}

infoValues := append([]string(nil), keyValues...)
infoValues := slices.Clone(keyValues)

for _, name := range c.infoFields {
infoValues = append(infoValues, row[name])
Expand Down
3 changes: 2 additions & 1 deletion lvmreport/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"slices"
"strings"

"github.com/kballard/go-shellquote"
Expand All @@ -32,7 +33,7 @@ func NewCommand(args []string) *Command {
}

func (c *Command) completeArgs() []string {
args := append([]string(nil), c.args...)
args := slices.Clone(c.args)

args = append(args,
"fullreport",
Expand Down

0 comments on commit 9b6f1dc

Please sign in to comment.