Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into runtime_deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tan108 committed Dec 23, 2024
2 parents 581831c + 47e3139 commit 434b5f1
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 21 deletions.
89 changes: 87 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/Conditions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object Conditions {
EQUALS -> attributeValue.value == conditionValue.value
NOT_EQUALS -> attributeValue.value != conditionValue.value
CONTAINS -> attributeValue.value?.contains(conditionValue.value.orEmpty()) ?: false
NOT_CONTAINS -> attributeValue.value?.contains(conditionValue.value.orEmpty())?.not() ?: false
NOT_CONTAINS -> attributeValue.value?.contains(conditionValue.value.orEmpty())?.not() ?: false
STARTS_WITH -> attributeValue.value?.startsWith(conditionValue.value.orEmpty()) ?: false
ENDS_WITH -> attributeValue.value?.endsWith(conditionValue.value.orEmpty()) ?: false
SEMVER_EQUALS -> compareVersions(
Expand Down Expand Up @@ -119,6 +119,65 @@ object Conditions {
}
}

attributeValue is AttributeValue.IntValue && conditionValue is ConditionValue.ArrayValue -> {
when (operator) {
IN_ARRAY -> attributeValue.value.toString() in conditionValue.values
NOT_IN_ARRAY -> (attributeValue.value.toString() !in conditionValue.values)
else -> false
}
}

attributeValue is AttributeValue.DoubleValue && conditionValue is ConditionValue.StringValue -> {
when (operator) {
EQUALS -> attributeValue.value.toString() == conditionValue.value
NOT_EQUALS -> attributeValue.value.toString() != conditionValue.value

SEMVER_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty(),
) == 0

SEMVER_NOT_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty(),
) != 0

SEMVER_GREATER_THAN -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) == 1

SEMVER_GREATER_THAN_OR_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) >= 0

SEMVER_LESS_THAN -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) == -1

SEMVER_LESS_THAN_OR_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) <= 0

else -> false
}
}

attributeValue is AttributeValue.StringValue && conditionValue is ConditionValue.IntValue -> {
when (operator) {
EQUALS -> attributeValue.value == conditionValue.value.toString()
NOT_EQUALS -> attributeValue.value != conditionValue.value.toString()
CONTAINS -> attributeValue.value?.contains(conditionValue.value.toString()) ?: false
NOT_CONTAINS -> attributeValue.value?.contains(conditionValue.value.toString())?.not() ?: false
STARTS_WITH -> attributeValue.value?.startsWith(conditionValue.value.toString()) ?: false
ENDS_WITH -> attributeValue.value?.endsWith(conditionValue.value.toString()) ?: false
else -> false
}
}

attributeValue is AttributeValue.DateValue && conditionValue is ConditionValue.DateTimeValue -> {
when (operator) {
EQUALS -> attributeValue.value == conditionValue.value
Expand All @@ -143,9 +202,35 @@ object Conditions {

private fun compareVersions(actual: String, condition: String): Int {
return try {
SemVer.parse(actual).compareTo(SemVer.parse(condition))
SemVer.parse(normalizeSemver(actual)).compareTo(SemVer.parse(normalizeSemver(condition)))
} catch (e: Exception) {
0
}
}

private fun normalizeSemver(version: String): String {
val parts = version.split("-", "+")
val mainParts = parts[0].split(".")

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(".") {
it
}
normalizedVersion += "-$preRelease"
}

if (parts.size > 2 && parts[2].isNotEmpty()) {
val buildMetadata = parts[2]
normalizedVersion += "+$buildMetadata"
}

return normalizedVersion
}
}
Loading

0 comments on commit 434b5f1

Please sign in to comment.