@@ -116,41 +116,37 @@ func (om *OrderedMap[K, V]) UnmarshalJSON(data []byte) error {
116
116
var key K
117
117
var value V
118
118
119
- if typedKeyPointer , ok := any (& key ).(encoding.TextUnmarshaler ); ok {
120
- // pointer receiver
121
- if err := typedKeyPointer .UnmarshalText (keyData ); err != nil {
119
+ switch tkp := any (& key ).(type ) {
120
+ case * string :
121
+ * tkp = string (keyData )
122
+ case encoding.TextUnmarshaler :
123
+ if err := tkp .UnmarshalText (keyData ); err != nil {
122
124
return err
123
125
}
124
- } else {
125
- keyAlreadyUnmarshalled := false
126
- switch typedKey := any (key ).(type ) {
127
- case string :
128
- keyData = quoteString (keyData )
129
- case encoding.TextUnmarshaler :
130
- // not a pointer receiver
131
- if err := typedKey .UnmarshalText (keyData ); err != nil {
132
- return err
133
- }
134
- keyAlreadyUnmarshalled = true
135
- case int , int8 , int16 , int32 , int64 , uint , uint8 , uint16 , uint32 , uint64 :
136
- default :
137
-
138
- // this switch takes care of wrapper types around primitive types, such as
139
- // type myType string
140
- switch reflect .TypeOf (key ).Kind () {
141
- case reflect .String :
142
- keyData = quoteString (keyData )
143
- case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 ,
144
- reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 :
145
- default :
146
- return fmt .Errorf ("unsupported key type: %T" , typedKey )
147
- }
126
+ case * encoding.TextUnmarshaler :
127
+ // This is to preserve compatibility with original implementation
128
+ // that handled none pointer receivers, but I (xiegeo) believes this is unused.
129
+ if err := (* tkp ).UnmarshalText (keyData ); err != nil {
130
+ return err
148
131
}
149
-
150
- if ! keyAlreadyUnmarshalled {
132
+ case * int , * int8 , * int16 , * int32 , * int64 , * uint , * uint8 , * uint16 , * uint32 , * uint64 :
133
+ if err := json .Unmarshal (keyData , tkp ); err != nil {
134
+ return err
135
+ }
136
+ default :
137
+ // this switch takes care of wrapper types around primitive types, such as
138
+ // type myType string
139
+ switch reflect .TypeOf (key ).Kind () {
140
+ case reflect .String :
141
+ convertedkeyData := reflect .ValueOf (keyData ).Convert (reflect .TypeOf (key ))
142
+ reflect .ValueOf (& key ).Elem ().Set (convertedkeyData )
143
+ case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 ,
144
+ reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 :
151
145
if err := json .Unmarshal (keyData , & key ); err != nil {
152
146
return err
153
147
}
148
+ default :
149
+ return fmt .Errorf ("unsupported key type: %T" , key )
154
150
}
155
151
}
156
152
@@ -162,11 +158,3 @@ func (om *OrderedMap[K, V]) UnmarshalJSON(data []byte) error {
162
158
return nil
163
159
})
164
160
}
165
-
166
- func quoteString (data []byte ) []byte {
167
- withQuotes := make ([]byte , len (data )+ 2 ) //nolint:gomnd
168
- copy (withQuotes [1 :], data )
169
- withQuotes [0 ] = '"'
170
- withQuotes [len (data )+ 1 ] = '"'
171
- return withQuotes
172
- }
0 commit comments