2023-01-27 21:37:20 +00:00
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
2022-05-01 23:15:20 +00:00
// Package tests serves a list of tests for tailscale.com/cmd/viewer.
package tests
import (
"fmt"
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"
2024-07-08 15:11:00 +00:00
"golang.org/x/exp/constraints"
2024-07-14 16:45:55 +00:00
"tailscale.com/types/ptr"
2024-07-08 15:11:00 +00:00
"tailscale.com/types/views"
2022-05-01 23:15:20 +00:00
)
2024-08-22 21:33:35 +00:00
//go:generate go run tailscale.com/cmd/viewer --type=StructWithPtrs,StructWithoutPtrs,Map,StructWithSlices,OnlyGetClone,StructWithEmbedded,GenericIntStruct,GenericNoPtrsStruct,GenericCloneableStruct,StructWithContainers,StructWithTypeAliasFields,GenericTypeAliasStruct --clone-only-type=OnlyGetClone
2022-05-01 23:15:20 +00:00
type StructWithoutPtrs struct {
Int int
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
Pfx netip . Prefix
2022-05-01 23:15:20 +00:00
}
type Map struct {
2022-05-09 16:30:39 +00:00
Int map [ string ] int
SliceInt map [ string ] [ ] int
2022-07-13 06:45:37 +00:00
StructPtrWithPtr map [ string ] * StructWithPtrs
StructPtrWithoutPtr map [ string ] * StructWithoutPtrs
StructWithoutPtr map [ string ] StructWithoutPtrs
2022-05-09 16:30:39 +00:00
SlicesWithPtrs map [ string ] [ ] * StructWithPtrs
SlicesWithoutPtrs map [ string ] [ ] * StructWithoutPtrs
StructWithoutPtrKey map [ StructWithoutPtrs ] int ` json:"-" `
2024-07-08 15:11:00 +00:00
StructWithPtr map [ string ] StructWithPtrs
2022-05-09 16:30:39 +00:00
2022-07-13 06:45:37 +00:00
// Unsupported views.
2022-05-09 16:30:39 +00:00
SliceIntPtr map [ string ] [ ] * int
PointerKey map [ * string ] int ` json:"-" `
StructWithPtrKey map [ StructWithPtrs ] int ` json:"-" `
2022-05-01 23:15:20 +00:00
}
type StructWithPtrs struct {
Value * StructWithoutPtrs
Int * int
NoCloneValue * StructWithoutPtrs ` codegen:"noclone" `
}
func ( v * StructWithPtrs ) String ( ) string { return fmt . Sprintf ( "%v" , v . Int ) }
func ( v * StructWithPtrs ) Equal ( v2 * StructWithPtrs ) bool {
return v . Value == v2 . Value
}
type StructWithSlices struct {
Values [ ] StructWithoutPtrs
ValuePointers [ ] * StructWithoutPtrs
StructPointers [ ] * StructWithPtrs
Slice [ ] string
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
Prefixes [ ] netip . Prefix
2022-05-01 23:15:20 +00:00
Data [ ] byte
2024-07-08 15:11:00 +00:00
// Unsupported views.
Structs [ ] StructWithPtrs
Ints [ ] * int
2022-05-01 23:15:20 +00:00
}
2022-08-17 21:56:56 +00:00
type OnlyGetClone struct {
SinViewerPorFavor bool
}
2023-05-09 22:35:47 +00:00
type StructWithEmbedded struct {
A * StructWithPtrs
StructWithSlices
}
2024-07-08 15:11:00 +00:00
type GenericIntStruct [ T constraints . Integer ] struct {
Value T
Pointer * T
Slice [ ] T
Map map [ string ] T
// Unsupported views.
PtrSlice [ ] * T
PtrKeyMap map [ * T ] string ` json:"-" `
PtrValueMap map [ string ] * T
SliceMap map [ string ] [ ] T
}
type BasicType interface {
~ bool | constraints . Integer | constraints . Float | constraints . Complex | ~ string
}
type GenericNoPtrsStruct [ T StructWithoutPtrs | netip . Prefix | BasicType ] struct {
Value T
Pointer * T
Slice [ ] T
Map map [ string ] T
// Unsupported views.
PtrSlice [ ] * T
PtrKeyMap map [ * T ] string ` json:"-" `
PtrValueMap map [ string ] * T
SliceMap map [ string ] [ ] T
}
type GenericCloneableStruct [ T views . ViewCloner [ T , V ] , V views . StructView [ T ] ] struct {
Value T
Slice [ ] T
Map map [ string ] T
// Unsupported views.
Pointer * T
PtrSlice [ ] * T
PtrKeyMap map [ * T ] string ` json:"-" `
PtrValueMap map [ string ] * T
SliceMap map [ string ] [ ] T
}
2024-07-14 16:45:55 +00:00
// Container is a pre-defined container type, such as a collection, an optional
// value or a generic wrapper.
type Container [ T any ] struct {
Item T
}
func ( c * Container [ T ] ) Clone ( ) * Container [ T ] {
if c == nil {
return nil
}
if cloner , ok := any ( c . Item ) . ( views . Cloner [ T ] ) ; ok {
return & Container [ T ] { cloner . Clone ( ) }
}
if ! views . ContainsPointers [ T ] ( ) {
return ptr . To ( * c )
}
panic ( fmt . Errorf ( "%T contains pointers, but is not cloneable" , c . Item ) )
}
// ContainerView is a pre-defined readonly view of a Container[T].
type ContainerView [ T views . ViewCloner [ T , V ] , V views . StructView [ T ] ] 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.
ж * Container [ T ]
}
func ( cv ContainerView [ T , V ] ) Item ( ) V {
return cv . ж . Item . View ( )
}
func ContainerViewOf [ T views . ViewCloner [ T , V ] , V views . StructView [ T ] ] ( c * Container [ T ] ) ContainerView [ T , V ] {
return ContainerView [ T , V ] { c }
}
2024-08-14 20:06:50 +00:00
// MapContainer is a predefined map-like container type.
// Unlike [Container], it has two type parameters, where the value
// is the second parameter.
type MapContainer [ K comparable , V views . Cloner [ V ] ] struct {
Items map [ K ] V
}
func ( c * MapContainer [ K , V ] ) Clone ( ) * MapContainer [ K , V ] {
if c == nil {
return nil
}
var m map [ K ] V
if c . Items != nil {
m = make ( map [ K ] V , len ( c . Items ) )
for i := range m {
m [ i ] = c . Items [ i ] . Clone ( )
}
}
return & MapContainer [ K , V ] { m }
}
// MapContainerView is a pre-defined readonly view of a [MapContainer][K, T].
type MapContainerView [ K comparable , T views . ViewCloner [ T , V ] , V views . StructView [ T ] ] 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.
ж * MapContainer [ K , T ]
}
func ( cv MapContainerView [ K , T , V ] ) Items ( ) views . MapFn [ K , T , V ] {
return views . MapFnOf ( cv . ж . Items , func ( t T ) V { return t . View ( ) } )
}
func MapContainerViewOf [ K comparable , T views . ViewCloner [ T , V ] , V views . StructView [ T ] ] ( c * MapContainer [ K , T ] ) MapContainerView [ K , T , V ] {
return MapContainerView [ K , T , V ] { c }
}
2024-07-14 16:45:55 +00:00
type GenericBasicStruct [ T BasicType ] struct {
Value T
}
type StructWithContainers struct {
2024-08-14 20:06:50 +00:00
IntContainer Container [ int ]
CloneableContainer Container [ * StructWithPtrs ]
BasicGenericContainer Container [ GenericBasicStruct [ int ] ]
CloneableGenericContainer Container [ * GenericNoPtrsStruct [ int ] ]
CloneableMap MapContainer [ int , * StructWithPtrs ]
CloneableGenericMap MapContainer [ int , * GenericNoPtrsStruct [ int ] ]
2024-07-14 16:45:55 +00:00
}
2024-08-22 21:33:35 +00:00
type (
StructWithPtrsAlias = StructWithPtrs
StructWithoutPtrsAlias = StructWithoutPtrs
)
type StructWithTypeAliasFields struct {
WithPtr StructWithPtrsAlias
WithoutPtr StructWithoutPtrsAlias
}
type integer = constraints . Integer
type GenericTypeAliasStruct [ T integer , T2 views . ViewCloner [ T2 , V2 ] , V2 views . StructView [ T2 ] ] struct {
NonCloneable T
Cloneable T2
}