Skip to content

Commit

Permalink
refactor(clearly-defined): Get enum serial names more efficiently
Browse files Browse the repository at this point in the history
Based on the hint at [1] and the code at [2], leverage the serializer's
descriptor to get the serial name of the element, without doing any
"dummy serialization".

[1]: Kotlin/kotlinx.serialization#1482 (comment)
[2]: https://github.com/andregasser/bigbone/pull/332/files#diff-8d64fdf06ceb51ff99e649f64c911cf787b07aff678d5d7efd7bf13322db4983R90

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed May 27, 2024
1 parent e2f71f0 commit f19867c
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions clients/clearly-defined/src/main/kotlin/Enums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ package org.ossreviewtoolkit.clients.clearlydefined

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonPrimitive

/**
* See https://github.com/clearlydefined/service/blob/48f2c97/schemas/definition-1.0.json#L32-L48.
Expand Down Expand Up @@ -50,7 +48,7 @@ enum class ComponentType(val defaultProvider: Provider? = null) {

// Align the string representation with the serial name to make Retrofit's GET request work. Also see:
// https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter/issues/39
override fun toString() = ClearlyDefinedService.JSON.encodeToJsonElement(this).jsonPrimitive.content
override fun toString() = serializer().descriptor.getElementName(ordinal)
}

/**
Expand Down Expand Up @@ -80,7 +78,7 @@ enum class Provider {

// Align the string representation with the serial name to make Retrofit's GET request work. Also see:
// https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter/issues/39
override fun toString() = ClearlyDefinedService.JSON.encodeToJsonElement(this).jsonPrimitive.content
override fun toString() = serializer().descriptor.getElementName(ordinal)
}

/**
Expand Down

0 comments on commit f19867c

Please sign in to comment.