Skip to content

Commit

Permalink
refactor(analyzer): Move Graph.toPackageReferenceForest()
Browse files Browse the repository at this point in the history
Prepare for making the graph operate on (name, version) tuples.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Oct 19, 2023
1 parent 723694d commit 07797c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
23 changes: 23 additions & 0 deletions analyzer/src/main/kotlin/managers/GoMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.Hash
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.PackageLinkage
import org.ossreviewtoolkit.model.PackageReference
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.ProjectAnalyzerResult
import org.ossreviewtoolkit.model.RemoteArtifact
Expand Down Expand Up @@ -337,6 +339,27 @@ class GoMod(
run(args = args, workingDir = workingDir, environment = environment)

private fun Map<String, ModuleInfo>.getMainModuleId(): Identifier = values.single { it.main }.toId()

/**
* Convert this [Graph] to a set of [PackageReference]s that spawn the dependency trees of the direct dependencies
* of the given [root] package. The graph must not contain any cycles, so [Graph.breakCycles] should be called
* before.
*/
private fun Graph.toPackageReferenceForest(root: Identifier): Set<PackageReference> {
fun getPackageReference(id: Identifier): PackageReference {
val dependencies = getDependencies(id).mapTo(mutableSetOf()) {
getPackageReference(it)
}

return PackageReference(
id = id,
linkage = PackageLinkage.PROJECT_STATIC,
dependencies = dependencies
)
}

return getDependencies(root).mapTo(mutableSetOf()) { getPackageReference(it) }
}
}

/** Separator string indicating that data of a new package follows in the output of the go mod why command. */
Expand Down
24 changes: 1 addition & 23 deletions analyzer/src/main/kotlin/managers/utils/Graph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import java.util.LinkedList
import org.apache.logging.log4j.kotlin.logger

import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.PackageLinkage
import org.ossreviewtoolkit.model.PackageReference

/**
* A class to represent a graph with dependencies. This representation is basically an adjacency list implemented by a
Expand Down Expand Up @@ -115,30 +113,10 @@ internal class Graph private constructor(private val nodeMap: MutableMap<Identif
return Graph(outgoingEdgesForNodes.mapValuesTo(mutableMapOf()) { it.value.toMutableSet() })
}

/**
* Convert this [Graph] to a set of [PackageReference]s that spawn the dependency trees of the direct dependencies
* of the given [root] package. The graph must not contain any cycles, so [breakCycles] should be called before.
*/
fun toPackageReferenceForest(root: Identifier): Set<PackageReference> {
fun getPackageReference(id: Identifier): PackageReference {
val dependencies = getDependencies(id).mapTo(mutableSetOf()) {
getPackageReference(it)
}

return PackageReference(
id = id,
linkage = PackageLinkage.PROJECT_STATIC,
dependencies = dependencies
)
}

return getDependencies(root).mapTo(mutableSetOf()) { getPackageReference(it) }
}

/**
* Return the identifiers of the direct dependencies of the package denoted by [id].
*/
private fun getDependencies(id: Identifier): Set<Identifier> = nodeMap[id].orEmpty()
fun getDependencies(id: Identifier): Set<Identifier> = nodeMap[id].orEmpty()
}

private enum class NodeColor { WHITE, GRAY, BLACK }

0 comments on commit 07797c3

Please sign in to comment.