From 64652fd5ddbdadf8e21ed38f533c55a21da01ef5 Mon Sep 17 00:00:00 2001 From: Tanmay Ranjan Date: Thu, 19 Dec 2024 17:26:18 +0530 Subject: [PATCH] semver version with more than 3 digits throws exception fixes --- .../kotlin/com/featurevisor/sdk/Conditions.kt | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/featurevisor/sdk/Conditions.kt b/src/main/kotlin/com/featurevisor/sdk/Conditions.kt index bef5b03..e62453e 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Conditions.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Conditions.kt @@ -210,23 +210,27 @@ object Conditions { private fun normalizeSemver(version: String): String { val parts = version.split("-", "+") - val mainParts = parts[0].split(".").map { it.toInt().toString() } - var normalizedVersion = mainParts.joinToString(".") + val mainParts = parts[0].split(".") - if (version.contains("-")) { + val normalizedMainParts = mainParts.take(3).mapIndexed { _, part -> + val num = part.toIntOrNull() ?: 0 + num.coerceAtMost(999).toString() + } + + var normalizedVersion = normalizedMainParts.joinToString(".") + + if (parts.size > 1 && parts[1].isNotEmpty()) { val preRelease = parts[1].split(".").joinToString(".") { - if (it.all { char -> char.isDigit() }) it.toInt().toString() - else it + it } normalizedVersion += "-$preRelease" } - if (version.contains("+")) { - val buildMetadata = version.split("+")[1] + if (parts.size > 2 && parts[2].isNotEmpty()) { + val buildMetadata = parts[2] normalizedVersion += "+$buildMetadata" } return normalizedVersion } - }