feat: support multi company, split using ,

main
sudo pacman -Syu 2023-01-22 11:03:54 +07:00
parent c518272fb9
commit e60f1202e6
2 changed files with 26 additions and 8 deletions

View File

@ -52,8 +52,8 @@ type Formatter struct {
stdPackages map[string]struct{} stdPackages map[string]struct{}
moduleNames map[string]string moduleNames map[string]string
formattedPaths map[string]struct{} formattedPaths map[string]struct{}
companyPrefixes map[string]struct{}
eg errgroup.Group eg errgroup.Group
companyPrefix string
muModuleNames sync.RWMutex muModuleNames sync.RWMutex
muFormattedPaths sync.RWMutex muFormattedPaths sync.RWMutex
isList bool isList bool
@ -316,7 +316,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
result := make(map[string][]*dst.ImportSpec) result := make(map[string][]*dst.ImportSpec)
result[stdImport] = make([]*dst.ImportSpec, 0, 8) result[stdImport] = make([]*dst.ImportSpec, 0, 8)
result[thirdPartyImport] = make([]*dst.ImportSpec, 0, 8) result[thirdPartyImport] = make([]*dst.ImportSpec, 0, 8)
if ft.companyPrefix != "" { if len(ft.companyPrefixes) != 0 {
result[companyImport] = make([]*dst.ImportSpec, 0, 8) result[companyImport] = make([]*dst.ImportSpec, 0, 8)
} }
result[localImport] = make([]*dst.ImportSpec, 0, 8) result[localImport] = make([]*dst.ImportSpec, 0, 8)
@ -335,10 +335,19 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
continue continue
} }
if ft.companyPrefix != "" && if len(ft.companyPrefixes) != 0 {
strings.HasPrefix(importPath, ft.companyPrefix) { existImport := false
result[companyImport] = append(result[companyImport], importSpec) for companyPrefix := range ft.companyPrefixes {
continue if strings.HasPrefix(importPath, companyPrefix) {
result[companyImport] = append(result[companyImport], importSpec)
existImport = true
break
}
}
if existImport {
continue
}
} }
result[thirdPartyImport] = append(result[thirdPartyImport], importSpec) result[thirdPartyImport] = append(result[thirdPartyImport], importSpec)
@ -346,7 +355,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
ft.logDSTImportSpecs("groupDSTImportSpecs: stdImport", result[stdImport]) ft.logDSTImportSpecs("groupDSTImportSpecs: stdImport", result[stdImport])
ft.logDSTImportSpecs("groupDSTImportSpecs: thirdPartyImport", result[thirdPartyImport]) ft.logDSTImportSpecs("groupDSTImportSpecs: thirdPartyImport", result[thirdPartyImport])
if ft.companyPrefix != "" { if len(ft.companyPrefixes) != 0 {
ft.logDSTImportSpecs("groupDSTImportSpecs: companyImport", result[companyImport]) ft.logDSTImportSpecs("groupDSTImportSpecs: companyImport", result[companyImport])
} }
ft.logDSTImportSpecs("groupDSTImportSpecs: localImport", result[localImport]) ft.logDSTImportSpecs("groupDSTImportSpecs: localImport", result[localImport])

View File

@ -1,5 +1,7 @@
package imports package imports
import "strings"
type FormatterOptionFn func(*Formatter) type FormatterOptionFn func(*Formatter)
func FormatterWithList(isList bool) FormatterOptionFn { func FormatterWithList(isList bool) FormatterOptionFn {
@ -28,6 +30,13 @@ func FormatterWithVerbose(isVerbose bool) FormatterOptionFn {
func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn { func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn {
return func(ft *Formatter) { return func(ft *Formatter) {
ft.companyPrefix = companyPrefix ft.companyPrefixes = make(map[string]struct{})
for _, prefix := range strings.Split(companyPrefix, ",") {
prefix = strings.TrimSpace(prefix)
if prefix == "" {
continue
}
ft.companyPrefixes[prefix] = struct{}{}
}
} }
} }