Skip to content

Commit

Permalink
ResolvedLicenseInfo: Refactor sources to use original expression keys
Browse files Browse the repository at this point in the history
As the keys of the original expressions already contain the license
sources, the separate `sources` field is removed from the constructor.
The field itself is kept within `ResolvedLicense` because it is useful
in the API.

Signed-off-by: Marcel Bochtler <marcel.bochtler@bosch.io>
  • Loading branch information
MarcelBochtler committed Feb 25, 2021
1 parent 948a606 commit 72db653
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion evaluator/src/test/kotlin/PackageRuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PackageRuleTest : WordSpec() {
LicenseRule(
name = "test",
resolvedLicense = resolvedLicenseInfo[license]
?: ResolvedLicense(license, emptySet(), emptySet(), emptyMap(), emptySet()),
?: ResolvedLicense(license, emptySet(), emptyMap(), emptySet()),
licenseSource = licenseSource
)

Expand Down
8 changes: 1 addition & 7 deletions model/src/main/kotlin/licenses/LicenseInfoResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class LicenseInfoResolver(
// Handle concluded licenses.
concludedLicenses.forEach { license ->
license.builder().apply {
sources += LicenseSource.CONCLUDED

licenseInfo.concludedLicenseInfo.concludedLicense?.let {
originalExpressions[LicenseSource.CONCLUDED] = setOf(it)
}
Expand All @@ -89,8 +87,6 @@ class LicenseInfoResolver(
// Handle declared licenses.
declaredLicenses.forEach { license ->
license.builder().apply {
sources += LicenseSource.DECLARED

licenseInfo.declaredLicenseInfo.processed.spdxExpression?.let {
originalExpressions[LicenseSource.DECLARED] = setOf(it)
}
Expand All @@ -111,7 +107,6 @@ class LicenseInfoResolver(

resolvedLocations.keys.forEach { license ->
license.builder().apply {
sources += LicenseSource.DETECTED
resolvedLocations[license]?.let { locations.addAll(it) }

licenseInfo.detectedLicenseInfo.findings.forEach { findings ->
Expand Down Expand Up @@ -247,10 +242,9 @@ class LicenseInfoResolver(
}

private class ResolvedLicenseBuilder(val license: SpdxSingleLicenseExpression) {
val sources = mutableSetOf<LicenseSource>()
var originalDeclaredLicenses = mutableSetOf<String>()
var originalExpressions = mutableMapOf<LicenseSource, Set<SpdxExpression>>()
var locations = mutableSetOf<ResolvedLicenseLocation>()

fun build() = ResolvedLicense(license, sources, originalDeclaredLicenses, originalExpressions, locations)
fun build() = ResolvedLicense(license, originalDeclaredLicenses, originalExpressions, locations)
}
8 changes: 7 additions & 1 deletion model/src/main/kotlin/licenses/LicenseView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ class LicenseView(vararg licenseSources: Set<LicenseSource>) {

return resolvedLicenses.filter { it.license in remainingLicenses }.let { result ->
if (filterSources) {
result.map { it.copy(sources = remainingSources.getValue(it.license)) }
result.map { resolvedLicense ->
val remainingOriginalExpressions = resolvedLicense.originalExpressions.filterKeys {
it in remainingSources.getValue(resolvedLicense.license)
}

resolvedLicense.copy(originalExpressions = remainingOriginalExpressions)
}
} else {
result
}
Expand Down
10 changes: 5 additions & 5 deletions model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ data class ResolvedLicense(
*/
val license: SpdxSingleLicenseExpression,

/**
* The sources where this license was found.
*/
val sources: Set<LicenseSource>,

/**
* The list of original declared license that were [processed][DeclaredLicenseProcessor] to this [license], or an
* empty list, if this [license] was not modified during processing.
Expand All @@ -136,6 +131,11 @@ data class ResolvedLicense(
*/
val locations: Set<ResolvedLicenseLocation>
) {
/**
* The sources where this license was found.
*/
val sources = originalExpressions.keys

/**
* True, if this license was [detected][LicenseSource.DETECTED] and all [locations] have matching path excludes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ private fun List<ResolvedLicense>.merge(): ResolvedLicense {

return ResolvedLicense(
license = first().license,
sources = flatMapTo(mutableSetOf()) { it.sources },
originalDeclaredLicenses = flatMapTo(mutableSetOf()) { it.originalDeclaredLicenses },
originalExpressions = mergedOriginalExpressions,
locations = flatMapTo(mutableSetOf()) { it.locations }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,12 @@ class FreeMarkerTemplateProcessorTest : WordSpec({
val resolvedLicenses = listOf(
ResolvedLicense(
license = "MIT".toSpdx() as SpdxSingleLicenseExpression,
sources = emptySet(),
originalDeclaredLicenses = emptySet(),
originalExpressions = mapOf(LicenseSource.DECLARED to setOf("MIT".toSpdx())),
locations = emptySet()
),
ResolvedLicense(
license = "MIT".toSpdx() as SpdxSingleLicenseExpression,
sources = emptySet(),
originalDeclaredLicenses = emptySet(),
originalExpressions = mapOf(LicenseSource.DECLARED to setOf("GPL-2.0-only OR MIT".toSpdx())),
locations = emptySet()
Expand Down

0 comments on commit 72db653

Please sign in to comment.