types/views: remove various Map Range funcs; use iterators everywhere

The remaining range funcs in the tree are RangeOverTCPs and
RangeOverWebs in ServeConfig; those will be cleaned up separately.

Updates #12912

Change-Id: Ieeae4864ab088877263c36b805f77aa8e6be938d
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2025-01-04 11:50:48 -08:00 committed by Brad Fitzpatrick
parent 47bd0723a0
commit 4b56bf9039
2 changed files with 45 additions and 86 deletions

View File

@ -568,54 +568,46 @@ func ExpandProxyTargetValue(target string, supportedSchemes []string, defaultSch
// If the returned bool from the given f is false, then this function stops // If the returned bool from the given f is false, then this function stops
// iterating immediately and does not check other foreground configs. // iterating immediately and does not check other foreground configs.
func (v ServeConfigView) RangeOverTCPs(f func(port uint16, _ TCPPortHandlerView) bool) { func (v ServeConfigView) RangeOverTCPs(f func(port uint16, _ TCPPortHandlerView) bool) {
parentCont := true for k, v := range v.TCP().All() {
v.TCP().Range(func(k uint16, v TCPPortHandlerView) (cont bool) { if !f(k, v) {
parentCont = f(k, v) return
return parentCont
})
v.Foreground().Range(func(k string, v ServeConfigView) (cont bool) {
if !parentCont {
return false
} }
v.TCP().Range(func(k uint16, v TCPPortHandlerView) (cont bool) { }
parentCont = f(k, v) for _, conf := range v.Foreground().All() {
return parentCont for k, v := range conf.TCP().All() {
}) if !f(k, v) {
return parentCont return
}) }
}
}
} }
// RangeOverWebs ranges over both background and foreground Webs. // RangeOverWebs ranges over both background and foreground Webs.
// If the returned bool from the given f is false, then this function stops // If the returned bool from the given f is false, then this function stops
// iterating immediately and does not check other foreground configs. // iterating immediately and does not check other foreground configs.
func (v ServeConfigView) RangeOverWebs(f func(_ HostPort, conf WebServerConfigView) bool) { func (v ServeConfigView) RangeOverWebs(f func(HostPort, WebServerConfigView) bool) {
parentCont := true for k, v := range v.Web().All() {
v.Web().Range(func(k HostPort, v WebServerConfigView) (cont bool) { if !f(k, v) {
parentCont = f(k, v) return
return parentCont
})
v.Foreground().Range(func(k string, v ServeConfigView) (cont bool) {
if !parentCont {
return false
} }
v.Web().Range(func(k HostPort, v WebServerConfigView) (cont bool) { }
parentCont = f(k, v) for _, conf := range v.Foreground().All() {
return parentCont for k, v := range conf.Web().All() {
}) if !f(k, v) {
return parentCont return
}) }
}
}
} }
// FindTCP returns the first TCP that matches with the given port. It // FindTCP returns the first TCP that matches with the given port. It
// prefers a foreground match first followed by a background search if none // prefers a foreground match first followed by a background search if none
// existed. // existed.
func (v ServeConfigView) FindTCP(port uint16) (res TCPPortHandlerView, ok bool) { func (v ServeConfigView) FindTCP(port uint16) (res TCPPortHandlerView, ok bool) {
v.Foreground().Range(func(_ string, v ServeConfigView) (cont bool) { for _, conf := range v.Foreground().All() {
res, ok = v.TCP().GetOk(port) if res, ok := conf.TCP().GetOk(port); ok {
return !ok return res, ok
}) }
if ok {
return res, ok
} }
return v.TCP().GetOk(port) return v.TCP().GetOk(port)
} }
@ -624,12 +616,10 @@ func (v ServeConfigView) FindTCP(port uint16) (res TCPPortHandlerView, ok bool)
// prefers a foreground match first followed by a background search if none // prefers a foreground match first followed by a background search if none
// existed. // existed.
func (v ServeConfigView) FindWeb(hp HostPort) (res WebServerConfigView, ok bool) { func (v ServeConfigView) FindWeb(hp HostPort) (res WebServerConfigView, ok bool) {
v.Foreground().Range(func(_ string, v ServeConfigView) (cont bool) { for _, conf := range v.Foreground().All() {
res, ok = v.Web().GetOk(hp) if res, ok := conf.Web().GetOk(hp); ok {
return !ok return res, ok
}) }
if ok {
return res, ok
} }
return v.Web().GetOk(hp) return v.Web().GetOk(hp)
} }
@ -637,14 +627,15 @@ func (v ServeConfigView) FindWeb(hp HostPort) (res WebServerConfigView, ok bool)
// HasAllowFunnel returns whether this config has at least one AllowFunnel // HasAllowFunnel returns whether this config has at least one AllowFunnel
// set in the background or foreground configs. // set in the background or foreground configs.
func (v ServeConfigView) HasAllowFunnel() bool { func (v ServeConfigView) HasAllowFunnel() bool {
return v.AllowFunnel().Len() > 0 || func() bool { if v.AllowFunnel().Len() > 0 {
var exists bool return true
v.Foreground().Range(func(k string, v ServeConfigView) (cont bool) { }
exists = v.AllowFunnel().Len() > 0 for _, conf := range v.Foreground().All() {
return !exists if conf.AllowFunnel().Len() > 0 {
}) return true
return exists }
}() }
return false
} }
// FindFunnel reports whether target exists in either the background AllowFunnel // FindFunnel reports whether target exists in either the background AllowFunnel
@ -653,12 +644,10 @@ func (v ServeConfigView) HasFunnelForTarget(target HostPort) bool {
if v.AllowFunnel().Get(target) { if v.AllowFunnel().Get(target) {
return true return true
} }
var exists bool for _, conf := range v.Foreground().All() {
v.Foreground().Range(func(_ string, v ServeConfigView) (cont bool) { if conf.AllowFunnel().Get(target) {
if exists = v.AllowFunnel().Get(target); exists { return true
return false
} }
return true }
}) return false
return exists
} }

