1
1
package datastore
2
2
3
3
import (
4
- "sort"
4
+ "encoding/json"
5
+ "maps"
6
+ "slices"
5
7
"strings"
6
8
)
7
9
8
10
// LabelSet represents a set of labels on an address book entry.
9
- type LabelSet map [string ]struct {}
11
+ type LabelSet struct {
12
+ elements map [string ]struct {}
13
+ }
10
14
11
15
// NewLabelSet initializes a new LabelSet with any number of labels.
12
16
func NewLabelSet (labels ... string ) LabelSet {
13
- set := make (LabelSet )
14
- for _ , lb := range labels {
15
- set [lb ] = struct {}{}
17
+ set := make (map [ string ] struct {}, len ( labels ) )
18
+ for _ , l := range labels {
19
+ set [l ] = struct {}{}
16
20
}
17
21
18
- return set
22
+ return LabelSet {
23
+ elements : set ,
24
+ }
19
25
}
20
26
21
27
// Add inserts a label into the set.
22
- func (ls LabelSet ) Add (label string ) {
23
- ls [label ] = struct {}{}
28
+ func (s LabelSet ) Add (label string ) {
29
+ s . elements [label ] = struct {}{}
24
30
}
25
31
26
32
// Remove deletes a label from the set, if it exists.
27
- func (ls LabelSet ) Remove (label string ) {
28
- delete (ls , label )
33
+ func (s LabelSet ) Remove (label string ) {
34
+ delete (s . elements , label )
29
35
}
30
36
31
37
// Contains checks if the set contains the given label.
32
- func (ls LabelSet ) Contains (label string ) bool {
33
- _ , ok := ls [label ]
38
+ func (s LabelSet ) Contains (label string ) bool {
39
+ _ , ok := s .elements [label ]
40
+
34
41
return ok
35
42
}
36
43
37
44
// String returns the labels as a sorted, space-separated string.
38
- // It implements the fmt.Stringer interface.
39
- func (ls LabelSet ) String () string {
40
- labels := ls .List ()
45
+ //
46
+ // Implements the fmt.Stringer interface.
47
+ func (s LabelSet ) String () string {
48
+ labels := s .List ()
41
49
if len (labels ) == 0 {
42
50
return ""
43
51
}
@@ -47,38 +55,60 @@ func (ls LabelSet) String() string {
47
55
}
48
56
49
57
// List returns the labels as a sorted slice of strings.
50
- func (ls LabelSet ) List () []string {
51
- if len (ls ) == 0 {
58
+ func (s LabelSet ) List () []string {
59
+ if len (s . elements ) == 0 {
52
60
return []string {}
53
61
}
54
62
55
63
// Collect labels into a slice
56
- labels := make ([]string , 0 , len (ls ))
57
- for label := range ls {
58
- labels = append (labels , label )
59
- }
64
+ labels := slices .Collect (maps .Keys (s .elements ))
60
65
61
66
// Sort the labels to ensure consistent ordering
62
- sort . Strings (labels )
67
+ slices . Sort (labels )
63
68
64
69
return labels
65
70
}
66
71
67
72
// Equal checks if two LabelSets are equal.
68
- func (ls LabelSet ) Equal (other LabelSet ) bool {
69
- if len (ls ) != len (other ) {
70
- return false
71
- }
72
- for label := range ls {
73
- if _ , ok := other [label ]; ! ok {
74
- return false
75
- }
76
- }
73
+ func (s LabelSet ) Equal (other LabelSet ) bool {
74
+ return maps .Equal (s .elements , other .elements )
75
+ }
77
76
78
- return true
77
+ // Len returns the number of labels in the set.
78
+ func (s LabelSet ) Length () int {
79
+ return len (s .elements )
79
80
}
80
81
81
82
// IsEmpty checks if the LabelSet is empty.
82
- func (ls LabelSet ) IsEmpty () bool {
83
- return len (ls ) == 0
83
+ func (s LabelSet ) IsEmpty () bool {
84
+ return s .Length () == 0
85
+ }
86
+
87
+ // Clone creates a copy of the LabelSet.
88
+ func (s LabelSet ) Clone () LabelSet {
89
+ return LabelSet {
90
+ elements : maps .Clone (s .elements ),
91
+ }
92
+ }
93
+
94
+ // MarshalJSON marshals the LabelSet as a JSON array of strings.
95
+ //
96
+ // Implements the json.Marshaler interface.
97
+ func (s LabelSet ) MarshalJSON () ([]byte , error ) {
98
+ return json .Marshal (s .List ())
99
+ }
100
+
101
+ // UnmarshalJSON unmarshals a JSON array of strings into the LabelSet.
102
+ //
103
+ // Implements the json.Unmarshaler interface.
104
+ func (s * LabelSet ) UnmarshalJSON (data []byte ) error {
105
+ var labels []string
106
+ if err := json .Unmarshal (data , & labels ); err != nil {
107
+ return err
108
+ }
109
+
110
+ // Initialize the LabelSet with the unmarshaled labels
111
+ * s = NewLabelSet (labels ... )
112
+
113
+ return nil
84
114
}
0 commit comments