我有兩個函式(GetPodsOne 和 GetPodsTwo)回傳一個大的 csv 字串。然后我做一些處理來丟棄我不想要的字串部分。請參閱下面的片段。
var podValues = execGetPodsOne()
val testPodValuesLst: List<String> = podValues.split(",").map { it -> it.substringAfterLast("/") }?
testPodValuesLst.forEach { it ->? println("value from testPodList=$it")? }
podValues = execGetPodsTwo()
val sitPodValuesLst: List<String> = podValues.split(",").map { it -> it.substringAfterLast("/") }?
sitPodValuesLst.forEach { it ->? println("value from sitPodList=$it")? }
這給我留下了兩個串列。請參閱以下輸出:
value from testPodList=api-car-v1:0.0.118
value from testPodList=api-dog-v1:0.0.11
value from testPodList=api-plane-v1:0.0.36
value from sitPodList=api-car-v1:0.0.119
value from sitPodList=api-dog-v1:0.0.12
value from sitPodList=api-plane-v1:0.0.37
我想要做的是最終得到一個資料類中的物件,如下所示:
data class ImageVersions(val apiName: String, val testPodVersion: String, val sitPodVersion: String)
api-car-v1, 0.0.118, 0.0.119
api-dog-v1, 0.0.11, 0.0.12
api-plane-v1, 0.0.36, 0.0.37
我已經使用了 test 并坐在上面,但我最終可能會有另外 5 個環境。尋找一種很好的方法來獲取每個 api 的版本并輕松組合到該 ImageVersions 資料類中。
謝謝
uj5u.com熱心網友回復:
考慮到你是going to have maybe another 5 environments eventually,我試著寫一些可以很好地擴展的東西:
enum class Env { Test, Sit }
data class ImageVersions(val apiName: String, val versions: Map<Env, String?>)
fun String.getNameAndVersion() = substringBefore(':') to substringAfter(':')
fun getVersions(envMap: Map<Env, List<String>>): List<ImageVersions> {
val envApiNameMap = envMap.mapValues { it.value.associate(String::getNameAndVersion) }
val allApiNames = envApiNameMap.flatMap { it.value.keys }.distinct()
return allApiNames.map { apiName ->
ImageVersions(apiName, envApiNameMap.mapValues { it.value[apiName] })
}
}
游樂場示例
- 因此
val testPodVersion: String, val sitPodVersion: String,這里有一張地圖,而不是單獨的。現在ImageVersions無論您擁有多少環境,的結構始終保持不變。 getNameAndVersion是一個輔助函式,用于從原始字串中提取 apiName 和 version。getVersions接受對應于每個環境的版本串列并回傳一個串列ImageVersionsenvApiNameMap是一樣的envMap只是現在,這份清單地圖apiName及其版本的。allApiNames包含來自所有環境的所有可用 apiNames。- 然后對于每個
apiName,我們apiName從所有環境中獲取所有版本。
將來,如果您有另一個環境,只需將其添加到Env列舉中并在envMapof 中傳遞一個額外的映射條目getVersions。每次有新環境時都不需要修改此函式。
uj5u.com熱心網友回復:
這個怎么樣:
val testPodValuesMap = testPodValuesLst.associate { it.split(':').zipWithNext().single() }
val sitPodValuesMap = sitPodValuesLst.associate { it.split(':').zipWithNext().single() }
val mergedMap = (testPodValuesMap.keys sitPodValuesMap.keys).associateWith { key ->
testPodValuesMap.getValue(key) to sitPodValuesMap.getValue(key)
}
val imageVersions = mergedMap.map { (k, v) -> ImageVersions(k, v.first, v.second) }
println(imageVersions.joinToString("\n"))
哪個列印
ImageVersions(apiName=api-car-v1, testPodVersion=0.0.118, sitPodVersion=0.0.119)
ImageVersions(apiName=api-dog-v1, testPodVersion=0.0.11, sitPodVersion=0.0.12)
ImageVersions(apiName=api-plane-v1, testPodVersion=0.0.36, sitPodVersion=0.0.37)
uj5u.com熱心網友回復:
作為第一步,我將從兩個串列中提取apiNames:
val apiNames = list1.map { it.replace("value from ", "").split("[=:]".toRegex())[1] }
.plus(list2.map { it.replace("value from ", "").split("[=:]".toRegex())[1] })
.distinct()
然后我將通過遍歷apiNames 來創建ImageVersions實體:
val result = apiNames
.map { apiName ->
ImageVersions(
apiName,
(list1.firstOrNull { it.contains(apiName) } ?: "").split(":")[1],
(list2.firstOrNull { it.contains(apiName) } ?: "").split(":")[1]
)
}
.toList()
究其原因首先提取apiNames是,apiNames兩個串列中的一個缺失仍將在最終結果中結束。
科特林游樂場
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/368394.html
標籤:科特林
