-
Notifications
You must be signed in to change notification settings - Fork 2
/
package.go
154 lines (136 loc) · 4.19 KB
/
package.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
// nolint
package archunit
import (
"fmt"
"github.com/kcmvp/archunit/internal"
"github.com/samber/lo"
"regexp"
"strings"
)
type ArchPackage []*internal.Package
func AllPackages() ArchPackage {
return internal.Arch().Packages()
}
func Packages(paths ...string) (ArchPackage, error) {
patterns, err := ScopePattern(paths...)
return lo.Filter(AllPackages(), func(pkg *internal.Package, _ int) bool {
return lo.ContainsBy(patterns, func(pattern *regexp.Regexp) bool {
return pattern.MatchString(pkg.ID())
})
}), err
}
func (archPkg ArchPackage) ID() []string {
return lo.Map(archPkg, func(pkg *internal.Package, _ int) string {
return pkg.ID()
})
}
func (archPkg ArchPackage) Imports() []string {
var imports []string
lo.ForEach(archPkg, func(pkg *internal.Package, _ int) {
imports = append(imports, pkg.Imports()...)
})
return imports
}
func (archPkg ArchPackage) Skip(paths ...string) ArchPackage {
return lo.Filter(archPkg, func(pkg *internal.Package, _ int) bool {
return !lo.ContainsBy(paths, func(path string) bool {
return strings.HasSuffix(pkg.ID(), path)
})
})
}
func (archPkg ArchPackage) Types() Types {
var types Types
lo.ForEach(archPkg, func(pkg *internal.Package, _ int) {
types = append(types, pkg.Types()...)
})
return types
}
func (archPkg ArchPackage) Functions() Functions {
var functions Functions
lo.ForEach(archPkg, func(pkg *internal.Package, _ int) {
functions = append(functions, pkg.Functions()...)
})
return functions
}
func (archPkg ArchPackage) Files() FileSet {
var files []PackageFile
lo.ForEach(archPkg, func(pkg *internal.Package, _ int) {
files = append(files, PackageFile{A: pkg.ID(), B: pkg.Raw().GoFiles})
})
return files
}
func (archPkg ArchPackage) NameShouldBeSameAsFolder() error {
result := lo.FilterMap(archPkg, func(pkg *internal.Package, _ int) (string, bool) {
return pkg.ID(), !strings.HasSuffix(pkg.ID(), pkg.Name())
})
return lo.If(len(result) > 0, fmt.Errorf("package name and folder not the same: %v", archPkg.ID())).Else(nil)
}
func (archPkg ArchPackage) NameShould(pattern NamePattern, args ...string) error {
if pkg, ok := lo.Find(archPkg, func(pkg *internal.Package) bool {
return !pattern(pkg.Name(), lo.If(args == nil, "").ElseF(func() string {
return args[0]
}))
}); ok {
return fmt.Errorf("package %s's name is %s", pkg.ID(), pkg.Name())
}
return nil
}
func (archPkg ArchPackage) ShouldNotRefer(referred ...ArchPackage) error {
var ids []string
lo.ForEach(referred, func(ref ArchPackage, _ int) {
ids = append(ids, lo.Map(ref, func(pkg *internal.Package, _ int) string {
return pkg.ID()
})...)
})
if pkg, ok := lo.Find(archPkg, func(pkg *internal.Package) bool {
return lo.Some(pkg.Imports(), ids)
}); ok {
return fmt.Errorf("%s referrs %v", pkg.ID(), ids)
}
return nil
}
func (archPkg ArchPackage) ShouldNotReferPkgPaths(paths ...string) error {
pkgs, err := Packages(paths...)
if err != nil {
return err
}
return archPkg.ShouldNotRefer(pkgs)
}
func (archPkg ArchPackage) ShouldBeOnlyReferredByPackages(referrings ...ArchPackage) error {
var refIDs []string
lo.ForEach(referrings, func(ref ArchPackage, _ int) {
refIDs = append(refIDs, ref.Imports()...)
})
if pkg, ok := lo.Find(internal.Arch().Packages(), func(pkg *internal.Package) bool {
return lo.If(lo.Contains(archPkg, pkg), false).ElseF(func() bool {
return lo.Some(pkg.Imports(), archPkg.ID()) && !lo.Contains(refIDs, pkg.ID())
})
}); ok {
return fmt.Errorf("%s referrs %v", pkg.ID(), refIDs)
}
return nil
}
func (archPkg ArchPackage) ShouldOnlyReferPackages(referred ...ArchPackage) error {
var ids []string
lo.ForEach(referred, func(pkg ArchPackage, _ int) {
ids = append(ids, pkg.ID()...)
})
if d1, _ := lo.Difference(archPkg.Imports(), ids); len(d1) > 0 {
return fmt.Errorf("reference %v are out of scope %v", d1, ids)
}
return nil
}
func (archPkg ArchPackage) ShouldOnlyReferPkgPaths(paths ...string) error {
pkg, err := Packages(paths...)
if err != nil {
return err
}
return archPkg.ShouldOnlyReferPackages(pkg)
}
func (archPkg ArchPackage) ShouldBeOnlyReferredByPkgPaths(paths ...string) error {
pkg, err := Packages(paths...)
if err != nil {
return err
}
return archPkg.ShouldBeOnlyReferredByPackages(pkg)
}