-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_import.go
92 lines (75 loc) · 1.84 KB
/
block_import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package codegen
import (
"sort"
"strings"
)
type importLine struct {
alias string
path string
}
type importsBlock struct {
lines map[string]*importLine
}
// Imports creates a new imports block
func (f *File) Imports(imports ...*importLine) *importsBlock {
for _, value := range imports {
f.imports.addImport(value)
}
return f.imports
}
// AddImport adds a new import statement to the import block
func (i *importsBlock) AddImport(path string) *importLine {
return i.addImport(Import(path))
}
// AddImportAlias adds a new import statement with its package alias to the import block
func (i *importsBlock) AddImportAlias(path, alias string) *importLine {
return i.addImport(ImportAlias(path, alias))
}
// Import creates a new import statemet without an alias
func Import(path string) *importLine {
return &importLine{path: path}
}
// ImportAlias creates a new import statement with an alias
func ImportAlias(path, alias string) *importLine {
return &importLine{path: path, alias: alias}
}
func (i *importsBlock) addImport(line *importLine) *importLine {
if existing, ok := i.lines[line.path]; ok {
return existing
} else {
i.lines[line.path] = line
return line
}
}
func newImportsBlock() *importsBlock {
return &importsBlock{
lines: make(map[string]*importLine),
}
}
func (i *importsBlock) write(sb *strings.Builder) {
if count := len(i.lines); count == 0 {
return
} else {
keys := make([]string, 0, count)
for k := range i.lines {
keys = append(keys, k)
}
if count == 1 {
writeF(sb, "import ")
i.lines[keys[0]].wr(sb)
} else {
sort.Strings(keys)
writeNewLine(sb, "import (")
for _, key := range keys {
i.lines[key].wr(sb)
}
writeByteNewLine(sb, ')')
}
}
}
func (i *importLine) wr(sb *strings.Builder) {
if len(i.alias) != 0 {
writeF(sb, "%s ", i.alias)
}
writeNewLineF(sb, "\"%s\"", i.path)
}