Skip to content

Commit 1d7f66f

Browse files
committed
encoding/openapi: swap sort.Slice for slices.Sort
A cleanup to use the newer cmp-based APIs. While here, reuse the label helper. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I362773d2c0cdaef53362010f0f7d27f064e36075 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208400 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent db72890 commit 1d7f66f

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

encoding/openapi/build.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
package openapi
1616

1717
import (
18+
"cmp"
1819
"fmt"
1920
"math"
2021
"path"
2122
"regexp"
2223
"slices"
23-
"sort"
2424
"strings"
2525

2626
"cuelang.org/go/cue"
@@ -166,11 +166,8 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er
166166
}
167167
}
168168

169-
a := c.schemas.Elts
170-
sort.Slice(a, func(i, j int) bool {
171-
x, _, _ := ast.LabelName(a[i].(*ast.Field).Label)
172-
y, _, _ := ast.LabelName(a[j].(*ast.Field).Label)
173-
return x < y
169+
slices.SortFunc(c.schemas.Elts, func(a, b ast.Decl) int {
170+
return cmp.Compare(label(a), label(b))
174171
})
175172

176173
return (*ast.StructLit)(c.schemas), c.errs
@@ -286,15 +283,12 @@ func value(d ast.Decl) ast.Expr {
286283
}
287284

288285
func sortSchema(s *ast.StructLit) {
289-
sort.Slice(s.Elts, func(i, j int) bool {
290-
iName := label(s.Elts[i])
291-
jName := label(s.Elts[j])
292-
pi := fieldOrder[iName]
293-
pj := fieldOrder[jName]
294-
if pi != pj {
295-
return pi > pj
296-
}
297-
return iName < jName
286+
slices.SortFunc(s.Elts, func(a, b ast.Decl) int {
287+
aName := label(a)
288+
bName := label(b)
289+
aOrder := fieldOrder[aName]
290+
bOrder := fieldOrder[bName]
291+
return cmp.Or(-cmp.Compare(aOrder, bOrder), cmp.Compare(aName, bName))
298292
})
299293
}
300294

0 commit comments

Comments
 (0)