@@ -27,7 +27,7 @@ names+=('paths')
27
27
[[ " ${# types[@]} " = " ${# value_types[@]} " ]]
28
28
[[ " ${# types[@]} " = " ${# deref_vs[@]} " ]]
29
29
[[ " ${# types[@]} " = " ${# names[@]} " ]]
30
- [[ " ${# types[@]} " = " $( git grep -InF ' m map[string]*' -- openapi3/loader.go | wc -l) " ]]
30
+ [[ " ${# types[@]} " = " $( git grep -InF ' om map[string]*' -- openapi3/loader.go | wc -l) " ]] # FIXME: !map
31
31
32
32
33
33
maplike_header () {
@@ -36,10 +36,10 @@ package openapi3
36
36
37
37
import (
38
38
"encoding/json"
39
- "sort"
40
39
"strings"
41
40
42
41
"github.com/go-openapi/jsonpointer"
42
+ orderedmap "github.com/wk8/go-ordered-map/v2"
43
43
)
44
44
45
45
EOF
@@ -73,9 +73,9 @@ maplike_NewWithCapa() {
73
73
// New${type# ' *' } WithCapacity builds a ${name} object of the given capacity.
74
74
func New${type# ' *' } WithCapacity(cap int) ${type} {
75
75
if cap == 0 {
76
- return &${type# ' *' } {m: make(map [string] ${value_type} )}
76
+ return &${type# ' *' } {om: orderedmap.New [string, ${value_type} ]( )}
77
77
}
78
- return &${type# ' *' } {m: make(map [string] ${value_type} , cap)}
78
+ return &${type# ' *' } {om: orderedmap.New [string, ${value_type} ]( cap)}
79
79
}
80
80
81
81
EOF
@@ -89,35 +89,35 @@ func (${name} ${type}) Value(key string) ${value_type} {
89
89
if ${name} .Len() == 0 {
90
90
return nil
91
91
}
92
- return ${name} .m[ key]
92
+ return ${name} .om.Value( key)
93
93
}
94
94
95
95
// Set adds or replaces key 'key' of '${name} ' with 'value'.
96
96
// Note: '${name} ' MUST be non-nil
97
97
func (${name} ${type} ) Set(key string, value ${value_type} ) {
98
- if ${name} .m == nil {
99
- ${name} .m = make(map[string] ${value_type} )
98
+ if ${name} .om == nil {
99
+ ${name} .om = New ${type # ' * ' } WithCapacity(0).om
100
100
}
101
- ${name} .m[ key] = value
101
+ _, _ = ${name} .om.Set( key, value)
102
102
}
103
103
104
104
// Len returns the amount of keys in ${name} excluding ${name} .Extensions.
105
105
func (${name} ${type} ) Len() int {
106
- if ${name} == nil || ${name} .m == nil {
106
+ if ${name} == nil || ${name} .om == nil {
107
107
return 0
108
108
}
109
- return len( ${name} .m )
109
+ return ${name} .om.Len( )
110
110
}
111
111
112
112
// Map returns ${name} as a 'map'.
113
113
// Note: iteration on Go maps is not ordered.
114
114
func (${name} ${type} ) Map() (m map[string]${value_type} ) {
115
- if ${name} == nil || len( ${name} .m) == 0 {
115
+ if ${name} == nil || ${name} .om == nil {
116
116
return make(map[string]${value_type} )
117
117
}
118
- m = make(map[string]${value_type} , len( ${name} .m ))
119
- for k, v := range ${name} .m {
120
- m[k ] = v
118
+ m = make(map[string]${value_type} , ${name} .Len( ))
119
+ for pair := ${name} .Iter(); pair != nil; pair = pair.Next() {
120
+ m[pair.Key ] = pair.Value
121
121
}
122
122
return
123
123
}
126
126
}
127
127
128
128
129
+ maplike_IterNext () {
130
+ cat << EOF >>"$maplike "
131
+ type ${name} KV orderedmap.Pair[string, ${value_type} ] //FIXME: pub?
132
+ // Iter returns a pointer to the first pair, in insertion order.
133
+ func (${name} ${type} ) Iter() *${name} KV {
134
+ if ${name} .Len() == 0 {
135
+ return nil
136
+ }
137
+ return (*${name} KV)(${name} .om.Oldest())
138
+ }
139
+
140
+ // Next returns a pointer to the next pair, in insertion order.
141
+ func (pair *${name} KV) Next() *${name} KV {
142
+ ompair := (*orderedmap.Pair[string, ${value_type} ])(pair)
143
+ return (*${name} KV)(ompair.Next())
144
+ }
145
+
146
+ EOF
147
+ }
148
+
149
+
129
150
maplike_Pointable () {
130
151
cat << EOF >>"$maplike "
131
152
var _ jsonpointer.JSONPointable = (${type} )(nil)
@@ -151,36 +172,28 @@ maplike_UnMarsh() {
151
172
cat << EOF >>"$maplike "
152
173
// MarshalJSON returns the JSON encoding of ${type# ' *' } .
153
174
func (${name} ${type} ) MarshalJSON() ([]byte, error) {
154
- m := make(map [string] interface{}, ${name} .Len()+ len(${name} .Extensions))
155
- for k, v := range ${name} .Extensions {
156
- m[k] = v
175
+ om := orderedmap.New [string, interface{}]( ${name} .Len() + len(${name} .Extensions))
176
+ for pair := ${name} .Iter(); pair != nil; pair = pair.Next() {
177
+ om.Set(pair.Key, pair.Value)
157
178
}
158
- for k, v := range ${name} .Map() {
159
- m[k] = v
179
+ for k, v := range ${name} .Extensions {
180
+ om.Set(k, v)
160
181
}
161
- return json.Marshal(m )
182
+ return om.MarshalJSON( )
162
183
}
163
184
164
185
// UnmarshalJSON sets ${type# ' *' } to a copy of data.
165
186
func (${name} ${type} ) UnmarshalJSON(data []byte) (err error) {
166
- var m map [string] interface{}
167
- if err = json.Unmarshal(data, &m ); err != nil {
187
+ om := orderedmap.New [string, interface{}]()
188
+ if err = json.Unmarshal(data, &om ); err != nil {
168
189
return
169
190
}
170
191
171
- ks := make([]string, 0, len(m))
172
- for k := range m {
173
- ks = append(ks, k)
174
- }
175
- sort.Strings(ks)
176
-
177
- x := ${type# ' *' } {
178
- Extensions: make(map[string]interface{}),
179
- m: make(map[string]${value_type} , len(m)),
180
- }
192
+ x := New${type# ' *' } WithCapacity(om.Len())
193
+ x.Extensions = make(map[string]interface{})
181
194
182
- for _, k := range ks {
183
- v := m[k]
195
+ for pair := om.Oldest(); pair != nil; pair = pair.Next() {
196
+ k, v := pair.Key, pair.Value
184
197
if strings.HasPrefix(k, "x-") {
185
198
x.Extensions[k] = v
186
199
continue
@@ -194,9 +207,9 @@ func (${name} ${type}) UnmarshalJSON(data []byte) (err error) {
194
207
if err = vv.UnmarshalJSON(data); err != nil {
195
208
return
196
209
}
197
- x.m[k] = &vv
210
+ x.Set(k, &vv)
198
211
}
199
- *${name} = x
212
+ *${name} = * x
200
213
return
201
214
}
202
215
EOF
@@ -221,8 +234,10 @@ test_body() {
221
234
require.Equal(t, (${value_type} )(nil), x.Value("key"))
222
235
x.Set("key", &${value_type# ' *' } {})
223
236
require.Equal(t, 1, x.Len())
224
- require.Equal(t, map[string]${value_type} {"key": {}}, x.Map())
225
- require.Equal(t, &${value_type# ' *' } {}, x.Value("key"))
237
+ m := x.Map()
238
+ require.Equal(t, map[string]${value_type} {"key": {}}, m)
239
+ m["key"].Ref = "bla"
240
+ require.Equal(t, &${value_type# ' *' } {Ref: "bla"}, x.Value("key"))
226
241
})
227
242
})
228
243
@@ -242,6 +257,7 @@ for i in "${!types[@]}"; do
242
257
243
258
type=" $type " name=" $name " value_type=" $value_type " maplike_NewWithCapa
244
259
type=" $type " name=" $name " value_type=" $value_type " maplike_ValueSetLen
260
+ type=" $type " name=" $name " value_type=" $value_type " maplike_IterNext
245
261
type=" $type " name=" $name " deref_v=" $deref_v " maplike_Pointable
246
262
type=" $type " name=" $name " value_type=" $value_type " maplike_UnMarsh
247
263
[[ $(( i+ 1 )) != " ${# types[@]} " ]] && echo >> " $maplike "
0 commit comments