Skip to content

Commit aa94156

Browse files
committed
feat(model): Support aliases for renamed identifier types
Add a map to define aliases for renamed identifier types and use that when matching types. Ideally, the aliases should come from the package manager implementations where the renaming takes place, but that is not possible because the matching logic is inside the `model` module which is a dependency of the package manager plugins. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent f9b0252 commit aa94156

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

model/src/main/kotlin/Identifier.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,22 @@ data class Identifier(
126126
sanitizedProperties.joinToString(separator) { it.encodeOr(emptyValue) }
127127
}
128128

129+
/**
130+
* A map of lowercase aliases for types that were renamed.
131+
*/
132+
private val typeAliases = mapOf(
133+
"spdxdocument" to "spdxdocumentfile"
134+
)
135+
129136
/**
130137
* Return whether the [type][Identifier.type] of this [Identifier] matches the type of [other] by comparing them
131-
* case-insensitively.
138+
* case-insensitively. Additionally, the [typeAliases] are considered.
132139
*/
133-
fun Identifier.typeMatches(other: Identifier) = type.equals(other.type, ignoreCase = true)
140+
fun Identifier.typeMatches(other: Identifier): Boolean {
141+
val thisType = type.lowercase()
142+
val otherType = other.type.lowercase()
143+
144+
return thisType == otherType ||
145+
typeAliases[thisType] == otherType ||
146+
typeAliases[otherType] == thisType
147+
}

model/src/test/kotlin/IdentifierTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,12 @@ class IdentifierTest : WordSpec({
147147
val id = Identifier.EMPTY.copy(type = "type")
148148
id.typeMatches(Identifier.EMPTY.copy(type = "other")) shouldBe false
149149
}
150+
151+
"return true if the type matches an alias" {
152+
val id = Identifier.EMPTY.copy(type = "SpdxDocument")
153+
val otherId = Identifier.EMPTY.copy(type = "spdxdocumentfile")
154+
id.typeMatches(otherId) shouldBe true
155+
otherId.typeMatches(id) shouldBe true
156+
}
150157
}
151158
})

0 commit comments

Comments
 (0)