all: use any instead of interface{}

My favorite part of generics.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
Josh Bleecher Snyder
2022-03-16 16:27:57 -07:00
committed by Josh Bleecher Snyder
parent 5f176f24db
commit 0868329936
88 changed files with 204 additions and 204 deletions

View File

@@ -32,7 +32,7 @@ var _ResolverCloneNeedsRegeneration = Resolver(struct {
// Clone duplicates src into dst and reports whether it succeeded.
// To succeed, <src, dst> must be of types <*T, *T> or <*T, **T>,
// where T is one of Resolver.
func Clone(dst, src interface{}) bool {
func Clone(dst, src any) bool {
switch src := src.(type) {
case *Resolver:
switch dst := dst.(type) {

View File

@@ -28,7 +28,7 @@ import (
// Logf is the basic Tailscale logger type: a printf-like func.
// Like log.Printf, the format need not end in a newline.
// Logf functions must be safe for concurrent use.
type Logf func(format string, args ...interface{})
type Logf func(format string, args ...any)
// A Context is a context.Context that should contain a custom log function, obtainable from FromContext.
// If no log function is present, FromContext will return log.Printf.
@@ -43,7 +43,7 @@ type jenc struct {
enc *json.Encoder
}
var jencPool = &sync.Pool{New: func() interface{} {
var jencPool = &sync.Pool{New: func() any {
je := new(jenc)
je.enc = json.NewEncoder(&je.buf)
return je
@@ -61,7 +61,7 @@ var jencPool = &sync.Pool{New: func() interface{} {
//
// The level can be from 0 to 9. Levels from 1 to 9 are included in
// the logged JSON object, like {"foo":123,"v":2}.
func (logf Logf) JSON(level int, recType string, v interface{}) {
func (logf Logf) JSON(level int, recType string, v any) {
je := jencPool.Get().(*jenc)
defer jencPool.Put(je)
je.buf.Reset()
@@ -96,7 +96,7 @@ func Ctx(ctx context.Context, fn Logf) Context {
// WithPrefix wraps f, prefixing each format with the provided prefix.
func WithPrefix(f Logf, prefix string) Logf {
return func(format string, args ...interface{}) {
return func(format string, args ...any) {
f(prefix+format, args...)
}
}
@@ -119,7 +119,7 @@ func (w funcWriter) Write(p []byte) (int, error) {
}
// Discard is a Logf that throws away the logs given to it.
func Discard(string, ...interface{}) {}
func Discard(string, ...any) {}
// limitData is used to keep track of each format string's associated
// rate-limiting data.
@@ -165,7 +165,7 @@ func RateLimitedFnWithClock(logf Logf, f time.Duration, burst int, maxCache int,
msgCache = list.New() // a rudimentary LRU that limits the size of the map
)
return func(format string, args ...interface{}) {
return func(format string, args ...any) {
// Shortcut for formats with no rate limit
for _, sub := range rateFree {
if strings.Contains(format, sub) {
@@ -239,7 +239,7 @@ func LogOnChange(logf Logf, maxInterval time.Duration, timeNow func() time.Time)
tLastLogged = timeNow()
)
return func(format string, args ...interface{}) {
return func(format string, args ...any) {
s := fmt.Sprintf(format, args...)
mu.Lock()
@@ -270,13 +270,13 @@ func (fn ArgWriter) Format(f fmt.State, _ rune) {
argBufioPool.Put(bw)
}
var argBufioPool = &sync.Pool{New: func() interface{} { return bufio.NewWriterSize(ioutil.Discard, 1024) }}
var argBufioPool = &sync.Pool{New: func() any { return bufio.NewWriterSize(ioutil.Discard, 1024) }}
// Filtered returns a Logf that silently swallows some log lines.
// Each inbound format and args is evaluated and printed to a string s.
// The original format and args are passed to logf if and only if allow(s) returns true.
func Filtered(logf Logf, allow func(s string) bool) Logf {
return func(format string, args ...interface{}) {
return func(format string, args ...any) {
msg := fmt.Sprintf(format, args...)
if !allow(msg) {
return
@@ -297,7 +297,7 @@ func LogfCloser(logf Logf) (newLogf Logf, close func()) {
defer mu.Unlock()
closed = true
}
newLogf = func(msg string, args ...interface{}) {
newLogf = func(msg string, args ...any) {
mu.Lock()
if closed {
mu.Unlock()

View File

@@ -30,7 +30,7 @@ func TestStdLogger(t *testing.T) {
}
func logTester(want []string, t *testing.T, i *int) Logf {
return func(format string, args ...interface{}) {
return func(format string, args ...any) {
got := fmt.Sprintf(format, args...)
if *i >= len(want) {
t.Fatalf("Logging continued past end of expected input: %s", got)
@@ -204,7 +204,7 @@ func TestContext(t *testing.T) {
// Test that FromContext and Ctx work together.
var called bool
markCalled := func(string, ...interface{}) {
markCalled := func(string, ...any) {
called = true
}
ctx = Ctx(ctx, markCalled)
@@ -215,7 +215,7 @@ func TestContext(t *testing.T) {
func TestJSON(t *testing.T) {
var buf bytes.Buffer
var logf Logf = func(f string, a ...interface{}) { fmt.Fprintf(&buf, f, a...) }
var logf Logf = func(f string, a ...any) { fmt.Fprintf(&buf, f, a...) }
logf.JSON(1, "foo", &tailcfg.Hostinfo{})
want := "[v\x00JSON]1" + `{"foo":{"OS":"","Hostname":""}}`
if got := buf.String(); got != want {

View File

@@ -13,7 +13,7 @@ import (
// a prefixed log message to each line with the current binary memory usage
// and max RSS.
func RusagePrefixLog(logf Logf) Logf {
return func(f string, argv ...interface{}) {
return func(f string, argv ...any) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
goMem := float64(m.HeapInuse+m.StackInuse) / (1 << 20)

View File

@@ -30,7 +30,7 @@ func (b Bool) Get() (v bool, ok bool) {
}
// Scan implements database/sql.Scanner.
func (b *Bool) Scan(src interface{}) error {
func (b *Bool) Scan(src any) error {
if src == nil {
*b = ""
return nil

View File

@@ -13,7 +13,7 @@ import (
func TestBool(t *testing.T) {
tests := []struct {
name string
in interface{}
in any
want string // JSON
}{
{