Skip to content

Commit 5e4e259

Browse files
committed
Fix complex dependency checks
1 parent 0b61e23 commit 5e4e259

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

deps/deps.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,10 @@ func checkPkg(
5656
} else {
5757
strictRelImp = p.PkgPath
5858
}
59-
6059
unqImp := pkgs.UniquePackageName(relImp, strictRelImp)
61-
pl, dollars := cfg.AllowOnlyIn.MatchingList(strictRelImp)
62-
if pl == nil {
63-
pl, dollars = cfg.AllowOnlyIn.MatchingList(relImp)
64-
}
65-
if pl != nil {
66-
if _, full := isPackageInList(pl, dollars, relPkg, strictRelPkg); !full {
60+
61+
if hasKey, hasValue := cfg.AllowOnlyIn.HasKeyValue(relImp, strictRelImp, relPkg, strictRelPkg); hasKey {
62+
if !hasValue {
6763
errs = append(errs, fmt.Errorf(
6864
"package '%s' isn't allowed to import package '%s' (because of allowOnlyIn)",
6965
unqPkg, unqImp))
@@ -73,14 +69,7 @@ func checkPkg(
7369

7470
if internal {
7571
// check in allow first:
76-
pl = nil
77-
if strictRelPkg != "" {
78-
pl, dollars = cfg.AllowAdditionally.MatchingList(strictRelPkg)
79-
}
80-
if pl == nil {
81-
pl, dollars = cfg.AllowAdditionally.MatchingList(relPkg)
82-
}
83-
if _, full := isPackageInList(pl, dollars, relImp, strictRelImp); full {
72+
if hasKey, hasValue := cfg.AllowAdditionally.HasKeyValue(relPkg, strictRelPkg, relImp, strictRelImp); hasKey && hasValue {
8473
continue // this import is fine
8574
}
8675

x/config/config.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,33 @@ func (pm *PatternMap) String() string {
4646
return s[:len(s)-3]
4747
}
4848

49-
// MatchingList returns the PatternList and submatches in the key if any key of
50-
// this pattern map matches the given string and nil otherwise.
51-
func (pm *PatternMap) MatchingList(s string) (data.PatternList, []string) {
49+
// HasKeyValue checks if this pattern map contains the given key value pair.
50+
// The strict versions are checked first
51+
// (1. strictKey+strictValue, 2. strictKey+value, 3. key+strictValue, 4. key+value).
52+
func (pm *PatternMap) HasKeyValue(key, strictKey, value, strictValue string) (hasKey, hasValue bool) {
5253
if pm == nil {
53-
return nil, nil
54+
return false, false
5455
}
55-
for _, group := range *pm {
56-
if m := group.left.Regexp.FindStringSubmatch(s); len(m) > 0 {
57-
return group.right, m[1:]
56+
57+
for _, k := range []string{strictKey, key} {
58+
if k == "" {
59+
continue
60+
}
61+
for _, group := range *pm {
62+
if m := group.left.Regexp.FindStringSubmatch(k); len(m) > 0 {
63+
dollars := m[1:]
64+
65+
if _, full := group.right.MatchString(strictValue, dollars); strictValue != "" && full {
66+
return true, true
67+
}
68+
if _, full := group.right.MatchString(value, dollars); value != "" && full {
69+
return true, true
70+
}
71+
hasKey = true
72+
}
5873
}
5974
}
60-
return nil, nil
75+
return hasKey, false
6176
}
6277

6378
// Config contains the parsed configuration.

0 commit comments

Comments
 (0)