all: use Go 1.26 things, run most gofix modernizers
I omitted a lot of the min/max modernizers because they didn't result in more clear code. Some of it's older "for x := range 123". Also: errors.AsType, any, fmt.Appendf, etc. Updates #18682 Change-Id: I83a451577f33877f962766a5b65ce86f7696471c Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
4453cc5f53
commit
bd2a2d53d3
+4
-5
@@ -13,6 +13,7 @@ import (
|
||||
"expvar"
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
@@ -734,8 +735,8 @@ func (h errorHandler) handleError(w http.ResponseWriter, r *http.Request, lw *lo
|
||||
|
||||
// Extract a presentable, loggable error.
|
||||
var hOK bool
|
||||
var hErr HTTPError
|
||||
if errors.As(err, &hErr) {
|
||||
hErr, hAsOK := errors.AsType[HTTPError](err)
|
||||
if hAsOK {
|
||||
hOK = true
|
||||
if hErr.Code == 0 {
|
||||
lw.logf("[unexpected] HTTPError %v did not contain an HTTP status code, sending internal server error", hErr)
|
||||
@@ -854,9 +855,7 @@ func WriteHTTPError(w http.ResponseWriter, r *http.Request, e HTTPError) {
|
||||
h.Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
// Custom headers from the error.
|
||||
for k, vs := range e.Header {
|
||||
h[k] = vs
|
||||
}
|
||||
maps.Copy(h, e.Header)
|
||||
|
||||
// Write the msg back to the user.
|
||||
w.WriteHeader(e.Code)
|
||||
|
||||
+6
-7
@@ -93,8 +93,8 @@ func prometheusMetric(prefix string, key string) (string, string, string) {
|
||||
typ = "histogram"
|
||||
key = strings.TrimPrefix(key, histogramPrefix)
|
||||
}
|
||||
if strings.HasPrefix(key, labelMapPrefix) {
|
||||
key = strings.TrimPrefix(key, labelMapPrefix)
|
||||
if after, ok := strings.CutPrefix(key, labelMapPrefix); ok {
|
||||
key = after
|
||||
if a, b, ok := strings.Cut(key, "_"); ok {
|
||||
label, key = a, b
|
||||
}
|
||||
@@ -154,7 +154,7 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
|
||||
case PrometheusMetricsReflectRooter:
|
||||
root := v.PrometheusMetricsReflectRoot()
|
||||
rv := reflect.ValueOf(root)
|
||||
if rv.Type().Kind() == reflect.Ptr {
|
||||
if rv.Type().Kind() == reflect.Pointer {
|
||||
if rv.IsNil() {
|
||||
return
|
||||
}
|
||||
@@ -419,8 +419,7 @@ func structTypeSortedFields(t reflect.Type) []sortedStructField {
|
||||
return v.([]sortedStructField)
|
||||
}
|
||||
fields := make([]sortedStructField, 0, t.NumField())
|
||||
for i, n := 0, t.NumField(); i < n; i++ {
|
||||
sf := t.Field(i)
|
||||
for sf := range t.Fields() {
|
||||
name := sf.Name
|
||||
if v := sf.Tag.Get("json"); v != "" {
|
||||
v, _, _ = strings.Cut(v, ",")
|
||||
@@ -433,7 +432,7 @@ func structTypeSortedFields(t reflect.Type) []sortedStructField {
|
||||
}
|
||||
}
|
||||
fields = append(fields, sortedStructField{
|
||||
Index: i,
|
||||
Index: sf.Index[0],
|
||||
Name: name,
|
||||
SortName: removeTypePrefixes(name),
|
||||
MetricType: sf.Tag.Get("metrictype"),
|
||||
@@ -467,7 +466,7 @@ func foreachExportedStructField(rv reflect.Value, f func(fieldOrJSONName, metric
|
||||
sf := ssf.StructFieldType
|
||||
if ssf.MetricType != "" || sf.Type.Kind() == reflect.Struct {
|
||||
f(ssf.Name, ssf.MetricType, rv.Field(ssf.Index))
|
||||
} else if sf.Type.Kind() == reflect.Ptr && sf.Type.Elem().Kind() == reflect.Struct {
|
||||
} else if sf.Type.Kind() == reflect.Pointer && sf.Type.Elem().Kind() == reflect.Struct {
|
||||
fv := rv.Field(ssf.Index)
|
||||
if !fv.IsNil() {
|
||||
f(ssf.Name, ssf.MetricType, fv.Elem())
|
||||
|
||||
Reference in New Issue
Block a user