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
132 stars 38 forks source link

Enhancement/Feature Request - addDefault() enforce quote style #65

Closed omega0verride closed 2 years ago

omega0verride commented 2 years ago

It would be great if we could use this same syntax for addDefault() yamlFile.set("quotes.custom", "This is double quote style", QuoteStyle.DOUBLE);

I could not find any documentation on how to get this to work, looking at the code we might have to modify MemoryConfiguration.

A workaround would be combining isSet() and set() but that does not look like a great idea.

Carleslc commented 2 years ago

If you want to set a single default value with a custom quote style you can wrap the value with a QuoteValue like this:

yamlFile.addDefault("default", new QuoteValue<>("test", QuoteStyle.DOUBLE));

If you want to write all default strings with double quotes you can set your own configuration defaults with the setDefaults method, for instance you can use a YamlConfiguration instance for your defaults (instead of the default MemoryConfiguration), so you can use the set method for your defaults:

YamlConfiguration defaults = new YamlConfiguration();

defaults.options().quoteStyleDefaults().setQuoteStyle(String.class, QuoteStyle.DOUBLE);

defaults.set("default", "test"); // String quote style is now DOUBLE by default

defaults.set("plain", "This is plain quote style", QuoteStyle.PLAIN); // You can override the quote style for specific values

yamlFile.setDefaults(defaults);

Output:

default: "test"
plain: This is plain quote style

Of course you can also avoid setting the quoteStyleDefaults and use the set syntax providing the QuoteStyle for each default:

YamlConfiguration defaults = new YamlConfiguration();

defaults.set("quotes.custom", "This is double quote style", QuoteStyle.DOUBLE);

yamlFile.setDefaults(defaults);
quotes:
  custom: "This is double quote style"
Carleslc commented 2 years ago

Note that quote styles of default values will only be applied the first time when the value does not exist in the file, because when the path already exists it is not a default value anymore, so if you want to preserve the quote style you can use the addDefault or setDefaults as usual like in my previous message and then a function to set the quote style of your desired values for already existing values, without changing their values:

private static void wrapValues(YamlFile yamlFile, QuoteStyle quoteStyle, String... paths) {
    for (String path : paths) {
        if (yamlFile.isSet(path)) {
            yamlFile.set(path, yamlFile.get(path), quoteStyle);
        }
    }
}
wrapValues(yamlFile, QuoteStyle.DOUBLE, "default");

Or you can use a custom method to set defaults, probably this is what you mean by a workaround:

private static <T> void setDefault(YamlFile yamlFile, String path, T value, QuoteStyle quoteStyle) {
    yamlFile.set(path, yamlFile.isSet(path) ? yamlFile.get(path) : value, quoteStyle);
}
setDefault(yamlFile, "default", "test", QuoteStyle.DOUBLE);

Instead of using static functions you could also extend YamlFile with your own methods.

omega0verride commented 2 years ago

Thank you @Carleslc! This works like a charm for my requirements: yamlFile.addDefault("default", new QuoteValue<>("test", QuoteStyle.DOUBLE));