Skip to content

Commit 6e7cd01

Browse files
committed
chore: simplify sorting
1 parent 816d426 commit 6e7cd01

File tree

2 files changed

+24
-47
lines changed

2 files changed

+24
-47
lines changed

tagalign.go

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package tagalign
22

33
import (
4+
"cmp"
45
"fmt"
56
"go/ast"
67
"reflect"
7-
"sort"
8+
"slices"
89
"strconv"
910
"strings"
1011

@@ -200,7 +201,7 @@ func (w *Helper) Process(pass *analysis.Pass) {
200201
cp[i] = tag
201202
}
202203
notSortedTagsGroup = append(notSortedTagsGroup, cp)
203-
sortBy(w.fixedTagOrder, tags)
204+
sortTags(w.fixedTagOrder, tags)
204205
}
205206
for _, t := range tags.Tags() {
206207
addKey(t.Key)
@@ -211,7 +212,7 @@ func (w *Helper) Process(pass *analysis.Pass) {
211212
}
212213

213214
if w.sort && StrictStyle == w.style {
214-
sortAllKeys(w.fixedTagOrder, uniqueKeys)
215+
sortKeys(w.fixedTagOrder, uniqueKeys)
215216
maxTagNum = len(uniqueKeys)
216217
}
217218

@@ -318,7 +319,7 @@ func (w *Helper) Process(pass *analysis.Pass) {
318319
}
319320
originalTags := append([]*structtag.Tag(nil), tags.Tags()...)
320321
if w.sort {
321-
sortBy(w.fixedTagOrder, tags)
322+
sortTags(w.fixedTagOrder, tags)
322323
}
323324

324325
newTagValue := fmt.Sprintf("`%s`", tags.String())
@@ -333,61 +334,37 @@ func (w *Helper) Process(pass *analysis.Pass) {
333334
}
334335
}
335336

336-
// sortBy sorts tags by fixed order.
337+
// sortTags sorts tags by fixed order.
337338
// If a tag is not in the fixed order, it will be sorted by name.
338-
func sortBy(fixedOrder []string, tags *structtag.Tags) {
339-
// sort by fixed order
340-
sort.Slice(tags.Tags(), func(i, j int) bool {
341-
ti := tags.Tags()[i]
342-
tj := tags.Tags()[j]
343-
344-
oi := findIndex(fixedOrder, ti.Key)
345-
oj := findIndex(fixedOrder, tj.Key)
346-
347-
if oi == -1 && oj == -1 {
348-
return ti.Key < tj.Key
349-
}
350-
351-
if oi == -1 {
352-
return false
353-
}
354-
355-
if oj == -1 {
356-
return true
357-
}
358-
359-
return oi < oj
339+
func sortTags(fixedOrder []string, tags *structtag.Tags) {
340+
slices.SortFunc(tags.Tags(), func(a, b *structtag.Tag) int {
341+
return compareByFixedOrder(fixedOrder)(a.Key, b.Key)
360342
})
361343
}
362344

363-
func sortAllKeys(fixedOrder []string, keys []string) {
364-
sort.Slice(keys, func(i, j int) bool {
365-
oi := findIndex(fixedOrder, keys[i])
366-
oj := findIndex(fixedOrder, keys[j])
345+
func sortKeys(fixedOrder []string, keys []string) {
346+
slices.SortFunc(keys, compareByFixedOrder(fixedOrder))
347+
}
348+
349+
func compareByFixedOrder(fixedOrder []string) func(a, b string) int {
350+
return func(a, b string) int {
351+
oi := slices.Index(fixedOrder, a)
352+
oj := slices.Index(fixedOrder, b)
367353

368354
if oi == -1 && oj == -1 {
369-
return keys[i] < keys[j]
355+
return strings.Compare(a, b)
370356
}
371357

372358
if oi == -1 {
373-
return false
359+
return 1
374360
}
375361

376362
if oj == -1 {
377-
return true
363+
return -1
378364
}
379365

380-
return oi < oj
381-
})
382-
}
383-
384-
func findIndex(s []string, e string) int {
385-
for i, a := range s {
386-
if a == e {
387-
return i
388-
}
366+
return cmp.Compare(oi, oj)
389367
}
390-
return -1
391368
}
392369

393370
func alignFormat(length int) string {

tagalign_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ func Test_alignAndSortWithOrder(t *testing.T) {
4040
analysistest.Run(t, sort, a)
4141
}
4242

43-
func TestSprintf(t *testing.T) {
43+
func Test_alignFormat(t *testing.T) {
4444
format := alignFormat(20)
4545
assert.Equal(t, "%-20s", format)
4646
}
4747

48-
func Test_sortBy(t *testing.T) {
48+
func Test_sortTags(t *testing.T) {
4949
tags, err := structtag.Parse(`zip:"foo" json:"foo,omitempty" yaml:"bar" binding:"required" xml:"baz" gorm:"column:foo"`)
5050
assert.NoError(t, err)
5151

52-
sortBy([]string{"json", "yaml", "xml"}, tags)
52+
sortTags([]string{"json", "yaml", "xml"}, tags)
5353
assert.Equal(t, "json", tags.Tags()[0].Key)
5454
assert.Equal(t, "yaml", tags.Tags()[1].Key)
5555
assert.Equal(t, "xml", tags.Tags()[2].Key)

0 commit comments

Comments
 (0)