Skip to content

Commit

Permalink
refactor: replace golang.org/x/exp with stdlib
Browse files Browse the repository at this point in the history
These experimental packages are now available in the Go standard
library since Go 1.21 and Go 1.23:

	1. golang.org/x/exp/slices -> slices [1]
	2. golang.org/x/exp/maps -> maps [2]

[1]: https://go.dev/doc/go1.21#slices
[2]: https://go.dev/doc/go1.21#maps
[3]: https://go.dev/doc/go1.23#iterators

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Jan 24, 2025
1 parent 55464a0 commit 254685c
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 31 deletions.
3 changes: 1 addition & 2 deletions depinject/check_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package depinject
import (
"fmt"
"reflect"
"slices"
"strings"
"unicode"

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

// isExportedType checks if the type is exported and not in an internal
Expand Down
3 changes: 1 addition & 2 deletions depinject/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module cosmossdk.io/depinject

go 1.22
go 1.23

require (
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/gogoproto v1.7.0
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
google.golang.org/grpc v1.69.4
google.golang.org/protobuf v1.36.3
gotest.tools/v3 v3.5.1
Expand Down
2 changes: 0 additions & 2 deletions depinject/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
Expand Down
14 changes: 6 additions & 8 deletions depinject/internal/util/util.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package util

import (
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"cmp"
"maps"
"slices"
)

// IterateMapOrdered iterates over the map with keys sorted in ascending order
// calling forEach for each key-value pair as long as forEach does not return an error.
func IterateMapOrdered[K constraints.Ordered, V any](m map[K]V, forEach func(k K, v V) error) error {
func IterateMapOrdered[K cmp.Ordered, V any](m map[K]V, forEach func(k K, v V) error) error {
keys := OrderedMapKeys(m)
for _, k := range keys {
if err := forEach(k, m[k]); err != nil {
Expand All @@ -19,8 +19,6 @@ func IterateMapOrdered[K constraints.Ordered, V any](m map[K]V, forEach func(k K
}

// OrderedMapKeys returns the map keys in ascending order.
func OrderedMapKeys[K constraints.Ordered, V any](m map[K]V) []K {
keys := maps.Keys(m)
slices.Sort(keys)
return keys
func OrderedMapKeys[K cmp.Ordered, V any](m map[K]V) []K {
return slices.Sorted(maps.Keys(m))
}
3 changes: 1 addition & 2 deletions depinject/provider_desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package depinject
import (
"fmt"
"reflect"
"slices"
"strings"
"unicode"

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

// providerDescriptor defines a special provider type that is defined by
Expand Down
1 change: 0 additions & 1 deletion math/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22
require (
github.com/cockroachdb/apd/v3 v3.2.1
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0
sigs.k8s.io/yaml v1.4.0
)

Expand Down
2 changes: 0 additions & 2 deletions math/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 h1:985EYyeCOxTpcgOTJpflJUwOeEz0CQOdPt73OzpE9F8=
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
Expand Down
6 changes: 3 additions & 3 deletions math/max_min.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package math

import "golang.org/x/exp/constraints"
import "cmp"

func Max[T constraints.Ordered](a, b T, rest ...T) T {
func Max[T cmp.Ordered](a, b T, rest ...T) T {
max := a
if b > a {
max = b
Expand All @@ -15,7 +15,7 @@ func Max[T constraints.Ordered](a, b T, rest ...T) T {
return max
}

func Min[T constraints.Ordered](a, b T, rest ...T) T {
func Min[T cmp.Ordered](a, b T, rest ...T) T {
min := a
if b < a {
min = b
Expand Down
2 changes: 1 addition & 1 deletion orm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/regen-network/gocuke v1.1.1
github.com/stretchr/testify v1.10.0
go.uber.org/mock v0.5.0
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67
google.golang.org/grpc v1.69.4
google.golang.org/protobuf v1.36.3
gotest.tools/v3 v3.5.1
Expand Down Expand Up @@ -65,6 +64,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tidwall/btree v1.7.0 // indirect
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
7 changes: 3 additions & 4 deletions orm/internal/codegen/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package codegen
import (
"bytes"
"fmt"
"maps"
"os"
"slices"

"github.com/iancoleman/strcase"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -57,8 +57,7 @@ func (g queryProtoGen) gen() error {
outBuf.F("package %s;", g.Desc.Package())
outBuf.F("")

imports := maps.Keys(g.imports)
slices.Sort(imports)
imports := slices.Sorted(maps.Keys(g.imports))
for _, i := range imports {
outBuf.F(`import "%s";`, i)
}
Expand Down
9 changes: 5 additions & 4 deletions orm/model/ormdb/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package ormdb
import (
"context"
"fmt"
"maps"
"slices"
"sort"

"golang.org/x/exp/maps"
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/core/appmodule"
Expand All @@ -22,7 +23,7 @@ func (m appModuleGenesisWrapper) IsOnePerModuleType() {}
func (m appModuleGenesisWrapper) IsAppModule() {}

func (m appModuleGenesisWrapper) DefaultGenesis(target appmodule.GenesisTarget) error {
tableNames := maps.Keys(m.tablesByName)
tableNames := slices.Collect(maps.Keys(m.tablesByName))
sort.Slice(tableNames, func(i, j int) bool {
ti, tj := tableNames[i], tableNames[j]
return ti.Name() < tj.Name()
Expand Down Expand Up @@ -50,7 +51,7 @@ func (m appModuleGenesisWrapper) DefaultGenesis(target appmodule.GenesisTarget)

func (m appModuleGenesisWrapper) ValidateGenesis(source appmodule.GenesisSource) error {
errMap := map[protoreflect.FullName]error{}
names := maps.Keys(m.tablesByName)
names := slices.Collect(maps.Keys(m.tablesByName))
sort.Slice(names, func(i, j int) bool {
ti, tj := names[i], names[j]
return ti.Name() < tj.Name()
Expand Down Expand Up @@ -124,7 +125,7 @@ func (m appModuleGenesisWrapper) InitGenesis(ctx context.Context, source appmodu

func (m appModuleGenesisWrapper) ExportGenesis(ctx context.Context, sink appmodule.GenesisTarget) error {
// Ensure that we export the tables in a deterministic order.
tableNames := maps.Keys(m.tablesByName)
tableNames := slices.Collect(maps.Keys(m.tablesByName))
sort.Slice(tableNames, func(i, j int) bool {
ti, tj := tableNames[i], tableNames[j]
return ti.Name() < tj.Name()
Expand Down

0 comments on commit 254685c

Please sign in to comment.