-
Notifications
You must be signed in to change notification settings - Fork 2
/
layer.go
222 lines (196 loc) · 5.56 KB
/
layer.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// nolint
package archunit
import (
"fmt"
"github.com/kcmvp/archunit/internal"
"github.com/samber/lo"
"path/filepath"
"regexp"
"strings"
)
type Visible int
const (
Public Visible = iota
Private
)
type NamePattern func(name, arg string) bool
func BeLowerCase(name, _ string) bool {
return strings.ToLower(name) == name
}
func BeUpperCase(name, _ string) bool {
return strings.ToUpper(name) == name
}
func HavePrefix(name, prefix string) bool {
return strings.HasPrefix(name, prefix)
}
func HaveSuffix(name, suffix string) bool {
return strings.HasSuffix(name, suffix)
}
type ArchLayer []*internal.Package
func SourceNameShould(pattern NamePattern, args ...string) error {
if file, ok := lo.Find(internal.Arch().GoFiles(), func(file string) bool {
return !pattern(filepath.Base(file), lo.If(args == nil, "").ElseF(func() string {
return args[0]
}))
}); ok {
return fmt.Errorf("file %s's name breaks the rule", file)
}
return nil
}
func ConstantsShouldBeDefinedInOneFileByPackage() error {
for _, pkg := range internal.Arch().Packages() {
files := pkg.ConstantFiles()
if len(files) > 1 {
return fmt.Errorf("package %s constants are definied in files %v", pkg.ID(), files)
}
}
return nil
}
func Layer(pkgPaths ...string) (ArchLayer, error) {
patterns, err := ScopePattern(pkgPaths...)
if err != nil {
return nil, err
}
return lo.Filter(internal.Arch().Packages(), func(pkg *internal.Package, _ int) bool {
return lo.ContainsBy(patterns, func(pattern *regexp.Regexp) bool {
return pattern.MatchString(pkg.ID())
})
}), nil
}
func (layer ArchLayer) Name() string {
pkgs := layer.packages()
idx := 0
left := lo.DropWhile(pkgs, func(item string) bool {
idx++
if idx == len(pkgs) {
return false
}
return lo.ContainsBy(pkgs[idx:], func(l string) bool {
return strings.HasPrefix(l, item)
})
})
return fmt.Sprintf("%v", left)
}
func (layer ArchLayer) Exclude(pkgPaths ...string) (ArchLayer, error) {
patterns, err := ScopePattern(pkgPaths...)
if err != nil {
return nil, err
}
return lo.Filter(layer, func(pkg *internal.Package, _ int) bool {
return lo.NoneBy(patterns, func(pattern *regexp.Regexp) bool {
return pattern.MatchString(pkg.ID())
})
}), nil
}
func (layer ArchLayer) Sub(name string, paths ...string) (ArchLayer, error) {
patterns, err := ScopePattern(paths...)
if err != nil {
return nil, err
}
return lo.Filter(layer, func(pkg *internal.Package, _ int) bool {
return lo.SomeBy(patterns, func(pattern *regexp.Regexp) bool {
return pattern.MatchString(pkg.ID())
})
}), nil
}
func (layer ArchLayer) Packages() ArchPackage {
return ArchPackage(layer)
}
func (layer ArchLayer) Functions() Functions {
var fs Functions
lo.ForEach(layer, func(pkg *internal.Package, _ int) {
fs = append(fs, pkg.Functions()...)
})
return fs
}
func (layer ArchLayer) packages() []string {
return lo.Map(layer, func(item *internal.Package, _ int) string {
return item.ID()
})
}
func (layer ArchLayer) Imports() []string {
var imports []string
for _, pkg := range layer {
imports = append(imports, pkg.Imports()...)
}
return imports
}
func (layer ArchLayer) Types() Types {
var ts Types
lo.ForEach(layer, func(pkg *internal.Package, _ int) {
ts = append(ts, pkg.Types()...)
})
return ts
}
func (layer ArchLayer) Files() FileSet {
return lo.Map(layer, func(pkg *internal.Package, _ int) PackageFile {
return PackageFile{A: pkg.ID(), B: pkg.GoFiles()}
})
}
func (layer ArchLayer) FilesInPackages(paths ...string) (FileSet, error) {
patterns, err := ScopePattern(paths...)
if err != nil {
return nil, err
}
return lo.FilterMap(layer, func(pkg *internal.Package, _ int) (PackageFile, bool) {
if lo.SomeBy(patterns, func(reg *regexp.Regexp) bool {
return reg.MatchString(pkg.ID())
}) {
return PackageFile{A: pkg.ID(), B: pkg.GoFiles()}, true
}
return PackageFile{}, false
}), nil
}
func (layer ArchLayer) ShouldNotReferLayers(layers ...ArchLayer) error {
var packages []string
for _, l := range layers {
packages = append(packages, l.packages()...)
}
path, ok := lo.Find(layer.Imports(), func(ref string) bool {
return lo.Contains(packages, ref)
})
return lo.If(ok, fmt.Errorf("%s refers %s", layer.Name(), path)).Else(nil)
}
func (layer ArchLayer) ShouldNotReferPackages(paths ...string) error {
l, err := Layer(paths...)
if err != nil {
return err
}
return layer.ShouldNotReferLayers(l)
}
func (layer ArchLayer) ShouldOnlyReferLayers(layers ...ArchLayer) error {
var pkgs []string
for _, l := range layers {
pkgs = append(pkgs, l.packages()...)
}
d1, _ := lo.Difference(layer.Imports(), pkgs)
return lo.If(len(d1) > 0, fmt.Errorf("%v are out of scope %v", d1, pkgs)).Else(nil)
}
func (layer ArchLayer) ShouldOnlyReferPackages(paths ...string) error {
l, err := Layer(paths...)
if err != nil {
return err
}
return layer.ShouldOnlyReferLayers(l)
}
func (layer ArchLayer) ShouldBeOnlyReferredByLayers(layers ...ArchLayer) error {
return ArchPackage(layer).ShouldBeOnlyReferredByPackages(lo.Map(layers, func(item ArchLayer, _ int) ArchPackage {
return ArchPackage(item)
})...)
}
func (layer ArchLayer) ShouldBeOnlyReferredByPackages(paths ...string) error {
l, err := Layer(paths...)
if err != nil {
return err
}
return layer.ShouldBeOnlyReferredByLayers(l)
}
func (layer ArchLayer) DepthShouldLessThan(depth int) error {
pkg := lo.MaxBy(layer, func(a *internal.Package, b *internal.Package) bool {
return len(strings.Split(a.ID(), "/")) > len(strings.Split(a.ID(), "/"))
})
if acc := len(strings.Split(pkg.ID(), "/")); acc >= depth {
return fmt.Errorf("%s max depth is %d", pkg.ID(), acc)
}
return nil
}