Skip to content

Commit 10dd6a2

Browse files
committed
Move library.properties depends field parsing code to utility function
This will allow the code to be shared by additional rule functions which need this data.
1 parent 2caada6 commit 10dd6a2

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

Diff for: internal/rule/rulefunction/library.go

+50-24
Original file line numberDiff line numberDiff line change
@@ -1201,37 +1201,21 @@ func LibraryPropertiesDependsFieldNotInIndex() (result ruleresult.Type, output s
12011201
return ruleresult.Skip, "Field not present"
12021202
}
12031203

1204-
dependsList := commaSeparatedToList(depends)
1204+
dependencies := libDependencies(depends)
12051205

1206-
var dependencyRegexp = regexp.MustCompile("^([^()]+?) *(?:\\((.+)\\))?$")
12071206
dependsNotInIndex := []string{}
1208-
for _, depend := range dependsList {
1209-
// Process raw depend string into a dependency object
1210-
if depend == "" {
1207+
for _, dependency := range dependencies {
1208+
if dependency.parseConstraintErr != nil {
12111209
// This is the responsibility of LibraryPropertiesDependsFieldInvalidFormat()
12121210
continue
12131211
}
1214-
dependencyData := dependencyRegexp.FindAllStringSubmatch(depend, -1)
1215-
if dependencyData == nil {
1216-
// This is the responsibility of LibraryPropertiesDependsFieldInvalidFormat()
1217-
continue
1218-
}
1219-
dependencyConstraint, err := semver.ParseConstraint(dependencyData[0][2])
1220-
if err != nil {
1221-
// This is the responsibility of LibraryPropertiesDependsFieldInvalidFormat()
1222-
continue
1223-
}
1224-
var dependency semver.Dependency = &librariesindex.Dependency{
1225-
Name: dependencyData[0][1],
1226-
VersionConstraint: dependencyConstraint,
1227-
}
12281212

1229-
logrus.Tracef("Checking if dependency %s is in index.", depend)
1213+
logrus.Tracef("Checking if dependency %s is in index.", dependency.depend)
12301214
// Get all releases of the dependency
1231-
library := projectdata.LibraryManagerIndex().Index.FindIndexedLibrary(&libraries.Library{Name: dependency.GetName()})
1215+
library := projectdata.LibraryManagerIndex().Index.FindIndexedLibrary(&libraries.Library{Name: dependency.data.GetName()})
12321216
if library == nil {
12331217
logrus.Tracef("Dependency is not in the index.")
1234-
dependsNotInIndex = append(dependsNotInIndex, depend)
1218+
dependsNotInIndex = append(dependsNotInIndex, dependency.depend)
12351219
continue
12361220
}
12371221
// Convert the dependency's libraries.Library object to a semver.Releases object
@@ -1240,10 +1224,10 @@ func LibraryPropertiesDependsFieldNotInIndex() (result ruleresult.Type, output s
12401224
releases = append(releases, release)
12411225
}
12421226
// Filter the dependency's releases according to the dependency's constraint
1243-
dependencyReleases := releases.FilterBy(dependency)
1227+
dependencyReleases := releases.FilterBy(&dependency.data)
12441228
if len(dependencyReleases) == 0 {
12451229
logrus.Tracef("No releases match dependency's constraint.")
1246-
dependsNotInIndex = append(dependsNotInIndex, depend)
1230+
dependsNotInIndex = append(dependsNotInIndex, dependency.depend)
12471231
continue
12481232
}
12491233
}
@@ -1552,3 +1536,45 @@ func commaSeparatedToList(commaSeparated string) []string {
15521536

15531537
return list
15541538
}
1539+
1540+
// libDependency is a library dependency
1541+
type libDependency struct {
1542+
depend string // Raw element from depends field.
1543+
data librariesindex.Dependency // Dependency object.
1544+
parseConstraintErr error // Error produced by parsing the version constraint.
1545+
}
1546+
1547+
var dependRegexp = regexp.MustCompile("^([^()]+?) *(?:\\((.+)\\))?$")
1548+
1549+
// libDependencies parses the library.properties `depends` field contents and returns an array of libDependency objects
1550+
func libDependencies(depends string) []libDependency {
1551+
dependList := commaSeparatedToList(depends)
1552+
1553+
dependencies := []libDependency{}
1554+
for _, depend := range dependList {
1555+
// Process raw depend string into a dependency object
1556+
if depend == "" {
1557+
// This function is only concerned with the parseable depend elements.
1558+
// `depends` field data format is checked separately.
1559+
continue
1560+
}
1561+
dependencyData := dependRegexp.FindAllStringSubmatch(depend, -1)
1562+
if dependencyData == nil {
1563+
// This function is only concerned with the parseable depend elements.
1564+
// `depends` field data format is checked separately.
1565+
continue
1566+
}
1567+
dependencyConstraint, err := semver.ParseConstraint(dependencyData[0][2])
1568+
dependencies = append(dependencies, libDependency{
1569+
depend: depend,
1570+
data: librariesindex.Dependency{
1571+
Name: dependencyData[0][1],
1572+
VersionConstraint: dependencyConstraint,
1573+
},
1574+
parseConstraintErr: err,
1575+
},
1576+
)
1577+
}
1578+
1579+
return dependencies
1580+
}

0 commit comments

Comments
 (0)