Skip to content

Commit 90f5ce1

Browse files
committed
cedar-go: reduce the usage of x/exp/maps in favor of the standard library
Signed-off-by: Patrick Jakubowski <patrick.jakubowski@strongdm.com>
1 parent 0a791dd commit 90f5ce1

File tree

6 files changed

+16
-20
lines changed

6 files changed

+16
-20
lines changed

internal/mapset/immutable_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestImmutableMapSet(t *testing.T) {
3939

4040
t.Run("slice", func(t *testing.T) {
4141
s := Immutable[int]()
42-
testutil.Equals(t, s.Slice(), []int{})
42+
testutil.Equals(t, s.Slice(), []int(nil))
4343

4444
inSlice := []int{1, 2, 3}
4545
s = Immutable[int](inSlice...)

internal/mapset/mapset.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55
"encoding/json"
66
"fmt"
77
"iter"
8+
"maps"
89
"slices"
9-
10-
"golang.org/x/exp/maps"
1110
)
1211

1312
// Similar to the concept of a [legal peppercorn](https://en.wikipedia.org/wiki/Peppercorn_(law)), this instance of
@@ -118,7 +117,7 @@ func (h MapSet[T]) Slice() []T {
118117
if h.m == nil {
119118
return nil
120119
}
121-
return maps.Keys(h.m)
120+
return slices.Collect(maps.Keys(h.m))
122121
}
123122

124123
// Len returns the size of the MapSet

internal/mapset/mapset_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ func TestMapSet(t *testing.T) {
6363

6464
t.Run("slice", func(t *testing.T) {
6565
s := Make[int]()
66-
testutil.Equals(t, s.Slice(), []int{})
66+
testutil.Equals(t, s.Slice(), []int(nil))
6767

6868
s = Make[int](10)
69-
testutil.Equals(t, s.Slice(), []int{})
69+
testutil.Equals(t, s.Slice(), []int(nil))
7070

7171
inSlice := []int{1, 2, 3}
7272
s = FromItems(inSlice...)

types/entity_map.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package types
22

33
import (
44
"encoding/json"
5+
"maps"
56
"slices"
67
"strings"
7-
8-
"golang.org/x/exp/maps"
98
)
109

1110
// An EntityGetter is an interface for retrieving an Entity by EntityUID.
@@ -21,7 +20,7 @@ var _ EntityGetter = EntityMap{}
2120
type EntityMap map[EntityUID]Entity
2221

2322
func (e EntityMap) MarshalJSON() ([]byte, error) {
24-
s := maps.Values(e)
23+
s := slices.Collect(maps.Values(e))
2524
slices.SortFunc(s, func(a, b Entity) int {
2625
return strings.Compare(a.UID.String(), b.UID.String())
2726
})

types/record.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import (
66
"encoding/json"
77
"hash/fnv"
88
"iter"
9+
"maps"
910
"slices"
1011
"strconv"
11-
12-
"golang.org/x/exp/maps"
1312
)
1413

1514
type RecordMap = map[String]Value
@@ -27,7 +26,7 @@ func NewRecord(m RecordMap) Record {
2726
// NewRecord(RecordMap{}) are the same
2827
var hashVal uint64
2928
if len(m) > 0 {
30-
orderedKeys := maps.Keys(m)
29+
orderedKeys := slices.Collect(maps.Keys(m))
3130
slices.Sort(orderedKeys)
3231

3332
h := fnv.New64()
@@ -146,7 +145,7 @@ func (r *Record) UnmarshalJSON(b []byte) error {
146145
func (r Record) MarshalJSON() ([]byte, error) {
147146
w := &bytes.Buffer{}
148147
w.WriteByte('{')
149-
keys := maps.Keys(r.m)
148+
keys := slices.Collect(maps.Keys(r.m))
150149
slices.Sort(keys)
151150
for i, kk := range keys {
152151
if i > 0 {
@@ -174,7 +173,7 @@ func (r Record) MarshalCedar() []byte {
174173
var sb bytes.Buffer
175174
sb.WriteRune('{')
176175
first := true
177-
keys := maps.Keys(r.m)
176+
keys := slices.Collect(maps.Keys(r.m))
178177
slices.Sort(keys)
179178
for _, k := range keys {
180179
v := r.m[k]

types/set.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"bytes"
55
"encoding/json"
66
"iter"
7+
"maps"
78
"slices"
8-
9-
"golang.org/x/exp/maps"
109
)
1110

1211
// A Set is an immutable collection of elements that can be of the same or different types.
@@ -42,7 +41,7 @@ func NewSet(v ...Value) Set {
4241
// Special case hashVal for empty set to 0 so that the return value of Value.hash() of Set{} and NewSet([]Value{})
4342
// are the same
4443
var hashVal uint64
45-
for _, v := range maps.Values(set) {
44+
for v := range maps.Values(set) {
4645
hashVal += v.hash()
4746
}
4847

@@ -100,7 +99,7 @@ func (s Set) Slice() []Value {
10099
if s.s == nil {
101100
return nil
102101
}
103-
return maps.Values(s.s)
102+
return slices.Collect(maps.Values(s.s))
104103
}
105104

106105
// Equal returns true if the sets are Equal.
@@ -148,7 +147,7 @@ func (s *Set) UnmarshalJSON(b []byte) error {
148147
func (s Set) MarshalJSON() ([]byte, error) {
149148
w := &bytes.Buffer{}
150149
w.WriteByte('[')
151-
orderedKeys := maps.Keys(s.s)
150+
orderedKeys := slices.Collect(maps.Keys(s.s))
152151
slices.Sort(orderedKeys)
153152
for i, k := range orderedKeys {
154153
if i != 0 {
@@ -172,7 +171,7 @@ func (s Set) String() string { return string(s.MarshalCedar()) }
172171
func (s Set) MarshalCedar() []byte {
173172
var sb bytes.Buffer
174173
sb.WriteRune('[')
175-
orderedKeys := maps.Keys(s.s)
174+
orderedKeys := slices.Collect(maps.Keys(s.s))
176175
slices.Sort(orderedKeys)
177176
for i, k := range orderedKeys {
178177
if i != 0 {

0 commit comments

Comments
 (0)