View File

@ -415,16 +415,6 @@ func (m *MapSlice[K, V]) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &m.ж) return json.Unmarshal(b, &m.ж)
} }
// Range calls f for every k,v pair in the underlying map.
// It stops iteration immediately if f returns false.
func (m MapSlice[K, V]) Range(f MapRangeFn[K, Slice[V]]) {
for k, v := range m.ж {
if !f(k, SliceOf(v)) {
return
}
}
}
// AsMap returns a shallow-clone of the underlying map. // AsMap returns a shallow-clone of the underlying map.
// //
// If V is a pointer type, it is the caller's responsibility to make sure the // If V is a pointer type, it is the caller's responsibility to make sure the
@ -527,16 +517,6 @@ func (m Map[K, V]) AsMap() map[K]V {
// Implementations should return false to stop range. // Implementations should return false to stop range.
type MapRangeFn[K comparable, V any] func(k K, v V) (cont bool) type MapRangeFn[K comparable, V any] func(k K, v V) (cont bool)
// Range calls f for every k,v pair in the underlying map.
// It stops iteration immediately if f returns false.
func (m Map[K, V]) Range(f MapRangeFn[K, V]) {
for k, v := range m.ж {
if !f(k, v) {
return
}
}
}
// All returns an iterator iterating over the keys // All returns an iterator iterating over the keys
// and values of m. // and values of m.
func (m Map[K, V]) All() iter.Seq2[K, V] { func (m Map[K, V]) All() iter.Seq2[K, V] {
@ -600,16 +580,6 @@ func (m MapFn[K, T, V]) GetOk(k K) (V, bool) {
return m.wrapv(v), ok return m.wrapv(v), ok
} }
// Range calls f for every k,v pair in the underlying map.
// It stops iteration immediately if f returns false.
func (m MapFn[K, T, V]) Range(f MapRangeFn[K, V]) {
for k, v := range m.ж {
if !f(k, m.wrapv(v)) {
return
}
}
}
// All returns an iterator iterating over the keys and value views of m. // All returns an iterator iterating over the keys and value views of m.
func (m MapFn[K, T, V]) All() iter.Seq2[K, V] { func (m MapFn[K, T, V]) All() iter.Seq2[K, V] {
return func(yield func(K, V) bool) { return func(yield func(K, V) bool) {