Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IG current version to selection widget #154

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/commonMain/kotlin/model/PackageInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package model

import kotlinx.serialization.Serializable

import kotlin.math.max


@Serializable
data class PackageInfo(
var id: String? = null,
Expand All @@ -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 {
Expand All @@ -36,6 +39,44 @@ data class PackageInfo(
}
}

class VersionComparator : Comparator<PackageInfo> {
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<String> = a.split(".", "-")
val bParts: List<String> = 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}"
Expand Down
4 changes: 0 additions & 4 deletions src/jsMain/kotlin/reactredux/slices/LocalizationSlice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/jsMain/kotlin/ui/components/options/IgSelector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ class IgSelector : RComponent<IgSelectorProps, IgSelectorState>() {
try {
val igResponse = sendIGVersionsRequest(igPackageName)
igResponse.packageInfo

} catch (e : Exception) {
mutableListOf()
}

val registryPackages : MutableList<PackageInfo> = props.igList.filter{ it.id == igPackageName }.toMutableList();
val allPackages = (registryPackages + simplifierPackages).distinctBy{it.version}
.sortedBy{it.version}.reversed().toMutableList()


val registryPackages : MutableList<PackageInfo> = 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()
Expand Down Expand Up @@ -96,6 +100,7 @@ class IgSelector : RComponent<IgSelectorProps, IgSelectorState>() {
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 {
Expand Down
32 changes: 32 additions & 0 deletions src/jvmTest/kotlin/model/PackageInfoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackageInfo>, sorted : List<PackageInfo>) {
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"))
Expand All @@ -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))
)
}
}