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

Added Configuration Support for Ignoring Empty Document #286

Closed
wants to merge 11 commits into from

Conversation

DasLixou
Copy link

@DasLixou DasLixou commented May 15, 2022

This adds ignoreEmptyDocument to the Configuration (default false)

When true, it doesnt throw an EmptyYamlDocumentException when the document is empty.

Solves #284 (from Discussion #281)

Copy link
Owner

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @DasLixou!

Two things:

  • I have a minor name change suggestion (see comments).
  • Could you please add a test for this?

src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt Outdated Show resolved Hide resolved
src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt Outdated Show resolved Hide resolved
src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt Outdated Show resolved Hide resolved
@DasLixou DasLixou requested a review from charleskorn May 16, 2022 05:24
@DasLixou
Copy link
Author

Ok I’ll add a test later :)

@DasLixou
Copy link
Author

@charleskorn What have i done wrong?

grafik

@charleskorn
Copy link
Owner

@charleskorn What have i done wrong?

grafik

I believe that's just a bug in IntelliJ - does it compile OK from the command line? Try ./gradlew check (this will also run the tests).

@DasLixou
Copy link
Author

That should do it :D

Copy link
Owner

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few suggestions on the tests to verify this behaves as expected and then this should be good to go!

Edit: it also looks like one of the tests is failing: https://github.com/charleskorn/kaml/runs/6479824327?check_suite_focus=true#step:8:66

}

it("expect ignoring empty document because of configuration") {
shouldNotThrowAny { Yaml(configuration = YamlConfiguration(allowReadingEmptyDocument = true)).decodeFromStream<String>(bytes) }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this scenario (reading a non-nullable string), what would you expect to be returned from decodeFromStream? Can we make an assertion that shows the expected behaviour?

@@ -42,5 +44,18 @@ class JvmYamlReadingTest : DescribeSpec({
result shouldBe 123
}
}

describe("reading an empty yaml without throwing an error") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes aren't specific to the JVM, but this file is for tests that only apply to the JVM. Could you please move them to the commonTest source set?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it but the problem is that the common Kotlin version of YAML only has the default variable. If there were an expected function decodeFromStream or so it would work..

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The common Kotlin version has a decodeFromString method that could be used in the tests.

val bytes = ByteArrayInputStream(input.toByteArray(Charsets.UTF_8))

it("expect throwing an error because it's an empty document") {
shouldThrowExactly<EmptyYamlDocumentException> { Yaml.default.decodeFromStream<String>(bytes) }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add tests to cover a couple of other scenarios as well. For example, reading lists, maps and Kotlin classes.

@DasLixou
Copy link
Author

I dont know how, but my last braincell made it working :D - After looking at it a second time it just totally make sense.
Now ill add the other test cases

@DasLixou
Copy link
Author

PROBLEM: When the document is empty, i cant get an object out of YamlInput. Is there any way to get the default object of an Deserialization? @charleskorn

@charleskorn
Copy link
Owner

PROBLEM: When the document is empty, i cant get an object out of YamlInput. Is there any way to get the default object of an Deserialization? @charleskorn

This probably requires changes in kotlinx.serialization. What does the built-in JSON format do if you try to deserialize an empty JSON stream to an object like this?

@DasLixou
Copy link
Author

Screw it. If anyone has more braincells, just go ahead and fork DasLixou/kaml if you want. I'll just check if the file is empty before running Kaml. This system is little more complicated.

@charleskorn
Copy link
Owner

Given the issues discussed above, I'm going to close this as "won't fix".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants