Skip to content

Commit 1f72d4a

Browse files
cmagliealessio-perugini
authored andcommitted
Made orderedmap more generic
1 parent ccc3184 commit 1f72d4a

File tree

4 files changed

+66
-66
lines changed

4 files changed

+66
-66
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ require (
1818
github.com/fatih/color v1.15.0
1919
github.com/go-git/go-git/v5 v5.4.2
2020
github.com/gofrs/uuid/v5 v5.0.0
21-
github.com/iancoleman/orderedmap v0.3.0
2221
github.com/leonelquinteros/gotext v1.4.0
2322
github.com/mailru/easyjson v0.7.7
2423
github.com/marcinbor85/gohex v0.0.0-20210308104911-55fb1c624d84

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
194194
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
195195
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
196196
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
197-
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
198-
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
199197
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
200198
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
201199
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=

internal/cli/feedback/result/rpc.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ import (
2323

2424
// NewPlatformResult creates a new result.Platform from rpc.PlatformSummary
2525
func NewPlatformResult(in *rpc.PlatformSummary) *Platform {
26-
releases := orderedmap.New[string, *PlatformRelease]()
26+
releases := orderedmap.NewWithConversionFunc[*semver.Version, *PlatformRelease, string]((*semver.Version).String)
2727
for k, v := range in.Releases {
28-
releases.Set(k, NewPlatformReleaseResult(v))
28+
releases.Set(semver.MustParse(k), NewPlatformReleaseResult(v))
2929
}
30-
releases.SortKeys(func(x, y string) int {
31-
return semver.ParseRelaxed(x).CompareTo(semver.ParseRelaxed(y))
32-
})
30+
releases.SortKeys((*semver.Version).CompareTo)
3331

3432
return &Platform{
3533
Id: in.Metadata.Id,
@@ -40,8 +38,8 @@ func NewPlatformResult(in *rpc.PlatformSummary) *Platform {
4038
Deprecated: in.Metadata.Deprecated,
4139
Indexed: in.Metadata.Indexed,
4240
Releases: releases,
43-
InstalledVersion: in.InstalledVersion,
44-
LatestVersion: in.LatestVersion,
41+
InstalledVersion: semver.MustParse(in.InstalledVersion),
42+
LatestVersion: semver.MustParse(in.LatestVersion),
4543
}
4644
}
4745

@@ -55,10 +53,10 @@ type Platform struct {
5553
Deprecated bool `json:"deprecated,omitempty"`
5654
Indexed bool `json:"indexed,omitempty"`
5755

58-
Releases *orderedmap.Map[string, *PlatformRelease] `json:"releases,omitempty"`
56+
Releases orderedmap.Map[*semver.Version, *PlatformRelease] `json:"releases,omitempty"`
5957

60-
InstalledVersion string `json:"installed_version,omitempty"`
61-
LatestVersion string `json:"latest_version,omitempty"`
58+
InstalledVersion *semver.Version `json:"installed_version,omitempty"`
59+
LatestVersion *semver.Version `json:"latest_version,omitempty"`
6260
}
6361

6462
// GetLatestRelease returns the latest relase of this platform or nil if none available.

internal/orderedmap/orderedmap.go

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,65 @@ package orderedmap
33
import (
44
"bytes"
55
"encoding/json"
6-
"reflect"
76
"slices"
87
)
98

10-
// Map is a container of properties
11-
type Map[K comparable, V any] struct {
12-
kv map[K]V
13-
o []K
9+
// Map is a map that keeps ordering insertion.
10+
type Map[K any, V any] interface {
11+
Get(K) V
12+
Set(K, V)
13+
Keys() []K
14+
Merge(...Map[K, V]) Map[K, V]
15+
SortKeys(f func(x, y K) int)
16+
SortStableKeys(f func(x, y K) int)
17+
}
18+
19+
// NewWithConversionFunc creates a map using the given conversion function
20+
// to convert non-comparable key type to comparable items.
21+
// The conversion function must be bijective.
22+
func NewWithConversionFunc[K any, V any, C comparable](conv func(K) C) Map[K, V] {
23+
return &mapImpl[K, V, C]{
24+
conv: conv,
25+
kv: map[C]V{},
26+
o: []K{},
27+
}
1428
}
1529

16-
// New returns a new Map
17-
func New[K comparable, V any]() *Map[K, V] {
18-
return &Map[K, V]{
19-
kv: map[K]V{},
20-
o: []K{},
30+
// New creates a map
31+
func New[K comparable, V any]() *mapImpl[K, V, K] {
32+
return &mapImpl[K, V, K]{
33+
conv: func(in K) K { return in }, // identity
34+
kv: map[K]V{},
35+
o: []K{},
2136
}
2237
}
2338

39+
type mapImpl[K any, V any, C comparable] struct {
40+
conv func(K) C
41+
kv map[C]V
42+
o []K
43+
}
44+
2445
// Get retrieves the value corresponding to key
25-
func (m *Map[K, V]) Get(key K) V {
26-
return m.kv[key]
46+
func (m *mapImpl[K, V, C]) Get(key K) V {
47+
return m.kv[m.conv(key)]
2748
}
2849

2950
// GetOk retrieves the value corresponding to key and returns a true/false indicator
3051
// to check if the key is present in the map (true if the key is present)
31-
func (m *Map[K, V]) GetOk(key K) (V, bool) {
32-
v, ok := m.kv[key]
52+
func (m *mapImpl[K, V, C]) GetOk(key K) (V, bool) {
53+
v, ok := m.kv[m.conv(key)]
3354
return v, ok
3455
}
3556

3657
// ContainsKey returns true if the map contains the specified key
37-
func (m *Map[K, V]) ContainsKey(key K) bool {
38-
_, has := m.kv[key]
58+
func (m *mapImpl[K, V, C]) ContainsKey(key K) bool {
59+
_, has := m.kv[m.conv(key)]
3960
return has
4061
}
4162

4263
// MarshalJSON marshal the map into json mantaining the order of the key
43-
func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
64+
func (m *mapImpl[K, V, C]) MarshalJSON() ([]byte, error) {
4465
if m.Size() == 0 {
4566
return []byte("{}"), nil
4667
}
@@ -52,7 +73,7 @@ func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
5273
return nil, err
5374
}
5475
buf.WriteByte(':')
55-
if err := encoder.Encode(m.kv[k]); err != nil {
76+
if err := encoder.Encode(m.kv[m.conv(k)]); err != nil {
5677
return nil, err
5778
}
5879
buf.WriteByte(',')
@@ -63,24 +84,26 @@ func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
6384
}
6485

6586
// Set inserts or replaces an existing key-value pair in the map
66-
func (m *Map[K, V]) Set(key K, value V) {
67-
if _, has := m.kv[key]; has {
87+
func (m *mapImpl[K, V, C]) Set(key K, value V) {
88+
compKey := m.conv(key)
89+
if _, has := m.kv[compKey]; has {
6890
m.Remove(key)
6991
}
70-
m.kv[key] = value
92+
m.kv[compKey] = value
7193
m.o = append(m.o, key)
7294
}
7395

7496
// Size returns the number of elements in the map
75-
func (m *Map[K, V]) Size() int {
97+
func (m *mapImpl[K, V, C]) Size() int {
7698
return len(m.kv)
7799
}
78100

79101
// Remove removes the key from the map
80-
func (m *Map[K, V]) Remove(key K) {
81-
delete(m.kv, key)
102+
func (m *mapImpl[K, V, C]) Remove(key K) {
103+
compKey := m.conv(key)
104+
delete(m.kv, compKey)
82105
for i, k := range m.o {
83-
if k == key {
106+
if m.conv(k) == compKey {
84107
m.o = append(m.o[:i], m.o[i+1:]...)
85108
return
86109
}
@@ -89,62 +112,44 @@ func (m *Map[K, V]) Remove(key K) {
89112

90113
// Merge merges other Maps into this one. Each key/value of the merged Maps replaces
91114
// the key/value present in the original Map.
92-
func (m *Map[K, V]) Merge(sources ...*Map[K, V]) *Map[K, V] {
115+
func (m *mapImpl[K, V, C]) Merge(sources ...Map[K, V]) Map[K, V] {
93116
for _, source := range sources {
94-
for _, key := range source.o {
95-
value := source.kv[key]
117+
for _, key := range source.Keys() {
118+
value := source.Get(key)
96119
m.Set(key, value)
97120
}
98121
}
99122
return m
100123
}
101124

102125
// Keys returns an array of the keys contained in the Map
103-
func (m *Map[K, V]) Keys() []K {
126+
func (m *mapImpl[K, V, C]) Keys() []K {
104127
keys := make([]K, len(m.o))
105128
copy(keys, m.o)
106129
return keys
107130
}
108131

109-
func (m *Map[K, V]) SortKeys(f func(x, y K) int) {
132+
func (m *mapImpl[K, V, C]) SortKeys(f func(x, y K) int) {
110133
slices.SortFunc(m.o, f)
111134
}
112135

113-
func (m *Map[K, V]) SortStableKeys(f func(x, y K) int) {
136+
func (m *mapImpl[K, V, C]) SortStableKeys(f func(x, y K) int) {
114137
slices.SortStableFunc(m.o, f)
115138
}
116139

117140
// Values returns an array of the values contained in the Map. Duplicated
118141
// values are repeated in the list accordingly.
119-
func (m *Map[K, V]) Values() []V {
142+
func (m *mapImpl[K, V, C]) Values() []V {
120143
values := make([]V, len(m.o))
121144
for i, key := range m.o {
122-
values[i] = m.kv[key]
145+
values[i] = m.kv[m.conv(key)]
123146
}
124147
return values
125148
}
126149

127-
// AsMap returns the underlying map[string]string. This is useful if you need to
128-
// for ... range but without the requirement of the ordered elements.
129-
func (m *Map[K, V]) AsMap() map[K]V {
130-
return m.kv
131-
}
132-
133150
// Clone makes a copy of the Map
134-
func (m *Map[K, V]) Clone() *Map[K, V] {
135-
clone := New[K, V]()
151+
func (m *mapImpl[K, V, C]) Clone() Map[K, V] {
152+
clone := NewWithConversionFunc[K, V, C](m.conv)
136153
clone.Merge(m)
137154
return clone
138155
}
139-
140-
// Equals returns true if the current Map contains the same key/value pairs of
141-
// the Map passed as argument, the order of insertion does not matter.
142-
func (m *Map[K, V]) Equals(other *Map[K, V]) bool {
143-
return reflect.DeepEqual(m.kv, other.kv)
144-
}
145-
146-
// EqualsWithOrder returns true if the current Map contains the same key/value pairs of
147-
// the Map passed as argument with the same order of insertion.
148-
func (m *Map[K, V]) EqualsWithOrder(other *Map[K, V]) bool {
149-
return reflect.DeepEqual(m.o, other.o) && reflect.DeepEqual(m.kv, other.kv)
150-
}

0 commit comments

Comments
 (0)