Invert if statement to reduce nesting in archive parser

Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
This commit is contained in:
Dan Luhring 2021-01-27 10:34:52 -05:00
parent 0ccfee03f1
commit 1416e3cb7a
No known key found for this signature in database
GPG Key ID: 9CEE23D079426CEF

View File

@ -178,67 +178,69 @@ func (j *archiveParser) discoverPkgsFromPomProperties(parentPkg *pkg.Package) ([
return nil, fmt.Errorf("failed to parse pom.properties (%s): %w", j.virtualPath, err) return nil, fmt.Errorf("failed to parse pom.properties (%s): %w", j.virtualPath, err)
} }
if propsObj != nil { if propsObj == nil {
if propsObj.Version != "" && propsObj.ArtifactID != "" { continue
// TODO: if there is no parentPkg (no java manifest) one of these poms could be the parent. We should discover the right parent and attach the correct info accordingly to each discovered package }
// keep the artifact name within the virtual path if this package does not match the parent package if propsObj.Version != "" && propsObj.ArtifactID != "" {
vPathSuffix := "" // TODO: if there is no parentPkg (no java manifest) one of these poms could be the parent. We should discover the right parent and attach the correct info accordingly to each discovered package
if parentPkg != nil && !strings.HasPrefix(propsObj.ArtifactID, parentPkg.Name) {
vPathSuffix += ":" + propsObj.ArtifactID
}
virtualPath := j.virtualPath + vPathSuffix
// discovered props = new package // keep the artifact name within the virtual path if this package does not match the parent package
p := pkg.Package{ vPathSuffix := ""
Name: propsObj.ArtifactID, if parentPkg != nil && !strings.HasPrefix(propsObj.ArtifactID, parentPkg.Name) {
Version: propsObj.Version, vPathSuffix += ":" + propsObj.ArtifactID
Language: pkg.Java, }
Type: pkg.JavaPkg, virtualPath := j.virtualPath + vPathSuffix
MetadataType: pkg.JavaMetadataType,
Metadata: pkg.JavaMetadata{ // discovered props = new package
VirtualPath: virtualPath, p := pkg.Package{
PomProperties: propsObj, Name: propsObj.ArtifactID,
Parent: parentPkg, Version: propsObj.Version,
}, Language: pkg.Java,
Type: pkg.JavaPkg,
MetadataType: pkg.JavaMetadataType,
Metadata: pkg.JavaMetadata{
VirtualPath: virtualPath,
PomProperties: propsObj,
Parent: parentPkg,
},
}
pkgKey := uniquePkgKey(&p)
// the name/version pair matches...
matchesParentPkg := pkgKey == parentKey
if parentPkg != nil {
// the virtual path matches...
matchesParentPkg = matchesParentPkg || parentPkg.Metadata.(pkg.JavaMetadata).VirtualPath == virtualPath
// the pom artifactId has the parent name or vice versa
if propsObj.ArtifactID != "" {
matchesParentPkg = matchesParentPkg || strings.Contains(parentPkg.Name, propsObj.ArtifactID) || strings.Contains(propsObj.ArtifactID, parentPkg.Name)
} }
pkgKey := uniquePkgKey(&p) if matchesParentPkg {
// we've run across more information about our parent package, add this info to the parent package metadata
// the name/version pair matches... // the pom properties is typically a better source of information for name and version than the manifest
matchesParentPkg := pkgKey == parentKey if p.Name != parentPkg.Name {
parentPkg.Name = p.Name
if parentPkg != nil { }
// the virtual path matches... if p.Version != parentPkg.Version {
matchesParentPkg = matchesParentPkg || parentPkg.Metadata.(pkg.JavaMetadata).VirtualPath == virtualPath parentPkg.Version = p.Version
// the pom artifactId has the parent name or vice versa
if propsObj.ArtifactID != "" {
matchesParentPkg = matchesParentPkg || strings.Contains(parentPkg.Name, propsObj.ArtifactID) || strings.Contains(propsObj.ArtifactID, parentPkg.Name)
} }
if matchesParentPkg { parentMetadata, ok := parentPkg.Metadata.(pkg.JavaMetadata)
// we've run across more information about our parent package, add this info to the parent package metadata if ok {
// the pom properties is typically a better source of information for name and version than the manifest parentMetadata.PomProperties = propsObj
if p.Name != parentPkg.Name { parentPkg.Metadata = parentMetadata
parentPkg.Name = p.Name
}
if p.Version != parentPkg.Version {
parentPkg.Version = p.Version
}
parentMetadata, ok := parentPkg.Metadata.(pkg.JavaMetadata)
if ok {
parentMetadata.PomProperties = propsObj
parentPkg.Metadata = parentMetadata
}
} }
} }
}
if !matchesParentPkg && !j.discoveredPkgs.Contains(pkgKey) { if !matchesParentPkg && !j.discoveredPkgs.Contains(pkgKey) {
// only keep packages we haven't seen yet (and are not related to the parent package) // only keep packages we haven't seen yet (and are not related to the parent package)
pkgs = append(pkgs, p) pkgs = append(pkgs, p)
}
} }
} }
} }