|
50 | 50 | // keyValueType is used in computeDistinctReflect. |
51 | 51 | keyValueType = reflect.TypeOf(KeyValue{}) |
52 | 52 |
|
53 | | - // emptySet is returned for empty attribute sets. |
54 | | - emptySet = &Set{ |
| 53 | + // userDefinedEmptySet is an empty set. It was mistakenly exposed to users |
| 54 | + // as something they can assign to, so it must remain addressable and |
| 55 | + // mutable. |
| 56 | + // |
| 57 | + // This is kept for backwards compatibility, but should not be used in new code. |
| 58 | + userDefinedEmptySet = &Set{ |
| 59 | + equivalent: Distinct{ |
| 60 | + iface: [0]KeyValue{}, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + emptySet = Set{ |
55 | 65 | equivalent: Distinct{ |
56 | 66 | iface: [0]KeyValue{}, |
57 | 67 | }, |
|
62 | 72 | // |
63 | 73 | // This is a convenience provided for optimized calling utility. |
64 | 74 | func EmptySet() *Set { |
65 | | - return emptySet |
| 75 | + // Continue to return the pointer to the user-defined empty set for |
| 76 | + // backwards-compatibility. |
| 77 | + // |
| 78 | + // New code should not use this, instead use emptySet. |
| 79 | + return userDefinedEmptySet |
66 | 80 | } |
67 | 81 |
|
68 | 82 | // reflectValue abbreviates reflect.ValueOf(d). |
@@ -169,12 +183,6 @@ func (l *Set) Encoded(encoder Encoder) string { |
169 | 183 | return encoder.Encode(l.Iter()) |
170 | 184 | } |
171 | 185 |
|
172 | | -func empty() Set { |
173 | | - return Set{ |
174 | | - equivalent: emptySet.equivalent, |
175 | | - } |
176 | | -} |
177 | | - |
178 | 186 | // NewSet returns a new Set. See the documentation for |
179 | 187 | // NewSetWithSortableFiltered for more details. |
180 | 188 | // |
@@ -204,7 +212,7 @@ func NewSetWithSortable(kvs []KeyValue, _ *Sortable) Set { |
204 | 212 | func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) { |
205 | 213 | // Check for empty set. |
206 | 214 | if len(kvs) == 0 { |
207 | | - return empty(), nil |
| 215 | + return emptySet, nil |
208 | 216 | } |
209 | 217 |
|
210 | 218 | // Stable sort so the following de-duplication can implement |
|
0 commit comments