Skip to content

Commit

Permalink
encoding/openapi: swap sort.Slice for slices.Sort
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Feb 10, 2025
1 parent db72890 commit 1d7f66f
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions encoding/openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package openapi

import (
"cmp"
"fmt"
"math"
"path"
"regexp"
"slices"
"sort"
"strings"

"cuelang.org/go/cue"
Expand Down Expand Up @@ -166,11 +166,8 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er
}
}

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

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

func sortSchema(s *ast.StructLit) {
sort.Slice(s.Elts, func(i, j int) bool {
iName := label(s.Elts[i])
jName := label(s.Elts[j])
pi := fieldOrder[iName]
pj := fieldOrder[jName]
if pi != pj {
return pi > pj
}
return iName < jName
slices.SortFunc(s.Elts, func(a, b ast.Decl) int {
aName := label(a)
bName := label(b)
aOrder := fieldOrder[aName]
bOrder := fieldOrder[bName]
return cmp.Or(-cmp.Compare(aOrder, bOrder), cmp.Compare(aName, bName))
})
}

Expand Down

0 comments on commit 1d7f66f

Please sign in to comment.