Skip to content

Commit

Permalink
Support empty yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed May 27, 2023
1 parent 1a0f95a commit 0aabb74
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ConfigLoader(
val onFailure: List<(Throwable) -> Unit> = emptyList(),
val decodeMode: DecodeMode = DecodeMode.Lenient,
val useReport: Boolean = false,
val allowEmptyTree: Boolean, // if true then we allow config files to be empty
val allowEmptyTree: Boolean, // if true then we allow the cascaded tree to be empty
val allowUnresolvedSubstitutions: Boolean,
val classLoader: ClassLoader? = null, // if null, then the current context thread loader
val preprocessingIterations: Int = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import com.sksamuel.hoplite.decoder.DotPath
import com.sksamuel.hoplite.parsers.Parser
import com.sksamuel.hoplite.withPath
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.SafeConstructor
import org.yaml.snakeyaml.error.Mark
import org.yaml.snakeyaml.events.AliasEvent
import org.yaml.snakeyaml.events.DocumentEndEvent
Expand All @@ -37,9 +35,14 @@ class YamlParser : Parser {
val events = yaml.parse(reader).iterator()
val stream = TokenStream(events)
require(stream.next().`is`(Event.ID.StreamStart)) { "Expected stream start at ${stream.current().startMark}" }
require(stream.next().`is`(Event.ID.DocumentStart)) { "Expected document start at ${stream.current().startMark}" }
stream.next()
return TokenProduction(stream, source, emptyMap(), DotPath.root).first
return when (stream.next().eventId) {
Event.ID.StreamEnd -> Undefined
Event.ID.DocumentStart -> {
stream.next() // move past the doc start
TokenProduction(stream, source, emptyMap(), DotPath.root).first
}
else -> error { "Expected document start at ${stream.current().startMark}" }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.sksamuel.hoplite.ConfigLoaderBuilder
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class EmptyFileTest : StringSpec({
class EmptySourceTest : StringSpec({

"empty files should be skipped when enabled in config builder" {
data class Test(val foo: String)
Expand All @@ -16,4 +16,15 @@ class EmptyFileTest : StringSpec({

config shouldBe Test("bar")
}

"empty files should support files with only comments" {
data class Test(val foo: String)

val config = ConfigLoaderBuilder.default()
.allowEmptySources()
.build()
.loadConfigOrThrow<Test>("/foo.yml", "/empty_with_comments.yml")

config shouldBe Test("bar")
}
})
2 changes: 2 additions & 0 deletions hoplite-yaml/src/test/resources/empty_with_comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#a:
# b: value

0 comments on commit 0aabb74

Please sign in to comment.