Skip to content

Commit cf145cf

Browse files
committedFeb 19, 2025
refactor(spdx-utils): Map deprecated and simple licenses separately
The previous approach was intertwining the mapping of deprecated and simple licenses in an "all or nothing" manner. However, conservative users of the API might only want to rely on unambiguous / uncontroversial mappings. Allow such users to only map deprecated licenses by mapping them separately from simple licenses. Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
1 parent 431abea commit cf145cf

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed
 

‎utils/ort/src/test/kotlin/DeclaredLicenseProcessorTest.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class DeclaredLicenseProcessorTest : StringSpec() {
4848
/**
4949
* A collection of declared license strings found in open source packages.
5050
*/
51-
private val declaredLicenses = SpdxSimpleLicenseMapping.mapping.keys + SpdxDeclaredLicenseMapping.mapping.keys
51+
private val declaredLicenses = SpdxDeclaredLicenseMapping.mapping.keys +
52+
SpdxSimpleLicenseMapping.simpleExpressionMapping.keys +
53+
SpdxSimpleLicenseMapping.deprecatedExpressionMapping.keys
5254

5355
init {
5456
"Declared licenses can be processed" {

‎utils/spdx/src/main/kotlin/SpdxSimpleLicenseMapping.kt

+15-14
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,29 @@ object SpdxSimpleLicenseMapping {
3838
/**
3939
* The map of simple license names associated with their corresponding [SPDX expression][SpdxLicenseIdExpression].
4040
*/
41-
internal val simpleExpressionMapping = simpleLicenseMapping.mapValues { (_, v) -> v.toExpression() }
42-
.toSortedMap(String.CASE_INSENSITIVE_ORDER)
41+
val simpleExpressionMapping by lazy {
42+
simpleLicenseMapping.mapValuesTo(sortedMapOf(String.CASE_INSENSITIVE_ORDER)) { (_, v) -> v.toExpression() }
43+
}
4344

4445
/**
4546
* The map of deprecated SPDX license IDs associated with their current [SPDX expression]
4647
* [SpdxSingleLicenseExpression].
4748
*/
48-
private val deprecatedExpressionMapping by lazy {
49+
val deprecatedExpressionMapping by lazy {
4950
val resource = checkNotNull(javaClass.getResource("/deprecated-license-mapping.yml"))
50-
yamlMapper.readValue<Map<String, SpdxSingleLicenseExpression>>(resource)
51+
val mapping = yamlMapper.readValue<Map<String, SpdxSingleLicenseExpression>>(resource)
52+
mapping.toSortedMap(String.CASE_INSENSITIVE_ORDER)
5153
}
5254

5355
/**
54-
* The map of varied SPDX license ids associated with their corresponding SPDX expression.
56+
* Return the [SpdxSingleLicenseExpression] the [license] maps to, or null if there is no corresponding expression.
57+
* If [mapDeprecated] is true, licenses marked as deprecated in the SPDX standard are mapped to their corresponding
58+
* current expression. If [mapSimple] is true, licenses that are commonly known abbreviations or aliases are mapped
59+
* to their corresponding official expression.
5560
*/
56-
val mapping = (simpleExpressionMapping + deprecatedExpressionMapping).toSortedMap(String.CASE_INSENSITIVE_ORDER)
57-
58-
/**
59-
* Return the [SpdxExpression] the [license] id maps to, or null if there is no corresponding expression. If
60-
* [mapDeprecated] is true, license ids marked as deprecated in the SPDX standard are mapped to their
61-
* corresponding current expression, otherwise they are mapped to their corresponding deprecated expression.
62-
*/
63-
fun map(license: String, mapDeprecated: Boolean = true) =
64-
(if (mapDeprecated) mapping else simpleExpressionMapping)[license] ?: SpdxLicense.forId(license)?.toExpression()
61+
fun map(license: String, mapDeprecated: Boolean = true, mapSimple: Boolean = true): SpdxSingleLicenseExpression? {
62+
if (mapDeprecated) deprecatedExpressionMapping[license]?.also { return it }
63+
if (mapSimple) simpleExpressionMapping[license]?.also { return it }
64+
return SpdxLicense.forId(license)?.toExpression()
65+
}
6566
}

‎utils/spdx/src/test/kotlin/SpdxSimpleLicenseMappingTest.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,19 @@ class SpdxSimpleLicenseMappingTest : WordSpec({
6262

6363
"The mapping" should {
6464
"contain only single ID strings" {
65-
SpdxSimpleLicenseMapping.mapping.keys.forAll { declaredLicense ->
66-
val tokens = SpdxExpressionLexer(declaredLicense).tokens().toList()
65+
val ids = SpdxSimpleLicenseMapping.simpleExpressionMapping.keys +
66+
SpdxSimpleLicenseMapping.deprecatedExpressionMapping.keys
67+
68+
ids.forAll { id ->
69+
val tokens = SpdxExpressionLexer(id).tokens().toList()
6770

6871
tokens shouldHaveAtLeastSize 1
6972
tokens shouldHaveAtMostSize 2
7073

7174
tokens.first() should beOfType<Token.IDENTIFIER>()
7275
tokens.getOrNull(1)?.let { it should beOfType<Token.PLUS>() }
7376

74-
SpdxExpressionParser(tokens.asSequence()).parse().toString() shouldBe declaredLicense
77+
SpdxExpressionParser(tokens.asSequence()).parse().toString() shouldBe id
7578
}
7679
}
7780

0 commit comments

Comments
 (0)
Please sign in to comment.