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

addDefault doesn't work since 1.8 if file contains only defaults #64

Closed
ChimpGamer opened this issue Jul 13, 2022 · 5 comments
Closed
Labels

Comments

@ChimpGamer
Copy link

I noticed that since the 1.8 update that addDefault doesn't work any more since the test.yml is empty after being created and saved.
I was able to recreate this in a test project which I uploaded to github: https://github.com/ChimpGamer/SimpleYamlBug/blob/main/src/main/kotlin/nl/chimpgamer/simpleyamlbug/Main.kt

Am I missing something or is it really a bug?

@DxsSucuk
Copy link

Having the same Issue.

@Carleslc Carleslc added the bug label Aug 1, 2022
@Carleslc
Copy link
Owner

Carleslc commented Aug 1, 2022

Yes, it is a bug of version 1.8.

It happens only when there are no keys set (only defaults, without any use of set method).

See the following example. I added a set call to your example.

val yamlFile = YamlFile("test.yml")

yamlFile.createOrLoad()

yamlFile.addDefault("default", "test")

yamlFile.set("key", 0)

yamlFile.save()

test.yml

default: test
key: 0

@Carleslc Carleslc changed the title addDefault doesn't work since 1.8 addDefault doesn't work since 1.8 if file contains only defaults Aug 1, 2022
@Carleslc
Copy link
Owner

Carleslc commented Aug 1, 2022

Reopen until the 1.8.1 release

@Carleslc Carleslc reopened this Aug 1, 2022
@Carleslc
Copy link
Owner

Carleslc commented Aug 1, 2022

Workaround for 1.8:

fun addDefault(yamlFile: YamlFile, path: String, value: Any) {
    if (!yamlFile.isSet(path)) {
        yamlFile.set(path, value)
    }
    yamlFile.addDefault(path, value)
}

fun main() {
    val yamlFile = YamlFile("test.yml")

    yamlFile.createOrLoad()

    addDefault(yamlFile, "default", "test")

    yamlFile.save()
}
default: test

@Carleslc
Copy link
Owner

Carleslc commented Aug 1, 2022

Fixed in 1.8.1

@Carleslc Carleslc closed this as completed Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants