Carleslc / Simple-YAML

This Java API provides an easy-to-use way to store data and provide configurations using the YAML format.
https://carleslc.me/Simple-YAML
GNU General Public License v3.0
130 stars 38 forks source link

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

Closed ChimpGamer closed 2 years ago

ChimpGamer commented 2 years ago

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 commented 2 years ago

Having the same Issue.

Carleslc commented 2 years ago

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 commented 2 years ago

Reopen until the 1.8.1 release

Carleslc commented 2 years ago

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 commented 2 years ago

Fixed in 1.8.1