chore: Replace sort package with slices and cmp packages - #986
Open
dongjiang1989 wants to merge 1 commit into
Open
chore: Replace sort package with slices and cmp packages#986dongjiang1989 wants to merge 1 commit into
dongjiang1989 wants to merge 1 commit into
Conversation
Replace all usage of Go's sort package with the modern slices and cmp packages (Go 1.21+) for more concise and readable sorting code. - Remove sort.Interface methods (Len/Less/Swap) from LabelNames, LabelValues, LabelPairs, Fingerprints, Alerts, Samples, Vector, and Matrix types. - Add Compare methods on *Sample, *SampleStream, *Alert, and *LabelPair for use with slices.SortFunc. - Replace sort.Sort calls with slices.SortFunc using the new Compare methods, and sort.Strings with slices.Sort. - Add depguard rule in .golangci.yml to forbid the sort package and prevent future regressions. - Fix pre-existing gofumpt formatting issues in config and route packages. Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace all usage of Go's
sortpackage with the modernslicesandcmppackages (Go 1.21+) for more concise and readable sorting code.Changes
sort.Interfacemethods (Len/Less/Swap) fromLabelNames,LabelValues,LabelPairs,Fingerprints,Alerts,Samples,Vector, andMatrixtypes.Comparemethods on*Sample,*SampleStream,*Alert, and*LabelPairfor use withslices.SortFunc, eliminating duplicated comparator code at call sites.sort.Sortcalls withslices.SortFunc(using the newComparemethods orcmp.Comparefor simple ordered types), andsort.Stringswithslices.Sort.depguardrule in.golangci.ymlto forbid thesortpackage and prevent future regressions.gofumptformatting issues inconfigandroutepackages (uncovered bymake lint).Note
The original
Alerts.Lesswas not a proper strict weak ordering (it usedStartsBefore || EndsBefore || FPLess). The new(*Alert).Compareimplements a correct hierarchical comparison (StartsAt → EndsAt → Fingerprint).TestSortAlertsexpected values were updated accordingly.Verification
go build ./...✅go vet ./...✅make lint✅go test ./model/... ./expfmt/...✅