Skip to content

Commit

Permalink
refactor(scancode): Rely on output_format_version to be present
Browse files Browse the repository at this point in the history
Avoid the need to handle a non-existing output format version.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Nov 18, 2024
1 parent 7feab15 commit a7d31d8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ data class HeaderEntry(
val options: Map<String, JsonElement>,
val startTimestamp: String,
val endTimestamp: String,
val outputFormatVersion: String? = null // This might be missing in JSON.
val outputFormatVersion: String
) {
fun getInput(): File {
val inputPath = when (val input = options.getValue("input")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ fun ScanCodeResult.toScanSummary(preferFileLicense: Boolean = false): ScanSummar
val header = headers.single()
val inputPath = header.getInput()

val outputFormatVersion = header.outputFormatVersion?.let { Semver(it) }
if (outputFormatVersion != null && outputFormatVersion.major > MAX_SUPPORTED_OUTPUT_FORMAT_MAJOR_VERSION) {
val outputFormatVersion = Semver(header.outputFormatVersion)

if (outputFormatVersion.major > MAX_SUPPORTED_OUTPUT_FORMAT_MAJOR_VERSION) {
issues += createAndLogIssue(
source = ScanCode.SCANNER_NAME,
message = "The output format version $outputFormatVersion exceeds the supported major version " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import java.io.File
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNamingStrategy
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.modules.SerializersModule

import org.semver4j.Semver
Expand All @@ -40,11 +40,16 @@ private fun parseResult(result: JsonElement): ScanCodeResult {
// As even the structure of the header itself may change with the output format version, first operate on raw JSON
// elements to get the version, and then parse the JSON elements into the appropriate data classes.
val header = result.jsonObject.getValue("headers").jsonArray.single().jsonObject
val outputFormatVersion = header["output_format_version"]?.let { Semver(it.jsonPrimitive.content) }

val outputFormatVersionPrimitive = requireNotNull(header["output_format_version"] as? JsonPrimitive) {
"ScanCode results that do not define an 'output_format_version' are not supported anymore."
}

val outputFormatVersion = Semver(outputFormatVersionPrimitive.content)

// Select the correct set of (de-)serializers bundled in a module for parsing the respective format version.
val module = when (outputFormatVersion?.major) {
null, 1 -> SerializersModule {
val module = when (outputFormatVersion.major) {
1 -> SerializersModule {
polymorphicDefaultDeserializer(FileEntry::class) { FileEntry.Version1.serializer() }
polymorphicDefaultDeserializer(LicenseEntry::class) { LicenseEntry.Version1.serializer() }
polymorphicDefaultDeserializer(CopyrightEntry::class) { CopyrightEntry.Version1.serializer() }
Expand Down

0 comments on commit a7d31d8

Please sign in to comment.