Skip to content

refactor: replace x/exp/maps with stdlib maps #45274

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ linters:
deny:
- pkg: math/rand$
desc: superseded by math/rand/v2
- pkg: golang.org/x/exp/maps$
desc: superseded by maps
- pkg: golang.org/x/exp/slices$
desc: superseded by slices
errcheck:
check-type-assertions: true
check-blank: false
Expand Down
74 changes: 37 additions & 37 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28128,43 +28128,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/exp
Version: v0.0.0-20250106191152-7588d65b2ba8
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/golang.org/x/exp@v0.0.0-20250106191152-7588d65b2ba8/LICENSE:

Copyright 2009 The Go Authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/mod
Version: v0.25.0
Expand Down Expand Up @@ -80142,6 +80105,43 @@ See the License for the specific language governing permissions and
limitations under the License.


--------------------------------------------------------------------------------
Dependency : golang.org/x/exp
Version: v0.0.0-20250106191152-7588d65b2ba8
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/golang.org/x/exp@v0.0.0-20250106191152-7588d65b2ba8/LICENSE:

Copyright 2009 The Go Authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/xerrors
Version: v0.0.0-20240903120638-7835f813f4da
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ require (
go.opentelemetry.io/collector/pdata v1.36.0
go.opentelemetry.io/collector/receiver v1.36.0
go.uber.org/mock v0.5.0
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
golang.org/x/term v0.32.0
google.golang.org/genproto/googleapis/api v0.0.0-20250603155806-513f23925822
gopkg.in/natefinch/lumberjack.v2 v2.2.1
Expand Down Expand Up @@ -489,6 +488,7 @@ require (
go.uber.org/ratelimit v0.3.1 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
gonum.org/v1/gonum v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250603155806-513f23925822 // indirect
Expand Down
6 changes: 3 additions & 3 deletions x-pack/metricbeat/module/autoops_es/cat_shards/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package cat_shards

import (
"maps"
"slices"
"time"

"golang.org/x/exp/maps"

"github.com/elastic/beats/v7/x-pack/metricbeat/module/autoops_es/utils"
)

Expand Down Expand Up @@ -101,7 +101,7 @@ func enrichNodeIndexShards(nodeIndexShardsMap map[string]NodeIndexShards, indexM
timestampDiff = &diff
}

nodeIndexShardsList := maps.Values(nodeIndexShardsMap)
nodeIndexShardsList := slices.Collect(maps.Values(nodeIndexShardsMap))

for i := range nodeIndexShardsList {
nodeIndexShardsList[i].TotalFractions = size
Expand Down
6 changes: 3 additions & 3 deletions x-pack/metricbeat/module/autoops_es/cat_shards/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package cat_shards
import (
"encoding/json"
"fmt"
"maps"
"math"
"slices"

"github.com/elastic/beats/v7/x-pack/metricbeat/module/autoops_es/events"

"golang.org/x/exp/maps"

"github.com/elastic/elastic-agent-libs/mapstr"

"github.com/elastic/beats/v7/metricbeat/mb"
Expand Down Expand Up @@ -82,7 +82,7 @@ func eventsMapping(m *elasticsearch.MetricSet, r mb.ReporterV2, info *utils.Clus

transactionID := utils.NewUUIDV4()

sendNodeShardsEvent(r, info, maps.Values(nodeShards), transactionID)
sendNodeShardsEvent(r, info, slices.Collect(maps.Values(nodeShards)), transactionID)

indexMetadata, err := getResolvedIndices(m)

Expand Down
6 changes: 3 additions & 3 deletions x-pack/metricbeat/module/autoops_es/cat_shards/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package cat_shards

import (
"maps"
"slices"
"testing"

Expand All @@ -16,7 +17,6 @@ import (
"github.com/elastic/beats/v7/x-pack/metricbeat/module/autoops_es/metricset"

"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

var (
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestSendNodeShardsEvent(t *testing.T) {
func TestSendNodeIndexShardsEventInBatch(t *testing.T) {
reporter := &mbtest.CapturingReporterV2{}
info := auto_ops_testing.CreateClusterInfo("8.15.3")
nodeIndexShards := maps.Values(getNodeIndexShards())
nodeIndexShards := slices.Collect(maps.Values(getNodeIndexShards()))
transactionId := "xyz"

sendNodeIndexShardsEvent(reporter, &info, nodeIndexShards, transactionId)
Expand All @@ -91,7 +91,7 @@ func TestSendNodeIndexShardsEvent(t *testing.T) {

reporter := &mbtest.CapturingReporterV2{}
info := auto_ops_testing.CreateClusterInfo("8.15.3")
nodeIndexShards := maps.Values(getNodeIndexShards())
nodeIndexShards := slices.Collect(maps.Values(getNodeIndexShards()))
transactionId := "xyz"

sendNodeIndexShardsEvent(reporter, &info, nodeIndexShards, transactionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ package tasks_management
import (
"errors"
"fmt"
"maps"
"slices"

e "github.com/elastic/beats/v7/x-pack/metricbeat/module/autoops_es/events"

"golang.org/x/exp/maps"

s "github.com/elastic/beats/v7/libbeat/common/schema"
c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface"
"github.com/elastic/beats/v7/metricbeat/mb"
Expand Down Expand Up @@ -85,7 +85,7 @@ func eventsMapping(r mb.ReporterV2, info *utils.ClusterInfo, nodeTasks *GroupedT
// guarantee the parent node is in the list (it may not be)
nodeMap[node] = true

task["node"] = maps.Keys(nodeMap)
task["node"] = slices.Collect(maps.Keys(nodeMap))

// remove the children node
delete(task, "children")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

package tasks_management

import (
"golang.org/x/exp/maps"
)

// Extract the Node ID from each child task and add it to the map (treated as a Set).
func parseChildNodes(children []any, ok bool) map[string]bool {
nodeMap := map[string]bool{}
Expand Down Expand Up @@ -37,7 +33,7 @@ func parseChildNodes(children []any, ok bool) map[string]bool {
if ok && innerChildren != nil {
innerChildren, ok := innerChildren.([]any)

for _, node := range maps.Keys(parseChildNodes(innerChildren, ok)) {
for node := range parseChildNodes(innerChildren, ok) {
nodeMap[node] = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
package tasks_management

import (
"maps"
"slices"
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

// Expect an empty array to return an empty map.
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestParseChildNodesReturnsNodeIdsWithDuplicates(t *testing.T) {
},
}

require.ElementsMatch(t, []string{"node1", "node2"}, maps.Keys(parseChildNodes(children, true)))
require.ElementsMatch(t, []string{"node1", "node2"}, slices.Collect(maps.Keys(parseChildNodes(children, true))))
}

// Expect an empty array to returns node IDs without recursion.
Expand All @@ -56,7 +57,7 @@ func TestParseChildNodesReturnsFlatNodeIds(t *testing.T) {
},
}

require.ElementsMatch(t, []string{"node1", "node2"}, maps.Keys(parseChildNodes(children, true)))
require.ElementsMatch(t, []string{"node1", "node2"}, slices.Collect(maps.Keys(parseChildNodes(children, true))))
}

// Expect an empty array to returns node IDs with recursion.
Expand All @@ -83,7 +84,7 @@ func TestParseChildNodesReturnsRecursiveNodeIds(t *testing.T) {
},
}

require.ElementsMatch(t, []string{"node1", "node2", "node3", "node4", "node5"}, maps.Keys(parseChildNodes(children, true)))
require.ElementsMatch(t, []string{"node1", "node2", "node3", "node4", "node5"}, slices.Collect(maps.Keys(parseChildNodes(children, true))))
}

// Expect an empty array to returns node IDs with recursion and ignores unspecified node IDs.
Expand All @@ -110,5 +111,5 @@ func TestParseChildNodesIgnoresEmptyNodeId(t *testing.T) {
},
}

require.ElementsMatch(t, []string{"node1", "node2", "node3", "node4"}, maps.Keys(parseChildNodes(children, true)))
require.ElementsMatch(t, []string{"node1", "node2", "node3", "node4"}, slices.Collect(maps.Keys(parseChildNodes(children, true))))
}
12 changes: 6 additions & 6 deletions x-pack/metricbeat/module/autoops_es/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ package templates
import (
"errors"
"fmt"
"maps"
"os"
"slices"
"strings"
"time"

"github.com/elastic/beats/v7/x-pack/metricbeat/module/autoops_es/events"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/autoops_es/utils"

"golang.org/x/exp/maps"

"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/module/elasticsearch"
"github.com/elastic/elastic-agent-libs/mapstr"
Expand Down Expand Up @@ -110,9 +110,9 @@ func GetPartitionedTemplates[T any](templates []T, namer utils.Supplier[*T, stri

nameLimit := utils.GetIntEnvParam(TEMPLATE_BATCH_SIZE_NAME, 1500)

return maps.Values(utils.PartitionByMaxValue(nameLimit, templateNames, func(template string) int {
return slices.Collect(maps.Values(utils.PartitionByMaxValue(nameLimit, templateNames, func(template string) int {
return len(template)
}))
})))
}

func GetPartitionedTemplatesWithErrors[T any](templates []T, namer utils.Supplier[*T, string], filter utils.CheckedPredicate[*T]) ([][]string, []error) {
Expand All @@ -128,9 +128,9 @@ func GetPartitionedTemplatesWithErrors[T any](templates []T, namer utils.Supplie

nameLimit := utils.GetIntEnvParam(TEMPLATE_BATCH_SIZE_NAME, 1500)

return maps.Values(utils.PartitionByMaxValue(nameLimit, templateNames, func(template string) int {
return slices.Collect(maps.Values(utils.PartitionByMaxValue(nameLimit, templateNames, func(template string) int {
return len(template)
})), predicateErrors
}))), predicateErrors
}

// Loop across the `partitionedTemplates` and request them in associated batches, then extract and report them as events sharing a Transaction ID.
Expand Down
9 changes: 4 additions & 5 deletions x-pack/metricbeat/module/autoops_es/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
package utils

import (
"maps"
"net/url"
"sort"
"slices"
"strings"

"golang.org/x/exp/maps"
)

type Supplier[T any, R any] func(T) R
Expand Down Expand Up @@ -98,8 +97,8 @@ func PartitionByMaxValue[T any](limit int, items []T, valueExtractor func(T) int
itemKey := valueExtractor(item)
sortedValues[itemKey] = append(sortedValues[itemKey], item)
}
allKeys := maps.Keys(sortedValues)
sort.Ints(allKeys)
allKeys := slices.Collect(maps.Keys(sortedValues))
slices.Sort(allKeys)
var sortedItems = make(map[int][]T)
currentCapacity := 0
cursor := 0
Expand Down
Loading