ipn/prefs: add views

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali
2022-10-21 23:05:43 +00:00
committed by Maisem Ali
parent ba459aeef5
commit 20324eeebc
5 changed files with 231 additions and 2 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ import (
"tailscale.com/types/structs"
)
//go:generate go run tailscale.com/cmd/cloner -type=Persist
//go:generate go run tailscale.com/cmd/viewer -type=Persist
// Persist is the JSON type stored on disk on nodes to remember their
// settings between runs.
+80
View File
@@ -0,0 +1,80 @@
// 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.
// Code generated by tailscale/cmd/viewer; DO NOT EDIT.
package persist
import (
"encoding/json"
"errors"
"tailscale.com/types/key"
"tailscale.com/types/structs"
)
//go:generate go run tailscale.com/cmd/cloner -clonefunc=false -type=Persist
// View returns a readonly view of Persist.
func (p *Persist) View() PersistView {
return PersistView{ж: p}
}
// PersistView provides a read-only view over Persist.
//
// Its methods should only be called if `Valid()` returns true.
type PersistView 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.
ж *Persist
}
// Valid reports whether underlying value is non-nil.
func (v PersistView) Valid() bool { return v.ж != nil }
// AsStruct returns a clone of the underlying value which aliases no memory with
// the original.
func (v PersistView) AsStruct() *Persist {
if v.ж == nil {
return nil
}
return v.ж.Clone()
}
func (v PersistView) MarshalJSON() ([]byte, error) { return json.Marshal(v.ж) }
func (v *PersistView) UnmarshalJSON(b []byte) error {
if v.ж != nil {
return errors.New("already initialized")
}
if len(b) == 0 {
return nil
}
var x Persist
if err := json.Unmarshal(b, &x); err != nil {
return err
}
v.ж = &x
return nil
}
func (v PersistView) LegacyFrontendPrivateMachineKey() key.MachinePrivate {
return v.ж.LegacyFrontendPrivateMachineKey
}
func (v PersistView) PrivateNodeKey() key.NodePrivate { return v.ж.PrivateNodeKey }
func (v PersistView) OldPrivateNodeKey() key.NodePrivate { return v.ж.OldPrivateNodeKey }
func (v PersistView) Provider() string { return v.ж.Provider }
func (v PersistView) LoginName() string { return v.ж.LoginName }
// A compilation failure here means this code must be regenerated, with the command at the top of this file.
var _PersistViewNeedsRegeneration = Persist(struct {
_ structs.Incomparable
LegacyFrontendPrivateMachineKey key.MachinePrivate
PrivateNodeKey key.NodePrivate
OldPrivateNodeKey key.NodePrivate
Provider string
LoginName string
}{})
+5
View File
@@ -201,6 +201,11 @@ func (v IPPrefixSlice) AsSlice() []netip.Prefix {
return v.ж.AsSlice()
}
// 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)
}
// PrefixesContainsIP reports whether any IPPrefix contains IP.
func (v IPPrefixSlice) ContainsIP(ip netip.Addr) bool {
return tsaddr.PrefixesContainsIP(v.ж.ж, ip)