ImI'm not really happy with the below solution. Background is that I am receiving results from a graphql query that includeincludes tags, and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node, I want to set the default weight.
Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im. I'm not sure if I like having additional logic in an enum like that. Also I am not happy that my toDto function still contains quite a bit of logic to achieve the final result of assigning a weight to the dto.
enum class Weight(val value: Int) {
LEVEL1(3),
LEVEL2(2),
DEFAULT(1);
companion object {
fun fromTagName(tagName: String): Weight {
return when (tagName) {
"level1" -> LEVEL1
"level2" -> LEVEL2
else -> DEFAULT
}
}
}
}
fun toDto(node: BaseNode): Result<Dto> =
Result.catch {
val weight = node.tags.filter { tag -> tag.name == "tier1" || tag.name == "tier2" }
.map { tag -> Weight.fromTagName(tag.name) }
.firstOrNull()?.value ?: Weight.DEFAULT.value
Dto(
name = provider.name,
weight = weight
)
}