From 03cab6dec87a4090bbc0895a0e5f386d9f29dd8a Mon Sep 17 00:00:00 2001 From: Marcel Bochtler Date: Thu, 18 Feb 2021 14:39:40 +0100 Subject: [PATCH] ResolvedLicenseInfo: Refactor sources to use original expression keys 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 --- evaluator/src/test/kotlin/PackageRuleTest.kt | 2 +- model/src/main/kotlin/licenses/LicenseInfoResolver.kt | 8 +------- model/src/main/kotlin/licenses/LicenseView.kt | 9 ++++++++- model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt | 10 +++++----- .../main/kotlin/utils/FreemarkerTemplateProcessor.kt | 1 - .../kotlin/utils/FreeMarkerTemplateProcessorTest.kt | 2 -- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/evaluator/src/test/kotlin/PackageRuleTest.kt b/evaluator/src/test/kotlin/PackageRuleTest.kt index b18507f0b1be6..2ed0fe2f3bad8 100644 --- a/evaluator/src/test/kotlin/PackageRuleTest.kt +++ b/evaluator/src/test/kotlin/PackageRuleTest.kt @@ -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 ) diff --git a/model/src/main/kotlin/licenses/LicenseInfoResolver.kt b/model/src/main/kotlin/licenses/LicenseInfoResolver.kt index 05846bbcc2772..554324e130b5c 100644 --- a/model/src/main/kotlin/licenses/LicenseInfoResolver.kt +++ b/model/src/main/kotlin/licenses/LicenseInfoResolver.kt @@ -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) } @@ -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) } @@ -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 -> @@ -248,10 +243,9 @@ class LicenseInfoResolver( } private class ResolvedLicenseBuilder(val license: SpdxSingleLicenseExpression) { - val sources = mutableSetOf() var originalDeclaredLicenses = mutableSetOf() var originalExpressions = mutableMapOf>() var locations = mutableSetOf() - fun build() = ResolvedLicense(license, sources, originalDeclaredLicenses, originalExpressions, locations) + fun build() = ResolvedLicense(license, originalDeclaredLicenses, originalExpressions, locations) } diff --git a/model/src/main/kotlin/licenses/LicenseView.kt b/model/src/main/kotlin/licenses/LicenseView.kt index 8d4636c2bdd4d..f7babf7223022 100644 --- a/model/src/main/kotlin/licenses/LicenseView.kt +++ b/model/src/main/kotlin/licenses/LicenseView.kt @@ -130,7 +130,14 @@ class LicenseView(vararg licenseSources: Set) { 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 } diff --git a/model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt b/model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt index c194b44d88ace..5be17985827a4 100644 --- a/model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt +++ b/model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt @@ -115,11 +115,6 @@ data class ResolvedLicense( */ val license: SpdxSingleLicenseExpression, - /** - * The sources where this license was found. - */ - val sources: Set, - /** * 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. @@ -136,6 +131,11 @@ data class ResolvedLicense( */ val locations: Set ) { + /** + * 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. */ diff --git a/reporter/src/main/kotlin/utils/FreemarkerTemplateProcessor.kt b/reporter/src/main/kotlin/utils/FreemarkerTemplateProcessor.kt index 07bc20d21457a..abc04066c25c0 100644 --- a/reporter/src/main/kotlin/utils/FreemarkerTemplateProcessor.kt +++ b/reporter/src/main/kotlin/utils/FreemarkerTemplateProcessor.kt @@ -284,7 +284,6 @@ private fun List.merge(): ResolvedLicense { return ResolvedLicense( license = first().license, - sources = flatMapTo(mutableSetOf()) { it.sources }, originalDeclaredLicenses = flatMapTo(mutableSetOf()) { it.originalDeclaredLicenses }, originalExpressions = mergedOriginalExpressions, locations = flatMapTo(mutableSetOf()) { it.locations } diff --git a/reporter/src/test/kotlin/utils/FreeMarkerTemplateProcessorTest.kt b/reporter/src/test/kotlin/utils/FreeMarkerTemplateProcessorTest.kt index b57dfec12b460..12f3ecb7026cb 100644 --- a/reporter/src/test/kotlin/utils/FreeMarkerTemplateProcessorTest.kt +++ b/reporter/src/test/kotlin/utils/FreeMarkerTemplateProcessorTest.kt @@ -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()