Skip to content

Commit

Permalink
[CLEANUP] Replace experimental maps and slices with stdlib (#2993)
Browse files Browse the repository at this point in the history
The experimental functions are now available in the standard library
in Go 1.23 [1].

[1]: https://go.dev/doc/go1.23#new-unique-package

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Nov 6, 2024
1 parent b0b1a0f commit 7ac2990
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 53 deletions.
5 changes: 3 additions & 2 deletions internal/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package audit
import (
"context"
"fmt"
"maps"
"path"
"slices"
"sync"
"time"

Expand All @@ -23,7 +25,6 @@ import (
"github.com/gopasspw/gopass/pkg/termio"
"github.com/muesli/crunchy"
"github.com/nbutton23/zxcvbn-go"
"golang.org/x/exp/maps"
)

type secretGetter interface {
Expand Down Expand Up @@ -269,7 +270,7 @@ func (a *Auditor) checkHIBP(ctx context.Context) error {

// look up all known sha1sums. The LookupBatch method will sort the
// input so we don't need to.
matches := scanner.LookupBatch(ctx, maps.Keys(a.r.sha1sums))
matches := scanner.LookupBatch(ctx, slices.Collect(maps.Keys(a.r.sha1sums)))
for _, m := range matches {
// map any match back to the secret(s).
secs, found := a.r.sha1sums[m]
Expand Down
18 changes: 8 additions & 10 deletions internal/backend/registry.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package backend

import (
"cmp"
"context"
"fmt"
"sort"
"maps"
"slices"
"sync"

"golang.org/x/exp/maps"
"github.com/gopasspw/gopass/internal/set"
)

var (
Expand Down Expand Up @@ -73,10 +75,7 @@ func (r *Registry[K, V]) BackendNames() []string {
r.RLock()
defer r.RUnlock()

names := maps.Keys(r.nameToBackend)
sort.Strings(names)

return names
return set.SortedKeys(r.nameToBackend)
}

func (r *Registry[K, V]) Backends() []V {
Expand All @@ -96,11 +95,10 @@ func (r *Registry[K, V]) Prioritized() []V {
defer r.RUnlock()

bes := maps.Values(r.backends)
sort.Slice(bes, func(i, j int) bool {
return bes[i].Priority() < bes[j].Priority()
})

return bes
return slices.SortedFunc(bes, func(a, b V) int {
return cmp.Compare(a.Priority(), b.Priority())
})
}

func (r *Registry[K, V]) Get(key K) (V, error) {
Expand Down
5 changes: 2 additions & 3 deletions internal/config/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"github.com/gopasspw/gopass/internal/set"
"golang.org/x/exp/maps"
)

// ignoredEnvs is a list of environment variables that are used by gopass
Expand Down Expand Up @@ -186,12 +185,12 @@ func TestEnvVarsInDocs(t *testing.T) {
t.Logf("env options documented in doc: %+v", documented)
t.Logf("env options used in the code: %+v", used)

for _, k := range set.Sorted(maps.Keys(documented)) {
for _, k := range set.SortedKeys(documented) {
if !used[k] {
t.Errorf("Documented but not used: %s", k)
}
}
for _, k := range set.Sorted(maps.Keys(used)) {
for _, k := range set.SortedKeys(used) {
if !documented[k] {
t.Errorf("Used but not documented: %s", k)
}
Expand Down
6 changes: 2 additions & 4 deletions internal/config/legacy/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"os"
"path/filepath"
"sort"

"github.com/gopasspw/gopass/internal/set"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/fsutil"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -107,8 +106,7 @@ func checkOverflow(m map[string]any) error {
return nil
}

keys := maps.Keys(m)
sort.Strings(keys)
keys := set.SortedKeys(m)

return fmt.Errorf("unknown fields: %+v", keys)
}
Expand Down
7 changes: 1 addition & 6 deletions internal/recipients/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"sort"
"strings"

"github.com/gopasspw/gopass/internal/set"
"golang.org/x/exp/maps"
)

// Recipients is a list of Key IDs. It will try to retain the file as much as possible while manipulating the recipients.
Expand Down Expand Up @@ -37,10 +35,7 @@ func (r *Recipients) Len() int {

// IDs returns the key IDs.
func (r *Recipients) IDs() []string {
res := maps.Keys(r.r)
sort.Strings(res)

return res
return set.SortedKeys(r.r)
}

// Add adds a new recipients. It returns true if the recipient was added.
Expand Down
14 changes: 6 additions & 8 deletions internal/set/sorted.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package set

import (
"maps"
"slices"

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

// SortedKeys returns the sorted keys of the map.
func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K {
// sort
keys := maps.Keys(m)
slices.Sort(keys)

return keys
return slices.Sorted(keys)
}

// Sorted returns a sorted set of the input.
Expand All @@ -36,9 +36,7 @@ func SortedFiltered[K constraints.Ordered](l []K, want func(K) bool) []K {
}
m[k] = struct{}{}
}
// sort
keys := maps.Keys(m)
slices.Sort(keys)

return keys
// sort
return SortedKeys(m)
}
8 changes: 2 additions & 6 deletions internal/store/mockstore/inmem/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ package inmem
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"

"github.com/blang/semver/v4"
"github.com/gopasspw/gopass/internal/backend"
"golang.org/x/exp/maps"
"github.com/gopasspw/gopass/internal/set"
)

// InMem is a thread-safe in-memory store.
Expand Down Expand Up @@ -81,10 +80,7 @@ func (m *InMem) List(ctx context.Context, prefix string) ([]string, error) {
m.Lock()
defer m.Unlock()

keys := maps.Keys(m.data)
sort.Strings(keys)

return keys, nil
return set.SortedKeys(m.data), nil
}

// IsDir returns true if the entry is a directory.
Expand Down
5 changes: 2 additions & 3 deletions pkg/gitconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/gopasspw/gopass/internal/set"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestInsertOnce(t *testing.T) {
Expand Down Expand Up @@ -132,7 +131,7 @@ func TestInsertMultiple(t *testing.T) {
"core.noshow": "true",
}

for _, k := range set.Sorted(maps.Keys(updates)) {
for _, k := range set.SortedKeys(updates) {
v := updates[k]
require.NoError(t, c.insertValue(k, v))
}
Expand Down Expand Up @@ -163,7 +162,7 @@ func TestRewriteRaw(t *testing.T) {
"show.safecontent": "false",
"core.autoimport": "false",
}
for _, k := range set.Sorted(maps.Keys(updates)) {
for _, k := range set.SortedKeys(updates) {
v := updates[k]
require.NoError(t, c.Set(k, v))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gitconfig/gitconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

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

// https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html#EXAMPLES
Expand Down Expand Up @@ -343,7 +342,8 @@ func TestParseComplex(t *testing.T) {

c := ParseConfig(strings.NewReader(configSampleComplex))

assert.Contains(t, maps.Keys(c.vars), "core.sshCommand")
_, ok := c.vars["core.sshCommand"]
assert.True(t, ok)

v, ok := c.Get("core.sshCommand")
assert.True(t, ok)
Expand Down
5 changes: 2 additions & 3 deletions pkg/gopass/secrets/akv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/gopasspw/gopass/internal/set"
"github.com/gopasspw/gopass/pkg/debug"
"golang.org/x/exp/maps"
)

var kvSep = ": "
Expand Down Expand Up @@ -40,7 +39,7 @@ func NewAKVWithData(pw string, kvps map[string][]string, body string, converted
kv.SetPassword(pw)
kv.fromMime = converted

for _, k := range set.Sorted(maps.Keys(kvps)) {
for _, k := range set.SortedKeys(kvps) {
vs := kvps[k]
for _, v := range vs {
_ = kv.Add(k, v)
Expand All @@ -59,7 +58,7 @@ func (a *AKV) Bytes() []byte {

// Keys returns all the parsed keys.
func (a *AKV) Keys() []string {
return set.Sorted(maps.Keys(a.kvp))
return set.SortedKeys(a.kvp)
}

// Get returns the value of the requested key, if found.
Expand Down
8 changes: 2 additions & 6 deletions pkg/gopass/secrets/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"errors"
"fmt"
"io"
"sort"
"strings"

"github.com/caspr-io/yamlpath"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/set"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/gopass"
"golang.org/x/exp/maps"
yaml "gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -47,10 +46,7 @@ type YAML struct {

// Keys returns all keys.
func (y *YAML) Keys() []string {
keys := maps.Keys(y.data)
sort.Strings(keys)

return keys
return set.SortedKeys(y.data)
}

// Get returns the first value of a single key.
Expand Down

0 comments on commit 7ac2990

Please sign in to comment.