2022-02-15 05:20:41 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
2022-02-15 05:20:41 +00:00
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/net/tsaddr"
|
|
|
|
)
|
|
|
|
|
2022-03-17 04:45:19 +00:00
|
|
|
// Slice is a read-only accessor for a slice.
|
|
|
|
type Slice[T any] struct {
|
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-03-17 04:45:19 +00:00
|
|
|
// SliceOf returns a Slice for the provided slice.
|
|
|
|
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 {
|
2022-02-22 17:52:49 +00:00
|
|
|
if v.ж != nil {
|
2022-03-17 04:45:19 +00:00
|
|
|
return errors.New("Slice is already initialized")
|
2022-02-22 17:52:49 +00:00
|
|
|
}
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &v.ж); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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
|
|
|
|
|
|
|
// 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])
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPPrefixSlice is a read-only accessor for a slice of netaddr.IPPrefix.
|
|
|
|
type IPPrefixSlice struct {
|
2022-03-17 04:45:19 +00:00
|
|
|
ж Slice[netaddr.IPPrefix]
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IPPrefixSliceOf returns a IPPrefixSlice for the provided slice.
|
2022-03-17 04:45:19 +00:00
|
|
|
func IPPrefixSliceOf(x []netaddr.IPPrefix) 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
|
|
|
|
|
|
|
// At returns the IPPrefix at index `i` of the slice.
|
2022-03-17 04:45:19 +00:00
|
|
|
func (v IPPrefixSlice) At(i int) netaddr.IPPrefix { 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.
|
2022-02-15 05:20:41 +00:00
|
|
|
func (v IPPrefixSlice) AppendTo(dst []netaddr.IPPrefix) []netaddr.IPPrefix {
|
2022-03-17 04:45:19 +00:00
|
|
|
return v.ж.AppendTo(dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic returns the underlying Slice[netaddr.IPPrefix].
|
|
|
|
func (v IPPrefixSlice) Generic() Slice[netaddr.IPPrefix] {
|
|
|
|
return v.ж
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AsSlice returns a copy of underlying slice.
|
|
|
|
func (v IPPrefixSlice) AsSlice() []netaddr.IPPrefix {
|
2022-03-17 04:45:19 +00:00
|
|
|
return v.ж.AsSlice()
|
2022-02-15 05:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrefixesContainsIP reports whether any IPPrefix contains IP.
|
|
|
|
func (v IPPrefixSlice) ContainsIP(ip netaddr.IP) 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.
|
|
|
|
func (v IPPrefixSlice) ContainsFunc(f func(netaddr.IPPrefix) bool) bool {
|
2022-03-17 04:45:19 +00:00
|
|
|
return tsaddr.PrefixesContainsFunc(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-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
|
|
|
}
|