/
import_list_builder.go
137 lines (119 loc) · 3.16 KB
/
import_list_builder.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package helper
import (
"crypto/sha1"
"sort"
"strings"
"github.com/bitwormhole/starter-configen/configen3/data/model"
"github.com/bitwormhole/starter/util"
)
type importListItem *model.GoImport
// ImportListBuilder 是一个 go-import 列表构建器
type ImportListBuilder struct {
items map[string]importListItem
}
func (inst *ImportListBuilder) getTable() map[string]importListItem {
table := inst.items
if table == nil {
table = make(map[string]importListItem)
inst.items = table
}
return table
}
func (inst *ImportListBuilder) clone(item importListItem) importListItem {
if item == nil {
return nil
}
o2 := &model.GoImport{}
*o2 = *item
return o2
}
// makeAlias 计算 model.GoImport 的别名
func (inst *ImportListBuilder) makeAliasWithHash(i *model.GoImport) string {
path := i.Path
sum := sha1.Sum([]byte(path))
alias := inst.makeAliasWithoutHash(i)
return alias + "0x" + util.StringifyBytes(sum[0:3])
}
// makeAliasDefault 计算 model.GoImport 的默认别名
func (inst *ImportListBuilder) makeAliasWithoutHash(i *model.GoImport) string {
path := i.Path
index := strings.LastIndex(path, "/")
suffix := path
builder := strings.Builder{}
if index > 0 {
suffix = path[index+1:]
}
array := []rune(suffix)
for _, ch := range array {
if ch == '-' {
ch = '_'
}
builder.WriteRune(ch)
}
return builder.String()
}
// Create 创建go-import 列表
func (inst *ImportListBuilder) Create() []*model.GoImport {
table := inst.getTable()
keys := make([]string, 0)
for key := range table {
keys = append(keys, key)
}
sort.Strings(keys)
list := make([]*model.GoImport, 0)
for _, key := range keys {
item := table[key]
list = append(list, item)
}
return list
}
// AddComplexType 添加一条go-import 记录
func (inst *ImportListBuilder) AddComplexType(t *model.ComplexType, disableHash bool) {
if t == nil {
return
}
inst.AddSimpleType(t.KeyType, disableHash)
inst.AddSimpleType(t.ValueType, disableHash)
}
// AddSimpleType 添加一条go-import 记录
func (inst *ImportListBuilder) AddSimpleType(t *model.SimpleType, disableHash bool) {
if t == nil {
return
}
res := inst.AddPackagePath(t.PackageName, disableHash)
if res == nil {
return
}
t.PackageAlias = res.Alias
}
// AddPackagePath 添加一条go-import 记录
func (inst *ImportListBuilder) AddPackagePath(pkgPath string, disableHash bool) *model.GoImport {
adding := &model.GoImport{}
adding.Path = pkgPath
adding.NoHash = disableHash
return inst.Add(adding)
}
// Add 添加一条go-import 记录
func (inst *ImportListBuilder) Add(adding *model.GoImport) *model.GoImport {
if adding == nil {
return nil
}
adding = inst.clone(adding)
table := inst.getTable()
path := adding.Path
older := table[path]
if older == nil {
aliasWithHash := inst.makeAliasWithHash(adding)
alias := inst.makeAliasWithoutHash(adding)
if adding.NoHash {
adding.Alias = alias
adding.DefaultAlias = alias
} else {
adding.Alias = aliasWithHash
adding.DefaultAlias = alias
}
older = adding
table[path] = adding
}
return inst.clone(older)
}