@@ -1201,37 +1201,21 @@ func LibraryPropertiesDependsFieldNotInIndex() (result ruleresult.Type, output s
1201
1201
return ruleresult .Skip , "Field not present"
1202
1202
}
1203
1203
1204
- dependsList := commaSeparatedToList (depends )
1204
+ dependencies := libDependencies (depends )
1205
1205
1206
- var dependencyRegexp = regexp .MustCompile ("^([^()]+?) *(?:\\ ((.+)\\ ))?$" )
1207
1206
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 {
1211
1209
// This is the responsibility of LibraryPropertiesDependsFieldInvalidFormat()
1212
1210
continue
1213
1211
}
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
- }
1228
1212
1229
- logrus .Tracef ("Checking if dependency %s is in index." , depend )
1213
+ logrus .Tracef ("Checking if dependency %s is in index." , dependency . depend )
1230
1214
// 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 ()})
1232
1216
if library == nil {
1233
1217
logrus .Tracef ("Dependency is not in the index." )
1234
- dependsNotInIndex = append (dependsNotInIndex , depend )
1218
+ dependsNotInIndex = append (dependsNotInIndex , dependency . depend )
1235
1219
continue
1236
1220
}
1237
1221
// Convert the dependency's libraries.Library object to a semver.Releases object
@@ -1240,10 +1224,10 @@ func LibraryPropertiesDependsFieldNotInIndex() (result ruleresult.Type, output s
1240
1224
releases = append (releases , release )
1241
1225
}
1242
1226
// Filter the dependency's releases according to the dependency's constraint
1243
- dependencyReleases := releases .FilterBy (dependency )
1227
+ dependencyReleases := releases .FilterBy (& dependency . data )
1244
1228
if len (dependencyReleases ) == 0 {
1245
1229
logrus .Tracef ("No releases match dependency's constraint." )
1246
- dependsNotInIndex = append (dependsNotInIndex , depend )
1230
+ dependsNotInIndex = append (dependsNotInIndex , dependency . depend )
1247
1231
continue
1248
1232
}
1249
1233
}
@@ -1552,3 +1536,45 @@ func commaSeparatedToList(commaSeparated string) []string {
1552
1536
1553
1537
return list
1554
1538
}
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