Skip to content

Commit 9321907

Browse files
committed
refactor(ort-project-file): Use Identifier as the type for id
Signed-off-by: Frank Viernau <frank.viernau@gmail.com>
1 parent 6886cfb commit 9321907

3 files changed

Lines changed: 32 additions & 13 deletions

File tree

plugins/package-managers/ort-project-file/src/funTest/kotlin/OrtProjectFileFunTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class OrtProjectFileFunTest : WordSpec({
192192
val project = OrtProjectFileFactory.create().resolveSingleProject(definitionFile)
193193
project.packages should beEmptyCollection()
194194
project.issues.shouldBeSingleton {
195-
it.message shouldContain "The id 'Maven:0.1.0' is not a valid Identifier."
195+
it.message shouldContain "The id 'Maven:0.1.0::' is not a valid Identifier."
196196
it.source shouldBe "ORT Project File"
197197
}
198198
}

plugins/package-managers/ort-project-file/src/main/kotlin/OrtProject.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ import com.charleskorn.kaml.Yaml
2424
import java.io.File
2525
import java.io.IOException
2626

27+
import kotlinx.serialization.KSerializer
2728
import kotlinx.serialization.Serializable
2829
import kotlinx.serialization.decodeFromString
30+
import kotlinx.serialization.descriptors.PrimitiveKind
31+
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
32+
import kotlinx.serialization.descriptors.SerialDescriptor
33+
import kotlinx.serialization.encoding.Decoder
34+
import kotlinx.serialization.encoding.Encoder
2935
import kotlinx.serialization.json.Json
3036

37+
import org.ossreviewtoolkit.model.Identifier
38+
3139
@Serializable
3240
internal data class OrtProject(
3341
val projectName: String? = null,
@@ -39,7 +47,8 @@ internal data class OrtProject(
3947
) {
4048
@Serializable
4149
data class Dependency(
42-
val id: String? = null,
50+
@Serializable(with = IdentifierSerializer::class)
51+
val id: Identifier? = null,
4352
val purl: String? = null,
4453
val description: String? = null,
4554
val vcs: Vcs? = null,
@@ -84,3 +93,16 @@ internal fun File.parseOrtProject(): OrtProject =
8493
}.getOrElse { cause ->
8594
throw IOException("Could not parse ORT project file at '$absolutePath'.", cause)
8695
}
96+
97+
private object IdentifierSerializer : KSerializer<Identifier> {
98+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(
99+
serialName = checkNotNull(Identifier::class.qualifiedName),
100+
kind = PrimitiveKind.STRING
101+
)
102+
103+
override fun serialize(encoder: Encoder, value: Identifier) {
104+
encoder.encodeString(value.toCoordinates())
105+
}
106+
107+
override fun deserialize(decoder: Decoder) = Identifier(decoder.decodeString())
108+
}

plugins/package-managers/ort-project-file/src/main/kotlin/OrtProjectMapper.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,14 @@ private fun Dependency.getIdentifiers(): Pair<Identifier, String> {
9999
val packageUrl = purl.toPackageUrl()
100100

101101
when {
102-
id.isNullOrBlank() && packageUrl == null ->
102+
id == null && packageUrl == null ->
103103
throw IllegalArgumentException("There is no id or purl defined for the package.")
104104

105-
!id.isNullOrBlank() && packageUrl != null ->
106-
return Pair(Identifier(id), packageUrl.toString())
105+
id != null && packageUrl != null ->
106+
return Pair(id, packageUrl.toString())
107107

108-
!id.isNullOrBlank() -> {
109-
val identifier = Identifier(id)
110-
return Pair(identifier, identifier.toPurl())
111-
}
108+
id != null ->
109+
return Pair(id, id.toPurl())
112110

113111
packageUrl != null ->
114112
return Pair(packageUrl.toIdentifier(), packageUrl.toString())
@@ -121,10 +119,9 @@ private fun Dependency.getIdentifiers(): Pair<Identifier, String> {
121119
}
122120

123121
private fun Dependency.validateIdentifiers() {
124-
if (!id.isNullOrBlank()) {
125-
val identifier = Identifier(id)
126-
require(!identifier.type.isBlank() && !identifier.name.isBlank() && !identifier.version.isBlank()) {
127-
"The id '$id' is not a valid Identifier."
122+
if (id != null) {
123+
require(!id.type.isBlank() && !id.name.isBlank() && !id.version.isBlank()) {
124+
"The id '${id.toCoordinates()}' is not a valid Identifier."
128125
}
129126
}
130127

0 commit comments

Comments
 (0)