Skip to content

Commit

Permalink
refactor(gitlab-reporter): Use Ks3 serializers
Browse files Browse the repository at this point in the history
Reduce custom code by using the new sorted collection serializers from
Ks3.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Mar 14, 2024
1 parent e16b6f9 commit d2583ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
1 change: 1 addition & 0 deletions plugins/reporters/gitlab/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ dependencies {
implementation(projects.utils.spdxUtils)

implementation(libs.bundles.kotlinxSerialization)
implementation(libs.bundles.ks3)
}
33 changes: 11 additions & 22 deletions plugins/reporters/gitlab/src/main/kotlin/GitLabLicenseModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

package org.ossreviewtoolkit.plugins.reporters.gitlab

import io.ks3.standard.sortedListSerializer
import io.ks3.standard.sortedSetSerializer

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.serializer

private const val GITLAB_LICENSE_SCANNING_SCHEMA_VERSION_MAJOR_MINOR = "2.1"

Expand All @@ -42,15 +43,13 @@ internal data class GitLabLicenseModel(
/**
* The complete set of licenses referred to by [dependencies].
*/
@Serializable(SortedLicenseCollectionSerializer::class)
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(SortedLicenseSetSerializer::class)
val licenses: Set<License>,

/**
* The list of all dependencies.
*/
@Serializable(SortedDependenciesCollectionSerializer::class)
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(SortedDependenciesListSerializer::class)
val dependencies: List<Dependency>
) {
@Serializable
Expand Down Expand Up @@ -96,27 +95,17 @@ internal data class GitLabLicenseModel(
/**
* The declared licenses of this dependency.
*/
@Serializable(SortedStringCollectionSerializer::class)
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(SortedStringSetSerializer::class)
val licenses: Set<String>
)
}

private class SortedDependenciesCollectionSerializer :
KSerializer<Collection<GitLabLicenseModel.Dependency>> by sortedCollectionSerializer(
private class SortedDependenciesListSerializer :
KSerializer<List<GitLabLicenseModel.Dependency>> by sortedListSerializer(
compareBy({ it.packageManager }, { it.name }, { it.version })
)

private class SortedLicenseCollectionSerializer :
KSerializer<Collection<GitLabLicenseModel.License>> by sortedCollectionSerializer(compareBy { it.id })

private class SortedStringCollectionSerializer :
KSerializer<Collection<String>> by sortedCollectionSerializer(compareBy { it })
private class SortedLicenseSetSerializer :
KSerializer<Set<GitLabLicenseModel.License>> by sortedSetSerializer(compareBy { it.id })

private inline fun <reified T> sortedCollectionSerializer(comparator: Comparator<in T>): KSerializer<Collection<T>> {
val delegate = serializer<Collection<T>>()
return object : KSerializer<Collection<T>> by delegate {
override fun serialize(encoder: Encoder, value: Collection<T>) =
delegate.serialize(encoder, value.sortedWith(comparator))
}
}
private class SortedStringSetSerializer : KSerializer<Set<String>> by sortedSetSerializer(compareBy { it })

0 comments on commit d2583ac

Please sign in to comment.