Skip to content

Commit

Permalink
refactor(cargo): Migrate manifest parsing to kotlinx-serialization
Browse files Browse the repository at this point in the history
Note that kotlinx-serialization requires optional properties to have
default values even if they are nullable.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Oct 29, 2023
1 parent 44523e4 commit 3f835b3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
16 changes: 13 additions & 3 deletions plugins/package-managers/cargo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* License-Filename: LICENSE
*/

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
Expand All @@ -40,10 +42,18 @@ dependencies {
implementation(project(":utils:ort-utils"))
implementation(project(":utils:spdx-utils"))

implementation(libs.jacksonDatabind)
implementation(libs.jacksonModuleKotlin)
implementation(libs.kotlinxSerializationCore)
implementation(libs.bundles.kotlinxSerialization)
implementation(libs.tomlkt)

funTestImplementation(testFixtures(project(":analyzer")))
}

tasks.withType<KotlinCompile>().configureEach {
val customCompilerArgs = listOf(
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
)

compilerOptions {
freeCompilerArgs.addAll(customCompilerArgs)
}
}
13 changes: 9 additions & 4 deletions plugins/package-managers/cargo/src/main/kotlin/Cargo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@

package org.ossreviewtoolkit.plugins.packagemanagers.cargo

import com.fasterxml.jackson.module.kotlin.readValue

import java.io.File

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNamingStrategy

import net.peanuuutz.tomlkt.Toml
import net.peanuuutz.tomlkt.decodeFromNativeReader

Expand All @@ -46,7 +47,6 @@ import org.ossreviewtoolkit.model.RemoteArtifact
import org.ossreviewtoolkit.model.Scope
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.jsonMapper
import org.ossreviewtoolkit.model.orEmpty
import org.ossreviewtoolkit.utils.common.CommandLineTool
import org.ossreviewtoolkit.utils.common.splitOnWhitespace
Expand All @@ -56,6 +56,11 @@ import org.ossreviewtoolkit.utils.ort.ProcessedDeclaredLicense
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
import org.ossreviewtoolkit.utils.spdx.SpdxOperator

private val json = Json {
ignoreUnknownKeys = true
namingStrategy = JsonNamingStrategy.SnakeCase
}

private val toml = Toml { ignoreUnknownKeys = true }

/**
Expand Down Expand Up @@ -164,7 +169,7 @@ class Cargo(

val workingDir = definitionFile.parentFile
val metadataProcess = run(workingDir, "metadata", "--format-version=1")
val metadata = jsonMapper.readValue<CargoMetadata>(metadataProcess.stdout)
val metadata = json.decodeFromString<CargoMetadata>(metadataProcess.stdout)
val hashes = readHashes(resolveLockfile(metadata))

val packages = metadata.packages.associateBy(
Expand Down
31 changes: 16 additions & 15 deletions plugins/package-managers/cargo/src/main/kotlin/CargoMetadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,46 @@

package org.ossreviewtoolkit.plugins.packagemanagers.cargo

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import kotlinx.serialization.Serializable

/**
* See https://doc.rust-lang.org/cargo/commands/cargo-metadata.html.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
internal data class CargoMetadata(
val packages: List<Package>,
val workspaceMembers: List<String>,
val resolve: Resolve,
val workspaceRoot: String
) {
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Package(
val name: String,
val version: String,
val id: String,
val license: String?,
val licenseFile: String?,
val description: String?,
val source: String?,
val dependencies: List<Dependency>,
val authors: List<String>,
val repository: String?,
val homepage: String?
val license: String? = null,
val licenseFile: String? = null,
val description: String? = null,
val source: String? = null,
val dependencies: List<Dependency> = emptyList(),
val authors: List<String> = emptyList(),
val repository: String? = null,
val homepage: String? = null
)

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Dependency(
val name: String,
val kind: String?
val kind: String? = null
)

@Serializable
data class Resolve(
val nodes: List<Node>,
val root: String?
val root: String? = null
)

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Node(
val id: String,
val dependencies: List<String>
Expand Down

0 comments on commit 3f835b3

Please sign in to comment.