mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
refactor: deduplicate utility helpers across the codebase (#23338)
Audited exported helpers in `coderd/util/*`, `testutil`, `cryptorand`, and friends, then replaced duplicated implementations with canonical versions. - **fix: `maps.SortedKeys` generic signature** — value type was hardcoded to `any`, making it impossible to actually call. Added second type parameter `V any`. Added table-driven tests with `cmp.Diff`. - **refactor: replace ad-hoc ptr helpers with `ptr.Ref`** — removed `int64Ptr`, `stringPtr`, `boolPtr`, `i64ptr`, `strPtr`, `PtrInt32` across 6 files. - **refactor: replace local `sortedKeys`/`sortKeys` with `maps.SortedKeys`** — now that the signature is fixed, scripts can use it. - **refactor: replace hand-rolled `capitalize` with `strings.Capitalize`** — the typegen version was also not UTF-8 safe. > 🤖 This PR was created with the help of Coder Agents, and was reviewed by my human. 🧑💻
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/prometheus/common/expfmt"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/util/maps"
|
||||
"github.com/coder/coder/v2/scripts/atomicwrite"
|
||||
)
|
||||
|
||||
@@ -176,7 +177,7 @@ func updatePrometheusDoc(doc []byte, metricFamilies []*dto.MetricFamily) ([]byte
|
||||
}
|
||||
|
||||
if len(labels) > 0 {
|
||||
_, _ = buffer.WriteString(strings.Join(sortedKeys(labels), " "))
|
||||
_, _ = buffer.WriteString(strings.Join(maps.SortedKeys(labels), " "))
|
||||
}
|
||||
|
||||
_, _ = buffer.WriteString(" |\n")
|
||||
@@ -190,12 +191,3 @@ func updatePrometheusDoc(doc []byte, metricFamilies []*dto.MetricFamily) ([]byte
|
||||
func writePrometheusDoc(doc []byte) error {
|
||||
return atomicwrite.File(prometheusDocFile, doc)
|
||||
}
|
||||
|
||||
func sortedKeys(m map[string]struct{}) []string {
|
||||
var keys []string
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user