Skip to content

Commit

Permalink
refactor(cocoapods): Factor out YamlNode.toPod()
Browse files Browse the repository at this point in the history
Improve readability and prepare for removing a code redundancy.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau authored and sschuberth committed Jul 17, 2024
1 parent d4f0b5a commit 7e776e3
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions plugins/package-managers/cocoapods/src/main/kotlin/Lockfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ package org.ossreviewtoolkit.plugins.packagemanagers.cocoapods
import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlList
import com.charleskorn.kaml.YamlMap
import com.charleskorn.kaml.YamlNode
import com.charleskorn.kaml.YamlScalar
import com.charleskorn.kaml.yamlList
import com.charleskorn.kaml.yamlMap
import com.charleskorn.kaml.yamlScalar

import org.ossreviewtoolkit.plugins.packagemanagers.cocoapods.Lockfile.CheckoutOption
import org.ossreviewtoolkit.plugins.packagemanagers.cocoapods.Lockfile.Dependency
import org.ossreviewtoolkit.plugins.packagemanagers.cocoapods.Lockfile.Pod

internal data class Lockfile(
val pods: List<Pod>,
Expand All @@ -54,29 +56,7 @@ internal data class Lockfile(

internal fun String.parseLockfile(): Lockfile {
val root = Yaml.default.parseToYamlNode(this).yamlMap

val pods = root.get<YamlList>("PODS")!!.items.map { node ->
when {
node is YamlMap -> {
val (key, value) = node.yamlMap.entries.entries.single()
val (name, version) = parseNameAndVersion(key.content)

Lockfile.Pod(
name = name,
version = version,
dependencies = value.yamlList.items.map {
val (depName, depVersion) = parseNameAndVersion(it.yamlScalar.content)
Lockfile.Pod(depName, depVersion)
}
)
}

else -> {
val (name, version) = parseNameAndVersion(node.yamlScalar.content)
Lockfile.Pod(name, version)
}
}
}
val pods = root.get<YamlList>("PODS")!!.items.map { it.toPod() }

val checkoutOptions = root.get<YamlMap>("CHECKOUT OPTIONS")?.entries.orEmpty().map {
val name = it.key.content
Expand All @@ -98,6 +78,28 @@ internal fun String.parseLockfile(): Lockfile {
return Lockfile(pods, checkoutOptions, dependencies)
}

private fun YamlNode.toPod(): Pod =
when {
this is YamlMap -> {
val (key, value) = yamlMap.entries.entries.single()
val (name, version) = parseNameAndVersion(key.content)

Pod(
name = name,
version = version,
dependencies = value.yamlList.items.map {
val (depName, depVersion) = parseNameAndVersion(it.yamlScalar.content)
Pod(depName, depVersion)
}
)
}

else -> {
val (name, version) = parseNameAndVersion(yamlScalar.content)
Pod(name, version)
}
}

private fun parseNameAndVersion(entry: String): Pair<String, String?> {
val info = entry.split(' ', limit = 2)
val name = info[0]
Expand Down

0 comments on commit 7e776e3

Please sign in to comment.