From b7fc5a2d27e2433220483af0d7128439429e9b75 Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 14 Nov 2023 14:09:23 -0500 Subject: [PATCH] Add IG current version to selection widget Also, fix version ordering and null versioned IGs that appear as blanks --- src/commonMain/kotlin/model/PackageInfo.kt | 43 ++++++++++++++++++- .../reactredux/slices/LocalizationSlice.kt | 4 -- .../ui/components/options/IgSelector.kt | 11 +++-- src/jvmTest/kotlin/model/PackageInfoTest.kt | 32 ++++++++++++++ 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/commonMain/kotlin/model/PackageInfo.kt b/src/commonMain/kotlin/model/PackageInfo.kt index 6fb649ca..66d5db48 100644 --- a/src/commonMain/kotlin/model/PackageInfo.kt +++ b/src/commonMain/kotlin/model/PackageInfo.kt @@ -2,6 +2,9 @@ package model import kotlinx.serialization.Serializable +import kotlin.math.max + + @Serializable data class PackageInfo( var id: String? = null, @@ -18,7 +21,7 @@ data class PackageInfo( fun fhirVersionMatches(fhirVersion: String): Boolean { this.fhirVersion?.let { return extractMajorMinor(this.fhirVersion!!) == extractMajorMinor(fhirVersion) - } ?: return false + } ?: return true } fun extractMajorMinor(fhirVersion: String): String { @@ -36,6 +39,44 @@ data class PackageInfo( } } + class VersionComparator : Comparator { + override fun compare(a: PackageInfo, b: PackageInfo): Int { + if (a.version.equals("current")) { + return -1; + } else if (b.version.equals("current")) { + return 1; + } + if (a.version == null && b.version == null) { + return 0; + } + if (a.version == null && b.version != null) { + return -1; + } + if (b.version == null && a.version != null) { + return 1; + } + return try { + compare(a.version!!, b.version!!) + } catch (e : Exception) { + return a.version!!.compareTo(b.version!!) + } + + } + + fun compare(a : String, b: String) : Int{ + val aParts: List = a.split(".", "-") + val bParts: List = b.split(".", "-") + val length = max(aParts.size.toDouble(), bParts.size.toDouble()).toInt() + for (i in 0 until length) { + val aPart = if (i < aParts.size) aParts[i].toInt() else 0 + val bPart = if (i < bParts.size) bParts[i].toInt() else 0 + if (aPart < bPart) return -1 + if (aPart > bPart) return 1 + } + return 0 + } + } + companion object { fun igLookupString( packageName : String, packageVersion: String) : String { return "${packageName}#${packageVersion}" diff --git a/src/jsMain/kotlin/reactredux/slices/LocalizationSlice.kt b/src/jsMain/kotlin/reactredux/slices/LocalizationSlice.kt index e238ff2c..dcae32fa 100644 --- a/src/jsMain/kotlin/reactredux/slices/LocalizationSlice.kt +++ b/src/jsMain/kotlin/reactredux/slices/LocalizationSlice.kt @@ -23,13 +23,9 @@ object LocalizationSlice { fun reducer(state: State = State(), action: RAction): State { return when (action) { is SetPolyglot -> { - println("setting new polyglot instance\nexisting -> ${state.polyglotInstance.t("validate_heading")}") - println("setting new polyglot instance\nnew -> ${action.polyglotInstance.t("validate_heading")}") state.copy(polyglotInstance = action.polyglotInstance) } is SetLanguage -> { - println("setting new lang instance\nexisting -> ${state.selectedLanguage}") - println("setting new lang instance\nnew -> ${action.selectedLangauge}") state.copy(selectedLanguage = action.selectedLangauge) } else -> state diff --git a/src/jsMain/kotlin/ui/components/options/IgSelector.kt b/src/jsMain/kotlin/ui/components/options/IgSelector.kt index c52331e3..62de4267 100644 --- a/src/jsMain/kotlin/ui/components/options/IgSelector.kt +++ b/src/jsMain/kotlin/ui/components/options/IgSelector.kt @@ -47,13 +47,17 @@ class IgSelector : RComponent() { try { val igResponse = sendIGVersionsRequest(igPackageName) igResponse.packageInfo + } catch (e : Exception) { mutableListOf() } - val registryPackages : MutableList = props.igList.filter{ it.id == igPackageName }.toMutableList(); - val allPackages = (registryPackages + simplifierPackages).distinctBy{it.version} - .sortedBy{it.version}.reversed().toMutableList() + + + val registryPackages : MutableList = props.igList.filter{ it.id == igPackageName && it.version != null }.toMutableList(); + val allPackages = (registryPackages + simplifierPackages + PackageInfo(id = igPackageName, fhirVersion = null, url = null, version = "current")).distinctBy{it.version} + .sortedWith(PackageInfo.VersionComparator()).reversed().toMutableList() + setState { packageVersions = allPackages.map { Pair(it, false) }.toMutableList() @@ -96,6 +100,7 @@ class IgSelector : RComponent() { val versions = state.packageVersions.filter { it.first.fhirVersionMatches(props.fhirVersion)} .map{Pair(it.first.version ?: "", it.second)} .toMutableList() + val versionSelected = versions.filter { it.second }.isNotEmpty() styledSpan { css { diff --git a/src/jvmTest/kotlin/model/PackageInfoTest.kt b/src/jvmTest/kotlin/model/PackageInfoTest.kt index 0eb51f8c..1abfbb24 100644 --- a/src/jvmTest/kotlin/model/PackageInfoTest.kt +++ b/src/jvmTest/kotlin/model/PackageInfoTest.kt @@ -3,11 +3,22 @@ package model import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource internal class PackageInfoTest { + @ParameterizedTest + @MethodSource("listsToCompare") + fun versionComparatorWorks(unsorted: List, sorted : List) { + assertEquals(sorted, unsorted.sortedWith(PackageInfo.VersionComparator())) + } + + @Test fun fhirVersionMatches() { + assertTrue(PackageInfo(fhirVersion = null).fhirVersionMatches("4.0.1")) assertTrue(PackageInfo(fhirVersion = "4.0.1").fhirVersionMatches("4.0.1")) assertTrue(PackageInfo(fhirVersion = "4.0.1").fhirVersionMatches("4.0.5")) assertTrue(PackageInfo(fhirVersion = "4.0.1").fhirVersionMatches("4.0")) @@ -21,4 +32,25 @@ internal class PackageInfoTest { assertEquals("4.0", PackageInfo().extractMajorMinor("4.0.1.1")) assertEquals("4", PackageInfo().extractMajorMinor("4")) } + + companion object { + val current_package = PackageInfo(version = "current") + val _0_0_1_package = PackageInfo(version = "0.0.1") + val _0_0_2_package = PackageInfo(version = "0.0.2") + val _0_1_2_package = PackageInfo(version = "0.1.2") + val _0_1_2_snapshot_package = PackageInfo(version = "0.1.2-snapshot") + val _1_5_0_package = PackageInfo(version = "1.5.0") + val _6_0_0_package = PackageInfo(version = "6.0.0") + val _15_5_0_package = PackageInfo(version = "15.5.0") + @JvmStatic + fun listsToCompare() = listOf( + Arguments.of(listOf(_0_0_1_package), listOf(_0_0_1_package)), + Arguments.of(listOf(_0_0_2_package, _0_0_1_package), listOf(_0_0_1_package, _0_0_2_package)), + + Arguments.of(listOf(_15_5_0_package, _0_0_2_package, _1_5_0_package), listOf(_0_0_2_package, _1_5_0_package, _15_5_0_package)), + Arguments.of( + listOf(current_package, _6_0_0_package, _0_1_2_snapshot_package, _0_1_2_package, _15_5_0_package, _0_0_2_package, _1_5_0_package), + listOf(current_package, _0_0_2_package, _0_1_2_package, _0_1_2_snapshot_package, _1_5_0_package, _6_0_0_package, _15_5_0_package)) + ) + } } \ No newline at end of file