mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-05 04:11:59 +00:00
util/set: implement json.Marshaler/Unmarshaler (#10308)
Marshal as a JSON list instead of a map. Because set elements are `comparable` and not `cmp.Ordered`, we cannot easily sort the items before marshaling. Updates #cleanup Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package set
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"maps"
|
||||
)
|
||||
|
||||
@@ -66,3 +67,16 @@ func (s Set[T]) Len() int { return len(s) }
|
||||
func (s Set[T]) Equal(other Set[T]) bool {
|
||||
return maps.Equal(s, other)
|
||||
}
|
||||
|
||||
func (s Set[T]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s.Slice())
|
||||
}
|
||||
|
||||
func (s *Set[T]) UnmarshalJSON(buf []byte) error {
|
||||
var ss []T
|
||||
if err := json.Unmarshal(buf, &ss); err != nil {
|
||||
return err
|
||||
}
|
||||
*s = SetOf(ss)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user