Skip to content

Commit

Permalink
Improve parser
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Jan 13, 2025
1 parent 7fb8c67 commit 0b02252
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import scala.jdk.CollectionConverters.*
object gradleParser {
def parseDependenciesAndPlugins(input: String): (List[Dependency], List[Dependency]) = {
val parsed = Toml.parse(input)
val versionsTable = parsed.getTableOrEmpty("versions")
val librariesTable = parsed.getTableOrEmpty("libraries")
val pluginsTable = parsed.getTableOrEmpty("plugins")
val versionsTable = getTableSafe(parsed, "versions")
val librariesTable = getTableSafe(parsed, "libraries")
val pluginsTable = getTableSafe(parsed, "plugins")

val dependencies = collectEntries(librariesTable, parseDependency(_, versionsTable))
val plugins = collectEntries(pluginsTable, parsePlugin(_, versionsTable))
Expand Down Expand Up @@ -76,6 +76,13 @@ object gradleParser {
versionString.orElse(versionRef).map(Version.apply)
}

private def getTableSafe(table: TomlTable, key: String): TomlTable =
Option
.when(table.contains(key) && table.isTable(key))(table.getTableOrEmpty(key))
.getOrElse(emptyTable)

private val emptyTable: TomlTable = Toml.parse("")

private def getStringSafe(table: TomlTable, key: String): Option[String] =
Option.when(table.contains(key) && table.isString(key))(table.getString(key))
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class gradleParserTest extends FunSuite {
|groovy = "3.0.5"
|[libraries]
|groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy"
|foo = { module = "bar:qux:foo", version = "1" }
|[plugins]
|foo = ""
|""".stripMargin
val obtained = gradleParser.parseDependenciesAndPlugins(input)
assertEquals(obtained, (List.empty, List.empty))
Expand Down

0 comments on commit 0b02252

Please sign in to comment.