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:
committed by
Josh Bleecher Snyder
parent
5f176f24db
commit
0868329936
+4
-4
@@ -52,7 +52,7 @@ func Debugger(mux *http.ServeMux) *DebugHandler {
|
||||
// index page. The /pprof/ index already covers it.
|
||||
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
|
||||
|
||||
ret.KVFunc("Uptime", func() interface{} { return Uptime() })
|
||||
ret.KVFunc("Uptime", func() any { return Uptime() })
|
||||
ret.KV("Version", version.Long)
|
||||
ret.Handle("vars", "Metrics (Go)", expvar.Handler())
|
||||
ret.Handle("varz", "Metrics (Prometheus)", http.HandlerFunc(VarzHandler))
|
||||
@@ -79,7 +79,7 @@ func (d *DebugHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
f := func(format string, args ...interface{}) { fmt.Fprintf(w, format, args...) }
|
||||
f := func(format string, args ...any) { fmt.Fprintf(w, format, args...) }
|
||||
f("<html><body><h1>%s debug</h1><ul>", version.CmdName())
|
||||
for _, kv := range d.kvs {
|
||||
kv(w)
|
||||
@@ -101,7 +101,7 @@ func (d *DebugHandler) Handle(slug, desc string, handler http.Handler) {
|
||||
}
|
||||
|
||||
// KV adds a key/value list item to /debug/.
|
||||
func (d *DebugHandler) KV(k string, v interface{}) {
|
||||
func (d *DebugHandler) KV(k string, v any) {
|
||||
val := html.EscapeString(fmt.Sprintf("%v", v))
|
||||
d.kvs = append(d.kvs, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "<li><b>%s:</b> %s</li>", k, val)
|
||||
@@ -110,7 +110,7 @@ func (d *DebugHandler) KV(k string, v interface{}) {
|
||||
|
||||
// KVFunc adds a key/value list item to /debug/. v is called on every
|
||||
// render of /debug/.
|
||||
func (d *DebugHandler) KVFunc(k string, v func() interface{}) {
|
||||
func (d *DebugHandler) KVFunc(k string, v func() any) {
|
||||
d.kvs = append(d.kvs, func(w io.Writer) {
|
||||
val := html.EscapeString(fmt.Sprintf("%v", v()))
|
||||
fmt.Fprintf(w, "<li><b>%s:</b> %s</li>", k, val)
|
||||
|
||||
+2
-2
@@ -66,7 +66,7 @@ func TestDebuggerKV(t *testing.T) {
|
||||
dbg.KV("Donuts", 42)
|
||||
dbg.KV("Secret code", "hunter2")
|
||||
val := "red"
|
||||
dbg.KVFunc("Condition", func() interface{} { return val })
|
||||
dbg.KVFunc("Condition", func() any { return val })
|
||||
|
||||
code, _ := get(mux, "/debug/", pubIP)
|
||||
if code != 403 {
|
||||
@@ -183,7 +183,7 @@ func ExampleDebugHandler_KVFunc() {
|
||||
// Adds an count of page renders to /debug/. Note this example
|
||||
// isn't concurrency-safe.
|
||||
views := 0
|
||||
dbg.KVFunc("Debug pageviews", func() interface{} {
|
||||
dbg.KVFunc("Debug pageviews", func() any {
|
||||
views = views + 1
|
||||
return views
|
||||
})
|
||||
|
||||
@@ -19,16 +19,16 @@ import (
|
||||
)
|
||||
|
||||
type response struct {
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// JSONHandlerFunc is an HTTP ReturnHandler that writes JSON responses to the client.
|
||||
//
|
||||
// Return a HTTPError to show an error message, otherwise JSONHandlerFunc will
|
||||
// only report "internal server error" to the user with status code 500.
|
||||
type JSONHandlerFunc func(r *http.Request) (status int, data interface{}, err error)
|
||||
type JSONHandlerFunc func(r *http.Request) (status int, data any, err error)
|
||||
|
||||
// ServeHTTPReturn implements the ReturnHandler interface.
|
||||
//
|
||||
|
||||
+10
-10
@@ -71,7 +71,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
return d
|
||||
}
|
||||
|
||||
h21 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
h21 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
return http.StatusOK, nil, nil
|
||||
})
|
||||
|
||||
@@ -83,7 +83,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("403 HTTPError", func(t *testing.T) {
|
||||
h := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
h := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
return 0, nil, Error(http.StatusForbidden, "forbidden", nil)
|
||||
})
|
||||
|
||||
@@ -93,7 +93,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
checkStatus(t, w, "error", http.StatusForbidden)
|
||||
})
|
||||
|
||||
h22 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
h22 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
return http.StatusOK, &Data{Name: "tailscale"}, nil
|
||||
})
|
||||
|
||||
@@ -104,7 +104,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
checkStatus(t, w, "success", http.StatusOK)
|
||||
})
|
||||
|
||||
h31 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
h31 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
body := new(Data)
|
||||
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
|
||||
return 0, nil, Error(http.StatusBadRequest, err.Error(), err)
|
||||
@@ -140,7 +140,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
h32 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
h32 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
body := new(Data)
|
||||
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
|
||||
return 0, nil, Error(http.StatusBadRequest, err.Error(), err)
|
||||
@@ -187,7 +187,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Price": 10}`))
|
||||
r.Header.Set("Accept-Encoding", "gzip")
|
||||
value := []string{"foo", "foo", "foo"}
|
||||
JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
return 400, value, nil
|
||||
}).ServeHTTPReturn(w, r)
|
||||
res := w.Result()
|
||||
@@ -222,7 +222,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
t.Run("500 misuse", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
return http.StatusOK, make(chan int), nil
|
||||
}).ServeHTTPReturn(w, r)
|
||||
resp := checkStatus(t, w, "error", http.StatusInternalServerError)
|
||||
@@ -234,7 +234,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
t.Run("500 empty status code", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
JSONHandlerFunc(func(r *http.Request) (status int, data interface{}, err error) {
|
||||
JSONHandlerFunc(func(r *http.Request) (status int, data any, err error) {
|
||||
return
|
||||
}).ServeHTTPReturn(w, r)
|
||||
checkStatus(t, w, "error", http.StatusInternalServerError)
|
||||
@@ -243,7 +243,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
t.Run("403 forbidden, status returned by JSONHandlerFunc and HTTPError agree", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
return http.StatusForbidden, nil, Error(http.StatusForbidden, "403 forbidden", nil)
|
||||
}).ServeHTTPReturn(w, r)
|
||||
want := &Response{
|
||||
@@ -260,7 +260,7 @@ func TestNewJSONHandler(t *testing.T) {
|
||||
t.Run("403 forbidden, status returned by JSONHandlerFunc and HTTPError do not agree", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
err := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
||||
err := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
||||
return http.StatusInternalServerError, nil, Error(http.StatusForbidden, "403 forbidden", nil)
|
||||
}).ServeHTTPReturn(w, r)
|
||||
if !strings.HasPrefix(err.Error(), "[unexpected]") {
|
||||
|
||||
+8
-8
@@ -33,10 +33,10 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
expvar.Publish("process_start_unix_time", expvar.Func(func() interface{} { return timeStart.Unix() }))
|
||||
expvar.Publish("version", expvar.Func(func() interface{} { return version.Long }))
|
||||
expvar.Publish("counter_uptime_sec", expvar.Func(func() interface{} { return int64(Uptime().Seconds()) }))
|
||||
expvar.Publish("gauge_goroutines", expvar.Func(func() interface{} { return runtime.NumGoroutine() }))
|
||||
expvar.Publish("process_start_unix_time", expvar.Func(func() any { return timeStart.Unix() }))
|
||||
expvar.Publish("version", expvar.Func(func() any { return version.Long }))
|
||||
expvar.Publish("counter_uptime_sec", expvar.Func(func() any { return int64(Uptime().Seconds()) }))
|
||||
expvar.Publish("gauge_goroutines", expvar.Func(func() any { return runtime.NumGoroutine() }))
|
||||
}
|
||||
|
||||
// DevMode controls whether extra output in shown, for when the binary is being run in dev mode.
|
||||
@@ -513,7 +513,7 @@ type PrometheusMetricsReflectRooter interface {
|
||||
expvar.Var
|
||||
|
||||
// PrometheusMetricsReflectRoot returns the struct or struct pointer to walk.
|
||||
PrometheusMetricsReflectRoot() interface{}
|
||||
PrometheusMetricsReflectRoot() any
|
||||
}
|
||||
|
||||
var expvarDo = expvar.Do // pulled out for tests
|
||||
@@ -562,10 +562,10 @@ func foreachExportedStructField(rv reflect.Value, f func(fieldOrJSONName, metric
|
||||
}
|
||||
}
|
||||
|
||||
type expVarPromStructRoot struct{ v interface{} }
|
||||
type expVarPromStructRoot struct{ v any }
|
||||
|
||||
func (r expVarPromStructRoot) PrometheusMetricsReflectRoot() interface{} { return r.v }
|
||||
func (r expVarPromStructRoot) String() string { panic("unused") }
|
||||
func (r expVarPromStructRoot) PrometheusMetricsReflectRoot() any { return r.v }
|
||||
func (r expVarPromStructRoot) String() string { panic("unused") }
|
||||
|
||||
var (
|
||||
_ PrometheusMetricsReflectRooter = expVarPromStructRoot{}
|
||||
|
||||
+6
-6
@@ -241,7 +241,7 @@ func TestStdHandler(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var logs []AccessLogRecord
|
||||
logf := func(fmt string, args ...interface{}) {
|
||||
logf := func(fmt string, args ...any) {
|
||||
if fmt == "%s" {
|
||||
logs = append(logs, args[0].(AccessLogRecord))
|
||||
}
|
||||
@@ -378,19 +378,19 @@ func TestVarzHandler(t *testing.T) {
|
||||
{
|
||||
"func_float64",
|
||||
"counter_x",
|
||||
expvar.Func(func() interface{} { return float64(1.2) }),
|
||||
expvar.Func(func() any { return float64(1.2) }),
|
||||
"# TYPE x counter\nx 1.2\n",
|
||||
},
|
||||
{
|
||||
"func_float64_gauge",
|
||||
"gauge_x",
|
||||
expvar.Func(func() interface{} { return float64(1.2) }),
|
||||
expvar.Func(func() any { return float64(1.2) }),
|
||||
"# TYPE x gauge\nx 1.2\n",
|
||||
},
|
||||
{
|
||||
"func_float64_untyped",
|
||||
"x",
|
||||
expvar.Func(func() interface{} { return float64(1.2) }),
|
||||
expvar.Func(func() any { return float64(1.2) }),
|
||||
"x 1.2\n",
|
||||
},
|
||||
{
|
||||
@@ -466,7 +466,7 @@ foo_AUint16 65535
|
||||
{
|
||||
"func_returning_int",
|
||||
"num_goroutines",
|
||||
expvar.Func(func() interface{} { return 123 }),
|
||||
expvar.Func(func() any { return 123 }),
|
||||
"num_goroutines 123\n",
|
||||
},
|
||||
}
|
||||
@@ -531,6 +531,6 @@ type expvarAdapter struct {
|
||||
|
||||
func (expvarAdapter) String() string { return "{}" } // expvar JSON; unused in test
|
||||
|
||||
func (a expvarAdapter) PrometheusMetricsReflectRoot() interface{} {
|
||||
func (a expvarAdapter) PrometheusMetricsReflectRoot() any {
|
||||
return a.st
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user