util/syspolicy/setting: update Snapshot to use Go 1.23 iterators

Updates #12912
Updates #12687

Signed-off-by: Nick Khyl <nickk@tailscale.com>
pull/13736/head
Nick Khyl 2 months ago committed by Nick Khyl
parent 07c157ee9f
commit 29cf59a9b4

@ -4,6 +4,8 @@
package setting package setting
import ( import (
"iter"
"maps"
"slices" "slices"
"strings" "strings"
@ -25,15 +27,13 @@ func NewSnapshot(items map[Key]RawItem, opts ...SummaryOption) *Snapshot {
return &Snapshot{m: xmaps.Clone(items), sig: deephash.Hash(&items), summary: SummaryWith(opts...)} return &Snapshot{m: xmaps.Clone(items), sig: deephash.Hash(&items), summary: SummaryWith(opts...)}
} }
// All returns a map of all policy settings in s. // All returns an iterator over policy settings in s. The iteration order is not
// The returned map must not be modified. // specified and is not guaranteed to be the same from one call to the next.
func (s *Snapshot) All() map[Key]RawItem { func (s *Snapshot) All() iter.Seq2[Key, RawItem] {
if s == nil { if s == nil {
return nil return func(yield func(Key, RawItem) bool) {}
} }
// TODO(nickkhyl): return iter.Seq2[[Key], [RawItem]] in Go 1.23, return maps.All(s.m)
// and remove [keyItemPair].
return s.m
} }
// Get returns the value of the policy setting with the specified key // Get returns the value of the policy setting with the specified key
@ -87,12 +87,11 @@ func (s *Snapshot) EqualItems(s2 *Snapshot) bool {
// Keys return an iterator over keys in s. The iteration order is not specified // Keys return an iterator over keys in s. The iteration order is not specified
// and is not guaranteed to be the same from one call to the next. // and is not guaranteed to be the same from one call to the next.
func (s *Snapshot) Keys() []Key { func (s *Snapshot) Keys() iter.Seq[Key] {
if s.m == nil { if s.m == nil {
return nil return func(yield func(Key) bool) {}
} }
// TODO(nickkhyl): return iter.Seq[Key] in Go 1.23. return maps.Keys(s.m)
return xmaps.Keys(s.m)
} }
// Len reports the number of [RawItem]s in s. // Len reports the number of [RawItem]s in s.
@ -116,8 +115,6 @@ func (s *Snapshot) String() string {
if s.Len() == 0 && s.Summary().IsEmpty() { if s.Len() == 0 && s.Summary().IsEmpty() {
return "{Empty}" return "{Empty}"
} }
keys := s.Keys()
slices.Sort(keys)
var sb strings.Builder var sb strings.Builder
if !s.summary.IsEmpty() { if !s.summary.IsEmpty() {
sb.WriteRune('{') sb.WriteRune('{')
@ -127,7 +124,7 @@ func (s *Snapshot) String() string {
sb.WriteString(s.summary.String()) sb.WriteString(s.summary.String())
sb.WriteRune('}') sb.WriteRune('}')
} }
for _, k := range keys { for _, k := range slices.Sorted(s.Keys()) {
if sb.Len() != 0 { if sb.Len() != 0 {
sb.WriteRune('\n') sb.WriteRune('\n')
} }

Loading…
Cancel
Save