Skip to content

Commit ee9caf4

Browse files
authored
fix bugs when sections mismatch (#94)
Signed-off-by: Loong <[email protected]>
1 parent 57b97a2 commit ee9caf4

14 files changed

+94
-50
lines changed

pkg/config/config.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
)
1212

1313
var defaultOrder = map[string]int{
14-
"standard": 0,
15-
"default": 1,
16-
"custom": 2,
17-
"blank": 3,
18-
"dot": 4,
14+
section.StandardType: 0,
15+
section.DefaultType: 1,
16+
section.CustomType: 2,
17+
section.BlankType: 3,
18+
section.DotType: 4,
1919
}
2020

2121
type BoolConfig struct {
@@ -52,16 +52,11 @@ func (g YamlConfig) Parse() (*Config, error) {
5252
// if default order sorted sections
5353
if !g.Cfg.CustomOrder {
5454
sort.Slice(sections, func(i, j int) bool {
55-
sectionI, sectionJ := sections[i].String(), sections[j].String()
55+
sectionI, sectionJ := sections[i].Type(), sections[j].Type()
5656

57-
if strings.HasPrefix(sectionI, "prefix(") {
58-
sectionI = "custom"
57+
if strings.Compare(sectionI, sectionJ) == 0 {
58+
return strings.Compare(sections[i].String(), sections[j].String()) < 0
5959
}
60-
61-
if strings.HasPrefix(sectionJ, "prefix(") {
62-
sectionJ = "custom"
63-
}
64-
6560
return defaultOrder[sectionI] < defaultOrder[sectionJ]
6661
})
6762
}

pkg/config/config_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ var testFilesPath = "../gci/internal/testdata"
1414
func TestInitGciConfigFromEmptyYAML(t *testing.T) {
1515
gciCfg, err := InitializeGciConfigFromYAML(path.Join(testFilesPath, "defaultValues.cfg.yaml"))
1616
assert.NoError(t, err)
17-
_ = gciCfg
1817
assert.Equal(t, section.DefaultSections(), gciCfg.Sections)
1918
assert.Equal(t, section.DefaultSectionSeparators(), gciCfg.SectionSeparators)
2019
assert.False(t, gciCfg.Debug)
@@ -25,9 +24,30 @@ func TestInitGciConfigFromEmptyYAML(t *testing.T) {
2524
func TestInitGciConfigFromYAML(t *testing.T) {
2625
gciCfg, err := InitializeGciConfigFromYAML(path.Join(testFilesPath, "configTest.cfg.yaml"))
2726
assert.NoError(t, err)
28-
_ = gciCfg
2927
assert.Equal(t, section.SectionList{section.Default{}}, gciCfg.Sections)
3028
assert.False(t, gciCfg.Debug)
3129
assert.True(t, gciCfg.SkipGenerated)
3230
assert.False(t, gciCfg.CustomOrder)
3331
}
32+
33+
// the custom sections sort alphabetically as default.
34+
func TestParseOrder(t *testing.T) {
35+
cfg := YamlConfig{
36+
SectionStrings: []string{"default", "prefix(github/daixiang0/gci)", "prefix(github/daixiang0/gai)"},
37+
}
38+
gciCfg, err := cfg.Parse()
39+
assert.NoError(t, err)
40+
assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gai"}, section.Custom{Prefix: "github/daixiang0/gci"}}, gciCfg.Sections)
41+
}
42+
43+
func TestParseCustomOrder(t *testing.T) {
44+
cfg := YamlConfig{
45+
SectionStrings: []string{"default", "prefix(github/daixiang0/gci)", "prefix(github/daixiang0/gai)"},
46+
Cfg: BoolConfig{
47+
CustomOrder: true,
48+
},
49+
}
50+
gciCfg, err := cfg.Parse()
51+
assert.NoError(t, err)
52+
assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gci"}, section.Custom{Prefix: "github/daixiang0/gai"}}, gciCfg.Sections)
53+
}

pkg/gci/gci.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"os"
8-
"sort"
9-
"strings"
108
"sync"
119

1210
"github.com/hexops/gotextdiff"
@@ -143,39 +141,12 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
143141
head := src[:headEnd]
144142
tail := src[tailStart:]
145143

146-
// sort for custom sections
147-
customKeys := make([]string, 0, len(result))
148-
for k := range result {
149-
if strings.HasPrefix(k, "prefix(") {
150-
customKeys = append(customKeys, k)
151-
}
152-
}
153-
154144
firstWithIndex := true
155145

156146
var body []byte
157147

158148
// order by section list
159149
for _, s := range cfg.Sections {
160-
if strings.HasPrefix(s.String(), "prefix(") {
161-
if len(customKeys) > 0 {
162-
if body != nil && len(body) > 0 {
163-
body = append(body, utils.Linebreak)
164-
}
165-
sort.Sort(sort.StringSlice(customKeys))
166-
for _, k := range customKeys {
167-
if !strings.Contains(s.String(), k) {
168-
continue
169-
}
170-
for _, d := range result[k] {
171-
AddIndent(&body, &firstWithIndex)
172-
body = append(body, src[d.Start:d.End]...)
173-
}
174-
}
175-
}
176-
177-
continue
178-
}
179150
if len(result[s.String()]) > 0 {
180151
if body != nil && len(body) > 0 {
181152
body = append(body, utils.Linebreak)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sections:
2+
- Standard
3+
- Default
4+
- Prefix(github.com/daixiang0)
5+
- Prefix(github.com/daixiang0/gci)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
import (
3+
"fmt"
4+
5+
g "github.com/golang"
6+
7+
"github.com/daixiang0/gci"
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
import (
3+
"fmt"
4+
5+
g "github.com/golang"
6+
7+
"github.com/daixiang0/gci"
8+
)

pkg/section/blank.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
type Blank struct{}
99

10+
const BlankType = "blank"
11+
1012
func (b Blank) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
1113
if spec.Name == "_" {
1214
return specificity.NameMatch{}
@@ -15,5 +17,9 @@ func (b Blank) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecifi
1517
}
1618

1719
func (b Blank) String() string {
18-
return "blank"
20+
return BlankType
21+
}
22+
23+
func (b Blank) Type() string {
24+
return BlankType
1925
}

pkg/section/commentline.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ func (c CommentLine) MatchSpecificity(spec *parse.GciImports) specificity.MatchS
1818
func (c CommentLine) String() string {
1919
return fmt.Sprintf("commentline(%s)", c.Comment)
2020
}
21+
22+
func (c CommentLine) Type() string {
23+
return "commentline"
24+
}

pkg/section/default.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/daixiang0/gci/pkg/specificity"
66
)
77

8-
const defaultName = "default"
8+
const DefaultType = "default"
99

1010
type Default struct{}
1111

@@ -14,5 +14,9 @@ func (d Default) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpeci
1414
}
1515

1616
func (d Default) String() string {
17-
return defaultName
17+
return DefaultType
18+
}
19+
20+
func (d Default) Type() string {
21+
return DefaultType
1822
}

pkg/section/dot.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
type Dot struct{}
99

10+
const DotType = "dot"
11+
1012
func (d Dot) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
1113
if spec.Name == "." {
1214
return specificity.NameMatch{}
@@ -15,5 +17,9 @@ func (d Dot) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecifici
1517
}
1618

1719
func (d Dot) String() string {
18-
return "dot"
20+
return DotType
21+
}
22+
23+
func (d Dot) Type() string {
24+
return DotType
1925
}

pkg/section/newline.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ func (n NewLine) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpeci
1616
func (n NewLine) String() string {
1717
return newLineName
1818
}
19+
20+
func (n NewLine) Type() string {
21+
return newLineName
22+
}

pkg/section/prefix.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Custom struct {
1212
Prefix string
1313
}
1414

15+
const CustomType = "custom"
16+
1517
func (c Custom) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
1618
if strings.HasPrefix(spec.Path, c.Prefix) {
1719
return specificity.Match{Length: len(c.Prefix)}
@@ -22,3 +24,7 @@ func (c Custom) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecif
2224
func (c Custom) String() string {
2325
return fmt.Sprintf("prefix(%s)", c.Prefix)
2426
}
27+
28+
func (c Custom) Type() string {
29+
return CustomType
30+
}

pkg/section/section.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type Section interface {
1212

1313
// String Implements the stringer interface
1414
String() string
15+
16+
// return section type
17+
Type() string
1518
}
1619

1720
type SectionList []Section

pkg/section/standard.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/daixiang0/gci/pkg/specificity"
66
)
77

8-
const StandardName = "standard"
8+
const StandardType = "standard"
99

1010
type Standard struct{}
1111

@@ -17,7 +17,11 @@ func (s Standard) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpec
1717
}
1818

1919
func (s Standard) String() string {
20-
return StandardName
20+
return StandardType
21+
}
22+
23+
func (s Standard) Type() string {
24+
return StandardType
2125
}
2226

2327
func isStandard(pkg string) bool {

0 commit comments

Comments
 (0)