Skip to content

Commit

Permalink
feat(CompareCommand): Implement custom deserializer
Browse files Browse the repository at this point in the history
The custom deserializer for `Instant` and `Environment` are necessary for
the ignore options. If they are set, then default values will be
deserialized for those properties.

Signed-off-by: Onur Demirci <[email protected]>
  • Loading branch information
bs-ondem committed Nov 27, 2023
1 parent 243db7e commit ef3aa81
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion plugins/commands/compare/src/main/kotlin/CompareCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
package org.ossreviewtoolkit.plugins.commands.compare

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.module.kotlin.readValue
Expand Down Expand Up @@ -92,7 +96,14 @@ class CompareCommand : OrtCommand(
throw ProgramResult(2)
}

val deserializer = fileA.mapper()
val deserializer = fileA.mapper().registerModule(
SimpleModule().apply {
// TODO: Find a way to also ignore temporary directories.
if (ignoreTime) addDeserializer(Instant::class.java, EpochInstantDeserializer())
if (ignoreEnvironment) addDeserializer(Environment::class.java, DefaultEnvironmentDeserializer())
}
)

val resultA = deserializer.readValue<OrtResult>(fileA)
val resultB = deserializer.readValue<OrtResult>(fileB)

Expand Down Expand Up @@ -158,12 +169,22 @@ private class InvariantInstantSerializer : StdSerializer<Instant>(Instant::class
}
}

private class EpochInstantDeserializer : StdDeserializer<Instant>(Instant::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): Instant =
Instant.EPOCH.also { parser.codec.readTree<JsonNode>(parser) }
}

private class InvariantEnvironmentSerializer : StdSerializer<Environment>(Environment::class.java) {
override fun serialize(value: Environment, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeString("invariant")
}
}

private class DefaultEnvironmentDeserializer : StdDeserializer<Environment>(Environment::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): Environment =
Environment().also { parser.codec.readTree<JsonNode>(parser) }
}

private fun Map<Regex, String>.replaceIn(text: String) =
entries.fold(text) { currentText, (from, to) ->
currentText.replace(from, to)
Expand Down

0 comments on commit ef3aa81

Please sign in to comment.