Documentation
¶
Overview ¶
Package views provides read-only accessors for commonly used value types.
Index ¶
- func AppendStrings[T fmt.Stringer](dst []string, v Slice[T]) []string
- func ContainsPointers[T any]() bool
- func MapViewsEqual[K, V comparable](a, b Map[K, V]) bool
- func MapViewsEqualFunc[K comparable, V1, V2 any](a Map[K, V1], b Map[K, V2], eq func(V1, V2) bool) bool
- func SliceContains[T comparable](v Slice[T], e T) bool
- func SliceEqual[T comparable](a, b Slice[T]) bool
- func SliceEqualAnyOrder[T comparable](a, b Slice[T]) bool
- func SliceEqualAnyOrderFunc[T any, V comparable](a, b Slice[T], cmp func(T) V) bool
- func SliceMax[T cmp.Ordered](v Slice[T]) T
- func SliceMin[T cmp.Ordered](v Slice[T]) T
- type ByteSlice
- func (v ByteSlice[T]) AppendTo(dst T) T
- func (v ByteSlice[T]) AsSlice() T
- func (v ByteSlice[T]) At(i int) byte
- func (v ByteSlice[T]) Equal(b T) bool
- func (v ByteSlice[T]) EqualView(b ByteSlice[T]) bool
- func (v ByteSlice[T]) IsNil() bool
- func (v ByteSlice[T]) Len() int
- func (v ByteSlice[T]) MapKey() SliceMapKey[byte]
- func (v ByteSlice[T]) MarshalJSON() ([]byte, error)
- func (v ByteSlice[T]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (v ByteSlice[T]) Mem() mem.RO
- func (v ByteSlice[T]) Slice(i, j int) ByteSlice[T]
- func (v ByteSlice[T]) SliceFrom(i int) ByteSlice[T]
- func (v ByteSlice[T]) SliceTo(i int) ByteSlice[T]
- func (v *ByteSlice[T]) UnmarshalJSON(b []byte) error
- func (v *ByteSlice[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- type Cloner
- type Map
- func (m Map[K, V]) All() iter.Seq2[K, V]
- func (m Map[K, V]) AsMap() map[K]V
- func (m Map[K, V]) Contains(k K) bool
- func (m Map[K, V]) Get(k K) V
- func (m Map[K, V]) GetOk(k K) (V, bool)
- func (m Map[K, V]) Has(k K) bool
- func (m Map[K, V]) IsNil() bool
- func (m Map[K, V]) Len() int
- func (m Map[K, V]) MarshalJSON() ([]byte, error)
- func (m Map[K, V]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (m *Map[K, V]) UnmarshalJSON(b []byte) error
- func (m *Map[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- type MapFn
- type MapRangeFn
- type MapSlice
- func (m MapSlice[K, V]) All() iter.Seq2[K, Slice[V]]
- func (m MapSlice[K, V]) AsMap() map[K][]V
- func (m MapSlice[K, V]) Contains(k K) bool
- func (m MapSlice[K, V]) Get(k K) Slice[V]
- func (m MapSlice[K, V]) GetOk(k K) (Slice[V], bool)
- func (m MapSlice[K, V]) IsNil() bool
- func (m MapSlice[K, V]) Len() int
- func (m MapSlice[K, V]) MarshalJSON() ([]byte, error)
- func (m MapSlice[K, V]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (m *MapSlice[K, V]) UnmarshalJSON(b []byte) error
- func (m *MapSlice[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- type Slice
- func (v Slice[T]) All() iter.Seq2[int, T]
- func (v Slice[T]) AppendTo(dst []T) []T
- func (v Slice[T]) AsSlice() []T
- func (v Slice[T]) At(i int) T
- func (v Slice[T]) ContainsFunc(f func(T) bool) bool
- func (v Slice[T]) IndexFunc(f func(T) bool) int
- func (v Slice[T]) IsNil() bool
- func (v Slice[T]) Len() int
- func (v Slice[T]) MapKey() SliceMapKey[T]
- func (v Slice[T]) MarshalJSON() ([]byte, error)
- func (v Slice[T]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (v Slice[T]) MaxFunc(cmp func(a, b T) int) T
- func (v Slice[T]) MinFunc(cmp func(a, b T) int) T
- func (v Slice[T]) Slice(i, j int) Slice[T]
- func (v Slice[T]) SliceFrom(i int) Slice[T]
- func (v Slice[T]) SliceTo(i int) Slice[T]
- func (v *Slice[T]) UnmarshalJSON(b []byte) error
- func (v *Slice[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- type SliceMapKey
- type SliceView
- func (v SliceView[T, V]) All() iter.Seq2[int, V]
- func (v SliceView[T, V]) AppendTo(dst []V) []V
- func (v SliceView[T, V]) AsSlice() []V
- func (v SliceView[T, V]) At(i int) V
- func (v SliceView[T, V]) IsNil() bool
- func (v SliceView[T, V]) Len() int
- func (v SliceView[T, V]) MapKey() SliceMapKey[T]
- func (v SliceView[T, V]) MarshalJSON() ([]byte, error)
- func (v SliceView[T, V]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (v SliceView[T, V]) Slice(i, j int) SliceView[T, V]
- func (v SliceView[T, V]) SliceFrom(i int) SliceView[T, V]
- func (v SliceView[T, V]) SliceTo(i int) SliceView[T, V]
- func (v *SliceView[T, V]) UnmarshalJSON(b []byte) error
- func (v *SliceView[T, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- type StructView
- type ValuePointer
- func (p ValuePointer[T]) Clone() *T
- func (p ValuePointer[T]) Get() T
- func (p ValuePointer[T]) GetOk() (value T, ok bool)
- func (p ValuePointer[T]) GetOr(def T) T
- func (p ValuePointer[T]) MarshalJSON() ([]byte, error)
- func (p ValuePointer[T]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (p ValuePointer[T]) String() string
- func (p *ValuePointer[T]) UnmarshalJSON(b []byte) error
- func (p *ValuePointer[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (p ValuePointer[T]) Valid() bool
- type ViewCloner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendStrings ¶
AppendStrings appends the string representation of each element in v to dst.
func ContainsPointers ¶
ContainsPointers reports whether T contains any pointers, either explicitly or implicitly. It has special handling for some types that contain pointers that we know are free from memory aliasing/mutation concerns.
func MapViewsEqual ¶
func MapViewsEqual[K, V comparable](a, b Map[K, V]) bool
MapViewsEqual returns whether the two given [Map]s are equal. Both K and V must be comparable; if V is non-comparable, use MapViewsEqualFunc instead.
func MapViewsEqualFunc ¶
func MapViewsEqualFunc[K comparable, V1, V2 any](a Map[K, V1], b Map[K, V2], eq func(V1, V2) bool) bool
MapViewsEqualFunc returns whether the two given [Map]s are equal, using the given function to compare two values.
func SliceContains ¶
func SliceContains[T comparable](v Slice[T], e T) bool
SliceContains reports whether v contains element e.
As it runs in O(n) time, use with care.
func SliceEqual ¶
func SliceEqual[T comparable](a, b Slice[T]) bool
SliceEqual is like the standard library's slices.Equal, but for two views.
func SliceEqualAnyOrder ¶
func SliceEqualAnyOrder[T comparable](a, b Slice[T]) bool
SliceEqualAnyOrder reports whether a and b contain the same elements, regardless of order. The underlying slices for a and b can be nil.
func SliceEqualAnyOrderFunc ¶
func SliceEqualAnyOrderFunc[T any, V comparable](a, b Slice[T], cmp func(T) V) bool
SliceEqualAnyOrderFunc reports whether a and b contain the same elements, regardless of order. The underlying slices for a and b can be nil.
The provided function should return a comparable value for each element.
func SliceMax ¶
SliceMax returns the maximal value in v. It panics if v is empty. For floating point T, SliceMax propagates NaNs (any NaN value in v forces the output to be NaN). See also slices.Max.
Types ¶
type ByteSlice ¶
type ByteSlice[T ~[]byte] struct { // contains filtered or unexported fields }
ByteSlice is a read-only accessor for types that are backed by a []byte.
func ByteSliceOf ¶
ByteSliceOf returns a ByteSlice for the provided slice.
func (ByteSlice[T]) AppendTo ¶
func (v ByteSlice[T]) AppendTo(dst T) T
AppendTo appends the underlying slice values to dst.
func (ByteSlice[T]) AsSlice ¶
func (v ByteSlice[T]) AsSlice() T
AsSlice returns a copy of the underlying slice.
func (ByteSlice[T]) MapKey ¶
func (v ByteSlice[T]) MapKey() SliceMapKey[byte]
MapKey returns a unique key for a slice, based on its address and length.
func (ByteSlice[T]) MarshalJSON ¶
MarshalJSON implements jsonv1.Marshaler.
func (ByteSlice[T]) MarshalJSONTo ¶
MarshalJSONTo implements jsonv2.MarshalerTo.
func (*ByteSlice[T]) UnmarshalJSON ¶
UnmarshalJSON implements jsonv1.Unmarshaler. It must only be called on an uninitialized ByteSlice.
func (*ByteSlice[T]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements jsonv2.UnmarshalerFrom. It must only be called on an uninitialized ByteSlice.
type Cloner ¶
type Cloner[T any] interface { // Clone returns a deep-clone of the receiver. // It returns nil, when the receiver is nil. Clone() T }
Cloner is any type that has a Clone function returning a deep-clone of the receiver.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map provides a read-only view of a map. It is the caller's responsibility to make sure V is immutable.
func MapOf ¶
func MapOf[K comparable, V any](m map[K]V) Map[K, V]
MapOf returns a view over m. It is the caller's responsibility to make sure V is immutable.
func (Map[K, V]) AsMap ¶
func (m Map[K, V]) AsMap() map[K]V
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 (Map[K, V]) GetOk ¶
GetOk returns the element with key k and a bool representing whether the key is in map.
func (Map[K, V]) Has ¶
Has reports whether k has an entry in the map. Deprecated: use Contains instead.
func (Map[K, V]) MarshalJSON ¶
MarshalJSON implements jsonv1.Marshaler.
func (Map[K, V]) MarshalJSONTo ¶
MarshalJSONTo implements jsonv2.MarshalerTo.
func (*Map[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements jsonv1.Unmarshaler. It should only be called on an uninitialized Map.
func (*Map[K, V]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements jsonv2.UnmarshalerFrom. It must only be called on an uninitialized Map.
type MapFn ¶
type MapFn[K comparable, T any, V any] struct { // contains filtered or unexported fields }
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.
func MapFnOf ¶
func MapFnOf[K comparable, T any, V any](m map[K]T, f func(T) V) MapFn[K, T, V]
MapFnOf returns a MapFn for m.
func (MapFn[K, T, V]) GetOk ¶
GetOk returns the element with key k and a bool representing whether the key is in map.
func (MapFn[K, T, V]) Has ¶
Has reports whether k has an entry in the map. Deprecated: use Contains instead.
type MapRangeFn ¶
type MapRangeFn[K comparable, V any] func(k K, v V) (cont bool)
MapRangeFn is the func called from a Map.Range call. Implementations should return false to stop range.
type MapSlice ¶
type MapSlice[K comparable, V any] struct { // contains filtered or unexported fields }
MapSlice is a view over a map whose values are slices.
func MapSliceOf ¶
func MapSliceOf[K comparable, V any](m map[K][]V) MapSlice[K, V]
MapSliceOf returns a MapSlice for the provided map. It is the caller's responsibility to make sure V is immutable.
func (MapSlice[K, V]) AsMap ¶
func (m MapSlice[K, V]) AsMap() map[K][]V
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. The map and slices are cloned, but the values are not.
func (MapSlice[K, V]) GetOk ¶
GetOk returns the element with key k and a bool representing whether the key is in map.
func (MapSlice[K, V]) MarshalJSON ¶
MarshalJSON implements jsonv1.Marshaler.
func (MapSlice[K, V]) MarshalJSONTo ¶
MarshalJSONTo implements jsonv2.MarshalerTo.
func (*MapSlice[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements jsonv1.Unmarshaler. It should only be called on an uninitialized Map.
func (*MapSlice[K, V]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements jsonv2.UnmarshalerFrom. It should only be called on an uninitialized MapSlice.
type Slice ¶
type Slice[T any] struct { // contains filtered or unexported fields }
Slice is a read-only accessor for a slice.
func SliceOf ¶
SliceOf returns a Slice for the provided slice for immutable values. It is the caller's responsibility to make sure V is immutable.
func (Slice[T]) AppendTo ¶
func (v Slice[T]) AppendTo(dst []T) []T
AppendTo appends the underlying slice values to dst.
func (Slice[T]) AsSlice ¶
func (v Slice[T]) AsSlice() []T
AsSlice returns a copy of underlying slice.
func (Slice[T]) ContainsFunc ¶
ContainsFunc reports whether any element in v satisfies f(e).
As it runs in O(n) time, use with care.
func (Slice[T]) IndexFunc ¶
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 (Slice[T]) MapKey ¶
func (v Slice[T]) MapKey() SliceMapKey[T]
MapKey returns a unique key for a slice, based on its address and length.
func (Slice[T]) MarshalJSON ¶
MarshalJSON implements jsonv1.Marshaler.
func (Slice[T]) MarshalJSONTo ¶
MarshalJSONTo implements jsonv2.MarshalerTo.
func (Slice[T]) MaxFunc ¶
MaxFunc returns the maximal value in v, using cmp to compare elements. It panics if v is empty. If there is more than one maximal element according to the cmp function, MaxFunc returns the first one. See also slices.MaxFunc.
func (Slice[T]) MinFunc ¶
MinFunc returns the minimal value in v, using cmp to compare elements. It panics if v is empty. If there is more than one minimal element according to the cmp function, MinFunc returns the first one. See also slices.MinFunc.
func (*Slice[T]) UnmarshalJSON ¶
UnmarshalJSON implements jsonv1.Unmarshaler. It must only be called on an uninitialized Slice.
func (*Slice[T]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements jsonv2.UnmarshalerFrom. It must only be called on an uninitialized Slice.
type SliceMapKey ¶
type SliceMapKey[T any] struct { // contains filtered or unexported fields }
SliceMapKey represents a comparable unique key for a slice, based on its address and length. It can be used to key maps by slices but should only be used when the underlying slice is immutable.
Empty and nil slices have different keys.
type SliceView ¶
type SliceView[T ViewCloner[T, V], V StructView[T]] struct { // contains filtered or unexported fields }
SliceView wraps []T to provide accessors which return an immutable view V of T. It is used to provide the equivalent of SliceOf([]V) without having to allocate []V from []T.
func SliceOfViews ¶
func SliceOfViews[T ViewCloner[T, V], V StructView[T]](x []T) SliceView[T, V]
SliceOfViews returns a ViewSlice for x.
func (SliceView[T, V]) AppendTo ¶
func (v SliceView[T, V]) AppendTo(dst []V) []V
AppendTo appends the underlying slice values to dst.
func (SliceView[T, V]) AsSlice ¶
func (v SliceView[T, V]) AsSlice() []V
AsSlice returns a copy of underlying slice.
func (SliceView[T, V]) MapKey ¶
func (v SliceView[T, V]) MapKey() SliceMapKey[T]
MapKey returns a unique key for a slice, based on its address and length.
func (SliceView[T, V]) MarshalJSON ¶
MarshalJSON implements jsonv1.Marshaler.
func (SliceView[T, V]) MarshalJSONTo ¶
MarshalJSONTo implements jsonv2.MarshalerTo.
func (*SliceView[T, V]) UnmarshalJSON ¶
UnmarshalJSON implements jsonv1.Unmarshaler. It must only be called on an uninitialized SliceView.
func (*SliceView[T, V]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements jsonv2.UnmarshalerFrom. It must only be called on an uninitialized SliceView.
type StructView ¶
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 }
StructView represents the corresponding StructView of a Viewable. The concrete types are typically generated by github.com/Xinlong-Wu/tailscale-oh/cmd/viewer.
type ValuePointer ¶
type ValuePointer[T any] struct { // contains filtered or unexported fields }
ValuePointer provides a read-only view of a pointer to a value type, such as a primitive type or an immutable struct. Its Value and ValueOk methods return a stack-allocated shallow copy of the underlying value. It is the caller's responsibility to ensure that T is free from memory aliasing/mutation concerns.
func ValuePointerOf ¶
func ValuePointerOf[T any](v *T) ValuePointer[T]
ValuePointerOf returns an immutable view of a pointer to an immutable value. It is the caller's responsibility to ensure that T is free from memory aliasing/mutation concerns.
func (ValuePointer[T]) Clone ¶
func (p ValuePointer[T]) Clone() *T
Clone returns a shallow copy of the underlying value.
func (ValuePointer[T]) Get ¶
func (p ValuePointer[T]) Get() T
Get returns a shallow copy of the value if the underlying pointer is non-nil. Otherwise, it returns a zero value.
func (ValuePointer[T]) GetOk ¶
func (p ValuePointer[T]) GetOk() (value T, ok bool)
GetOk returns a shallow copy of the underlying value and true if the underlying pointer is non-nil. Otherwise, it returns a zero value and false.
func (ValuePointer[T]) GetOr ¶
func (p ValuePointer[T]) GetOr(def T) T
GetOr returns a shallow copy of the underlying value if it is non-nil. Otherwise, it returns the provided default value.
func (ValuePointer[T]) MarshalJSON ¶
func (p ValuePointer[T]) MarshalJSON() ([]byte, error)
MarshalJSON implements jsonv1.Marshaler.
func (ValuePointer[T]) MarshalJSONTo ¶
func (p ValuePointer[T]) MarshalJSONTo(enc *jsontext.Encoder) error
MarshalJSONTo implements jsonv2.MarshalerTo.
func (ValuePointer[T]) String ¶
func (p ValuePointer[T]) String() string
String implements fmt.Stringer.
func (*ValuePointer[T]) UnmarshalJSON ¶
func (p *ValuePointer[T]) UnmarshalJSON(b []byte) error
UnmarshalJSON implements jsonv1.Unmarshaler. It must only be called on an uninitialized ValuePointer.
func (*ValuePointer[T]) UnmarshalJSONFrom ¶
func (p *ValuePointer[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
UnmarshalJSONFrom implements jsonv2.UnmarshalerFrom. It must only be called on an uninitialized ValuePointer.
func (ValuePointer[T]) Valid ¶
func (p ValuePointer[T]) Valid() bool
Valid reports whether the underlying pointer is non-nil.
type ViewCloner ¶
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 }
ViewCloner is any type that has had View and Clone funcs generated using github.com/Xinlong-Wu/tailscale-oh/cmd/viewer.