#450 Fix YAML export of enum values

- Move writing logic to PropertyType
- Remove unused double property type
- Add sample enum property type to tests
This commit is contained in:
ljacqu
2016-01-31 10:49:30 +01:00
parent e747dfeb7f
commit fbd5265a0b
13 changed files with 69 additions and 109 deletions
@@ -171,19 +171,7 @@ public class NewSetting {
}
private <T> String toYaml(Property<T> property, int indent, Yaml simpleYaml, Yaml singleQuoteYaml) {
T value = property.getFromFile(configuration);
String representation = property.hasSingleQuotes()
? singleQuoteYaml.dump(value)
: simpleYaml.dump(value);
// If the property is a non-empty list we need to append a new line because it will be
// something like the following, which requires a new line:
// - 'item 1'
// - 'second item in list'
if (property.isList() && !((List) value).isEmpty()) {
representation = "\n" + representation;
}
String representation = property.toYaml(configuration, simpleYaml, singleQuoteYaml);
return join("\n" + indent(indent), representation.split("\\n"));
}
@@ -197,7 +185,7 @@ public class NewSetting {
}
private File buildMessagesFileFromCode(String language) {
return new File(pluginFolder.getName(),
return new File(pluginFolder,
makePath("messages", "messages_" + language + ".yml"));
}
@@ -1,6 +1,7 @@
package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.FileConfiguration;
import org.yaml.snakeyaml.Yaml;
/**
* Enum property type.
@@ -32,8 +33,8 @@ class EnumPropertyType<E extends Enum<E>> extends PropertyType<E> {
}
@Override
public boolean hasSingleQuotes() {
return true;
public String toYaml(E value, Yaml simpleYaml, Yaml singleQuoteYaml) {
return singleQuoteYaml.dump(value.name());
}
private E mapToEnum(String value) {
@@ -1,6 +1,7 @@
package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.FileConfiguration;
import org.yaml.snakeyaml.Yaml;
import java.util.Arrays;
import java.util.List;
@@ -102,24 +103,15 @@ public class Property<T> {
}
/**
* Return whether the property should be represented wrapped in single quotes in YAML.
* The YAML format allows both using single quotes and not for all types but for certain
* types one representation makes more sense. Typically we wrap string-like types in
* single quotes and all other ones not.
* Format the property's value as YAML.
*
* @return True if single quotes should be used, false otherwise
* @param configuration The file configuration
* @param simpleYaml YAML object (default)
* @param singleQuoteYaml YAML object using single quotes
* @return The generated YAML
*/
public boolean hasSingleQuotes() {
return type.hasSingleQuotes();
}
/**
* Return whether the property has a list type.
*
* @return True if the property type is a list, false otherwise
*/
public boolean isList() {
return type.isList();
public String toYaml(FileConfiguration configuration, Yaml simpleYaml, Yaml singleQuoteYaml) {
return type.toYaml(getFromFile(configuration), simpleYaml, singleQuoteYaml);
}
// -----
@@ -1,6 +1,7 @@
package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.FileConfiguration;
import org.yaml.snakeyaml.Yaml;
import java.util.List;
@@ -13,7 +14,6 @@ import java.util.List;
public abstract class PropertyType<T> {
public static final PropertyType<Boolean> BOOLEAN = new BooleanProperty();
public static final PropertyType<Double> DOUBLE = new DoubleProperty();
public static final PropertyType<Integer> INTEGER = new IntegerProperty();
public static final PropertyType<String> STRING = new StringProperty();
public static final PropertyType<List<String>> STRING_LIST = new StringListProperty();
@@ -39,16 +39,15 @@ public abstract class PropertyType<T> {
}
/**
* Return whether the property type should be wrapped in single quotes in YAML.
* Format the value as YAML.
*
* @return True if single quotes should be used, false if not
* @param value The value to export
* @param simpleYaml YAML object (default)
* @param singleQuoteYaml YAML object set to use single quotes
* @return The generated YAML
*/
public boolean hasSingleQuotes() {
return false;
}
public boolean isList() {
return false;
public String toYaml(T value, Yaml simpleYaml, Yaml singleQuoteYaml) {
return simpleYaml.dump(value);
}
@@ -62,16 +61,6 @@ public abstract class PropertyType<T> {
}
}
/**
* Double property.
*/
private static final class DoubleProperty extends PropertyType<Double> {
@Override
public Double getFromFile(Property<Double> property, FileConfiguration configuration) {
return configuration.getDouble(property.getPath(), property.getDefaultValue());
}
}
/**
* Integer property.
*/
@@ -91,8 +80,8 @@ public abstract class PropertyType<T> {
return configuration.getString(property.getPath(), property.getDefaultValue());
}
@Override
public boolean hasSingleQuotes() {
return true;
public String toYaml(String value, Yaml simpleYaml, Yaml singleQuoteYaml) {
return singleQuoteYaml.dump(value);
}
}
@@ -114,13 +103,13 @@ public abstract class PropertyType<T> {
}
@Override
public boolean hasSingleQuotes() {
return true;
}
@Override
public boolean isList() {
return true;
public String toYaml(List<String> value, Yaml simpleYaml, Yaml singleQuoteYaml) {
String yaml = singleQuoteYaml.dump(value);
// If the property is a non-empty list we need to append a new line because it will be
// something like the following, which requires a new line:
// - 'item 1'
// - 'second item in list'
return value.isEmpty() ? yaml : "\n" + yaml;
}
}