2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-02-15 05:20:41 +00:00
|
|
|
|
|
|
|
// Package views provides read-only accessors for commonly used
|
|
|
|
// value types.
|
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
2022-02-22 17:52:49 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-08-17 05:09:53 +00:00
|
|
|
"maps"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
2023-08-17 05:09:53 +00:00
|
|
|
"slices"
|
2022-02-22 17:52:49 +00:00
|
|
|
|
2022-02-15 05:20:41 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
|
|
|
)
|
|
|
|
|
2023-06-29 15:06:57 +00:00
|
|
|
func unmarshalSliceFromJSON[T any](b []byte, x *[]T) error {
|
2022-05-01 23:15:20 +00:00
|
|
|
if *x != nil {
|
|
|
|
return errors.New("already initialized")
|
|
|
|
}
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(b, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StructView represents the corresponding StructView of a Viewable. The concrete types are
|
|
|
|
// typically generated by tailscale.com/cmd/viewer.
|
|
|
|
type StructView[T any] interface {
|
|
|
|
// Valid reports whether the underlying Viewable is nil.
|
|
|
|
Valid() bool
|
|
|
|
// AsStruct returns a deep-copy of the underlying value.
|
|
|
|
// It returns nil, if Valid() is false.
|
|
|
|
AsStruct() T
|
|
|
|
}
|
|
|
|
|
|
|
|
// ViewCloner is any type that has had View and Clone funcs generated using
|
|
|
|
// tailscale.com/cmd/viewer.
|
|
|
|
type ViewCloner[T any, V StructView[T]] interface {
|
|
|
|
// View returns a read-only view of Viewable.
|
|
|
|
// If Viewable is nil, View().Valid() reports false.
|
|
|
|
View() V
|
|
|
|
// Clone returns a deep-clone of Viewable.
|
|
|
|
// It returns nil, when Viewable is nil.
|
|
|
|
Clone() T
|
|
|
|
}
|
|
|
|
|
|
|
|
// SliceOfViews returns a ViewSlice for x.
|
|
|
|
func SliceOfViews[T ViewCloner[T, V], V StructView[T]](x []T) SliceView[T, V] {
|
|
|
|
return SliceView[T, V]{x}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SliceView is a read-only wrapper around a struct which should only be exposed
|
|
|
|
// as a View.
|
|
|
|
type SliceView[T ViewCloner[T, V], V StructView[T]] struct {
|
2022-05-09 16:30:39 +00:00
|
|
|
// ж is the underlying mutable value, named with a hard-to-type
|
|
|
|
// character that looks pointy like a pointer.
|
2022-05-01 23:15:20 +00:00
|
|
|
// It is named distinctively to make you think of how dangerous it is to escape
|
|
|
|
// to callers. You must not let callers be able to mutate it.
|
|
|
|
ж []T
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (v SliceView[T, V]) MarshalJSON() ([]byte, error) { return json.Marshal(v.ж) }
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2023-06-29 15:06:57 +00:00
|
|
|
func (v *SliceView[T, V]) UnmarshalJSON(b []byte) error { return unmarshalSliceFromJSON(b, &v.ж) }
|
2022-05-01 23:15:20 +00:00
|
|
|
|
|
|
|
// IsNil reports whether the underlying slice is nil.
|
|
|
|
func (v SliceView[T, V]) IsNil() bool { return v.ж == nil }
|
|
|
|
|
|
|
|
// Len returns the length of the slice.
|
|
|
|
func (v SliceView[T, V]) Len() int { return len(v.ж) }
|
|
|
|
|
2023-08-18 15:03:32 +00:00
|
|
|
// LenIter returns a slice the same length as the v.Len().
|
|
|
|
// The caller can then range over it to get the valid indexes.
|
|
|
|
// It does not allocate.
|
|
|
|
func (v SliceView[T, V]) LenIter() []struct{} { return make([]struct{}, len(v.ж)) }
|
|
|
|
|
2022-05-01 23:15:20 +00:00
|
|
|
// At returns a View of the element at index `i` of the slice.
|
|
|
|
func (v SliceView[T, V]) At(i int) V { return v.ж[i].View() }
|
|
|
|
|
2023-06-21 03:12:25 +00:00
|
|
|
// SliceFrom returns v[i:].
|
|
|
|
func (v SliceView[T, V]) SliceFrom(i int) SliceView[T, V] { return SliceView[T, V]{v.ж[i:]} }
|
|
|
|
|
|
|
|
// SliceTo returns v[:i]
|
|
|
|
func (v SliceView[T, V]) SliceTo(i int) SliceView[T, V] { return SliceView[T, V]{v.ж[:i]} }
|
|
|
|
|
|
|
|
// Slice returns v[i:j]
|
|
|
|
func (v SliceView[T, V]) Slice(i, j int) SliceView[T, V] { return SliceView[T, V]{v.ж[i:j]} }
|
|
|
|
|
2022-05-01 23:15:20 +00:00
|
|
|
// AppendTo appends the underlying slice values to dst.
|
|
|
|
func (v SliceView[T, V]) AppendTo(dst []V) []V {
|
|
|
|
for _, x := range v.ж {
|
|
|
|
dst = append(dst, x.View())
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsSlice returns a copy of underlying slice.
|
|
|
|
func (v SliceView[T, V]) AsSlice() []V {
|
|
|
|
return v.AppendTo(nil)
|
|
|
|
}
|
|
|
|
|
2022-03-17 04:45:19 +00:00
|
|
|
// Slice is a read-only accessor for a slice.
|
|
|
|
type Slice[T any] struct {
|
2022-05-09 16:30:39 +00:00
|
|
|
// ж is the underlying mutable value, named with a hard-to-type
|
|
|
|
// character that looks pointy like a pointer.
|
2022-02-15 05:20:41 +00:00
|
|
|
// It is named distinctively to make you think of how dangerous it is to escape
|
|
|
|
// to callers. You must not let callers be able to mutate it.
|
2022-03-17 04:45:19 +00:00
|
|
|
ж []T
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 02:31:45 +00:00
|
|
|
// SliceOf returns a Slice for the provided slice for immutable values.
|
2022-05-10 18:19:51 +00:00
|
|
|
// It is the caller's responsibility to make sure V is immutable.
|
2022-05-10 02:31:45 +00:00
|
|
|
func SliceOf[T any](x []T) Slice[T] {
|
|
|
|
return Slice[T]{x}
|
|
|
|
}
|
2022-02-15 05:20:41 +00:00
|
|
|
|
2022-02-22 17:52:49 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v Slice[T]) MarshalJSON() ([]byte, error) {
|
2022-02-22 17:52:49 +00:00
|
|
|
return json.Marshal(v.ж)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v *Slice[T]) UnmarshalJSON(b []byte) error {
|
2023-06-29 15:06:57 +00:00
|
|
|
return unmarshalSliceFromJSON(b, &v.ж)
|
2022-02-22 17:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil reports whether the underlying slice is nil.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v Slice[T]) IsNil() bool { return v.ж == nil }
|
2022-02-22 17:52:49 +00:00
|
|
|
|
2022-02-15 05:20:41 +00:00
|
|
|
// Len returns the length of the slice.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v Slice[T]) Len() int { return len(v.ж) }
|
2022-02-15 05:20:41 +00:00
|
|
|
|
2023-08-18 15:03:32 +00:00
|
|
|
// LenIter returns a slice the same length as the v.Len().
|
|
|
|
// The caller can then range over it to get the valid indexes.
|
|
|
|
// It does not allocate.
|
|
|
|
func (v Slice[T]) LenIter() []struct{} { return make([]struct{}, len(v.ж)) }
|
|
|
|
|
2022-03-17 04:45:19 +00:00
|
|
|
// At returns the element at index `i` of the slice.
|
|
|
|
func (v Slice[T]) At(i int) T { return v.ж[i] }
|
2022-02-15 05:20:41 +00:00
|
|
|
|
2023-06-21 03:12:25 +00:00
|
|
|
// SliceFrom returns v[i:].
|
|
|
|
func (v Slice[T]) SliceFrom(i int) Slice[T] { return Slice[T]{v.ж[i:]} }
|
|
|
|
|
|
|
|
// SliceTo returns v[:i]
|
|
|
|
func (v Slice[T]) SliceTo(i int) Slice[T] { return Slice[T]{v.ж[:i]} }
|
|
|
|
|
|
|
|
// Slice returns v[i:j]
|
|
|
|
func (v Slice[T]) Slice(i, j int) Slice[T] { return Slice[T]{v.ж[i:j]} }
|
|
|
|
|
2022-02-15 05:20:41 +00:00
|
|
|
// AppendTo appends the underlying slice values to dst.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v Slice[T]) AppendTo(dst []T) []T {
|
2022-02-15 05:20:41 +00:00
|
|
|
return append(dst, v.ж...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsSlice returns a copy of underlying slice.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v Slice[T]) AsSlice() []T {
|
2022-02-15 05:20:41 +00:00
|
|
|
return v.AppendTo(v.ж[:0:0])
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:06:01 +00:00
|
|
|
// IndexFunc returns the first index of an element in v satisfying f(e),
|
|
|
|
// or -1 if none do.
|
|
|
|
//
|
|
|
|
// As it runs in O(n) time, use with care.
|
|
|
|
func (v Slice[T]) IndexFunc(f func(T) bool) int {
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if f(v.At(i)) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainsFunc reports whether any element in v satisfies f(e).
|
|
|
|
//
|
|
|
|
// As it runs in O(n) time, use with care.
|
|
|
|
func (v Slice[T]) ContainsFunc(f func(T) bool) bool {
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if f(v.At(i)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SliceContains reports whether v contains element e.
|
|
|
|
//
|
|
|
|
// As it runs in O(n) time, use with care.
|
|
|
|
func SliceContains[T comparable](v Slice[T], e T) bool {
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if v.At(i) == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-11 22:05:38 +00:00
|
|
|
// SliceEqualAnyOrder reports whether a and b contain the same elements, regardless of order.
|
|
|
|
// The underlying slices for a and b can be nil.
|
|
|
|
func SliceEqualAnyOrder[T comparable](a, b Slice[T]) bool {
|
|
|
|
if a.Len() != b.Len() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var diffStart int // beginning index where a and b differ
|
|
|
|
for n := a.Len(); diffStart < n; diffStart++ {
|
|
|
|
if a.At(diffStart) != b.At(diffStart) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if diffStart == a.Len() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// count the occurrences of remaining values and compare
|
|
|
|
valueCount := make(map[T]int)
|
|
|
|
for i, n := diffStart, a.Len(); i < n; i++ {
|
|
|
|
valueCount[a.At(i)]++
|
|
|
|
valueCount[b.At(i)]--
|
|
|
|
}
|
|
|
|
for _, count := range valueCount {
|
|
|
|
if count != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
// IPPrefixSlice is a read-only accessor for a slice of netip.Prefix.
|
2022-02-15 05:20:41 +00:00
|
|
|
type IPPrefixSlice struct {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
ж Slice[netip.Prefix]
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IPPrefixSliceOf returns a IPPrefixSlice for the provided slice.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func IPPrefixSliceOf(x []netip.Prefix) IPPrefixSlice { return IPPrefixSlice{SliceOf(x)} }
|
2022-02-15 05:20:41 +00:00
|
|
|
|
2022-02-22 17:52:49 +00:00
|
|
|
// IsNil reports whether the underlying slice is nil.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v IPPrefixSlice) IsNil() bool { return v.ж.IsNil() }
|
2022-02-22 17:52:49 +00:00
|
|
|
|
2022-02-15 05:20:41 +00:00
|
|
|
// Len returns the length of the slice.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v IPPrefixSlice) Len() int { return v.ж.Len() }
|
2022-02-15 05:20:41 +00:00
|
|
|
|
2023-08-18 15:03:32 +00:00
|
|
|
// LenIter returns a slice the same length as the v.Len().
|
|
|
|
// The caller can then range over it to get the valid indexes.
|
|
|
|
// It does not allocate.
|
|
|
|
func (v IPPrefixSlice) LenIter() []struct{} { return make([]struct{}, v.ж.Len()) }
|
|
|
|
|
2022-02-15 05:20:41 +00:00
|
|
|
// At returns the IPPrefix at index `i` of the slice.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (v IPPrefixSlice) At(i int) netip.Prefix { return v.ж.At(i) }
|
2022-02-15 05:20:41 +00:00
|
|
|
|
2022-03-17 04:45:19 +00:00
|
|
|
// AppendTo appends the underlying slice values to dst.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (v IPPrefixSlice) AppendTo(dst []netip.Prefix) []netip.Prefix {
|
2022-03-17 04:45:19 +00:00
|
|
|
return v.ж.AppendTo(dst)
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
// Unwrap returns the underlying Slice[netip.Prefix].
|
|
|
|
func (v IPPrefixSlice) Unwrap() Slice[netip.Prefix] {
|
2022-03-17 04:45:19 +00:00
|
|
|
return v.ж
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AsSlice returns a copy of underlying slice.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (v IPPrefixSlice) AsSlice() []netip.Prefix {
|
2022-03-17 04:45:19 +00:00
|
|
|
return v.ж.AsSlice()
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 23:05:43 +00:00
|
|
|
// Filter returns a new slice, containing elements of v that match f.
|
|
|
|
func (v IPPrefixSlice) Filter(f func(netip.Prefix) bool) []netip.Prefix {
|
|
|
|
return tsaddr.FilterPrefixesCopy(v.ж.ж, f)
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:20:41 +00:00
|
|
|
// PrefixesContainsIP reports whether any IPPrefix contains IP.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (v IPPrefixSlice) ContainsIP(ip netip.Addr) bool {
|
2022-03-17 04:45:19 +00:00
|
|
|
return tsaddr.PrefixesContainsIP(v.ж.ж, ip)
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrefixesContainsFunc reports whether f is true for any IPPrefix in the slice.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (v IPPrefixSlice) ContainsFunc(f func(netip.Prefix) bool) bool {
|
2022-12-06 02:22:53 +00:00
|
|
|
return slices.ContainsFunc(v.ж.ж, f)
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
2022-02-22 17:52:49 +00:00
|
|
|
|
2022-03-04 17:05:25 +00:00
|
|
|
// ContainsExitRoutes reports whether v contains ExitNode Routes.
|
|
|
|
func (v IPPrefixSlice) ContainsExitRoutes() bool {
|
2022-03-17 04:45:19 +00:00
|
|
|
return tsaddr.ContainsExitRoutes(v.ж.ж)
|
2022-03-04 17:05:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 17:36:08 +00:00
|
|
|
// ContainsNonExitSubnetRoutes reports whether v contains Subnet
|
|
|
|
// Routes other than ExitNode Routes.
|
|
|
|
func (v IPPrefixSlice) ContainsNonExitSubnetRoutes() bool {
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if v.At(i).Bits() != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-22 17:52:49 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (v IPPrefixSlice) MarshalJSON() ([]byte, error) {
|
2022-03-17 04:45:19 +00:00
|
|
|
return v.ж.MarshalJSON()
|
2022-02-22 17:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (v *IPPrefixSlice) UnmarshalJSON(b []byte) error {
|
2022-03-17 04:45:19 +00:00
|
|
|
return v.ж.UnmarshalJSON(b)
|
2022-02-22 17:52:49 +00:00
|
|
|
}
|
2022-05-09 16:30:39 +00:00
|
|
|
|
2022-05-10 18:19:51 +00:00
|
|
|
// MapOf returns a view over m. It is the caller's responsibility to make sure K
|
|
|
|
// and V is immutable, if this is being used to provide a read-only view over m.
|
2022-05-09 16:30:39 +00:00
|
|
|
func MapOf[K comparable, V comparable](m map[K]V) Map[K, V] {
|
|
|
|
return Map[K, V]{m}
|
|
|
|
}
|
|
|
|
|
2022-05-10 18:19:51 +00:00
|
|
|
// Map is a view over a map whose values are immutable.
|
2022-05-09 16:30:39 +00:00
|
|
|
type Map[K comparable, V any] struct {
|
|
|
|
// ж is the underlying mutable value, named with a hard-to-type
|
|
|
|
// character that looks pointy like a pointer.
|
|
|
|
// It is named distinctively to make you think of how dangerous it is to escape
|
|
|
|
// to callers. You must not let callers be able to mutate it.
|
|
|
|
ж map[K]V
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has reports whether k has an entry in the map.
|
|
|
|
func (m Map[K, V]) Has(k K) bool {
|
|
|
|
_, ok := m.ж[k]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil reports whether the underlying map is nil.
|
|
|
|
func (m Map[K, V]) IsNil() bool {
|
|
|
|
return m.ж == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the map.
|
|
|
|
func (m Map[K, V]) Len() int { return len(m.ж) }
|
|
|
|
|
|
|
|
// Get returns the element with key k.
|
|
|
|
func (m Map[K, V]) Get(k K) V {
|
|
|
|
return m.ж[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOk returns the element with key k and a bool representing whether the key
|
|
|
|
// is in map.
|
|
|
|
func (m Map[K, V]) GetOk(k K) (V, bool) {
|
|
|
|
v, ok := m.ж[k]
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2023-06-29 15:06:57 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (m Map[K, V]) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(m.ж)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
// It should only be called on an uninitialized Map.
|
|
|
|
func (m *Map[K, V]) UnmarshalJSON(b []byte) error {
|
|
|
|
if m.ж != nil {
|
|
|
|
return errors.New("already initialized")
|
|
|
|
}
|
|
|
|
return json.Unmarshal(b, &m.ж)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 values are immutable.
|
|
|
|
func (m *Map[K, V]) AsMap() map[K]V {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return maps.Clone(m.ж)
|
|
|
|
}
|
|
|
|
|
2022-05-10 02:31:45 +00:00
|
|
|
// MapRangeFn is the func called from a Map.Range call.
|
|
|
|
// Implementations should return false to stop range.
|
|
|
|
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]) {
|
2022-05-09 16:30:39 +00:00
|
|
|
for k, v := range m.ж {
|
2022-05-10 02:31:45 +00:00
|
|
|
if !f(k, v) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-09 16:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapFnOf returns a MapFn for m.
|
|
|
|
func MapFnOf[K comparable, T any, V any](m map[K]T, f func(T) V) MapFn[K, T, V] {
|
|
|
|
return MapFn[K, T, V]{
|
|
|
|
ж: m,
|
|
|
|
wrapv: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapFn is like Map but with a func to convert values from T to V.
|
|
|
|
// It is used to provide map of slices and views.
|
|
|
|
type MapFn[K comparable, T any, V any] struct {
|
|
|
|
// ж is the underlying mutable value, named with a hard-to-type
|
|
|
|
// character that looks pointy like a pointer.
|
|
|
|
// It is named distinctively to make you think of how dangerous it is to escape
|
|
|
|
// to callers. You must not let callers be able to mutate it.
|
|
|
|
ж map[K]T
|
|
|
|
wrapv func(T) V
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has reports whether k has an entry in the map.
|
|
|
|
func (m MapFn[K, T, V]) Has(k K) bool {
|
|
|
|
_, ok := m.ж[k]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the element with key k.
|
|
|
|
func (m MapFn[K, T, V]) Get(k K) V {
|
|
|
|
return m.wrapv(m.ж[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil reports whether the underlying map is nil.
|
|
|
|
func (m MapFn[K, T, V]) IsNil() bool {
|
|
|
|
return m.ж == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the map.
|
|
|
|
func (m MapFn[K, T, V]) Len() int { return len(m.ж) }
|
|
|
|
|
|
|
|
// GetOk returns the element with key k and a bool representing whether the key
|
|
|
|
// is in map.
|
|
|
|
func (m MapFn[K, T, V]) GetOk(k K) (V, bool) {
|
|
|
|
v, ok := m.ж[k]
|
|
|
|
return m.wrapv(v), ok
|
|
|
|
}
|
|
|
|
|
2022-05-10 02:31:45 +00:00
|
|
|
// 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]) {
|
2022-05-09 16:30:39 +00:00
|
|
|
for k, v := range m.ж {
|
2022-05-10 02:31:45 +00:00
|
|
|
if !f(k, m.wrapv(v)) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-09 16:30:39 +00:00
|
|
|
}
|
|
|
|
}
|