Fix: Comments not saved if FieldNameFormatter is used

dev
Exlll 6 years ago
parent 9681114e69
commit ee53d0c609

@ -1,5 +1,5 @@
name: ConfigLib
author: Exlll
version: 2.0.1
version: 2.0.2
main: de.exlll.configlib.ConfigLib

@ -1,5 +1,5 @@
name: ConfigLib
author: Exlll
version: 2.0.1
version: 2.0.2
main: de.exlll.configlib.ConfigLib

@ -1,6 +1,7 @@
package de.exlll.configlib.configs.yaml;
import de.exlll.configlib.Comments;
import de.exlll.configlib.format.FieldNameFormatter;
import java.util.List;
import java.util.Map;
@ -20,18 +21,19 @@ final class YamlComments {
return commentListToString(classComments);
}
Map<String, String> fieldCommentAsStrings() {
Map<String, String> fieldCommentAsStrings(FieldNameFormatter formatter) {
Map<String, List<String>> fieldComments = comments.getFieldComments();
return fieldComments.entrySet().stream()
.map(this::toStringCommentEntry)
.map(e -> toFormattedStringCommentEntry(e, formatter))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}
private Map.Entry<String, String> toStringCommentEntry(
Map.Entry<String, List<String>> entry
private Map.Entry<String, String> toFormattedStringCommentEntry(
Map.Entry<String, List<String>> entry, FieldNameFormatter formatter
) {
String fieldComments = commentListToString(entry.getValue());
return new MapEntry<>(entry.getKey(), fieldComments);
String formattedKey = formatter.fromFieldName(entry.getKey());
return new MapEntry<>(formattedKey, fieldComments);
}
private String commentListToString(List<String> comments) {

@ -122,7 +122,9 @@ final class YamlSource implements ConfigurationSource<YamlConfiguration> {
}
private void addFieldComment(String dumpLine) {
Map<String, String> map = yamlComments.fieldCommentAsStrings();
Map<String, String> map = yamlComments.fieldCommentAsStrings(
props.getFormatter()
);
for (Map.Entry<String, String> entry : map.entrySet()) {
String prefix = entry.getKey() + ":";
if (dumpLine.startsWith(prefix)) {

@ -112,17 +112,17 @@ public class ValidatorTest {
}
Map<String, Object> m = mapOf("l", listOf("s"));
String msg = "Can not set field 'l' with type 'CopyOnWriteArrayList' " +
"to 'List1'.";
"to 'ArrayList'.";
assertIfmCfgExceptionMessage(new A(), m, msg);
m = mapOf("s", setOf("s"));
msg = "Can not set field 's' with type 'ConcurrentSkipListSet' " +
"to 'Set1'.";
"to 'HashSet'.";
assertIfmCfgExceptionMessage(new B(), m, msg);
m = mapOf("m", mapOf(1, "s"));
msg = "Can not set field 'm' with type 'ConcurrentHashMap' " +
"to 'Map1'.";
"to 'HashMap'.";
assertIfmCfgExceptionMessage(new C(), m, msg);
}

@ -5,6 +5,7 @@ import de.exlll.configlib.Configuration;
import de.exlll.configlib.annotation.Comment;
import de.exlll.configlib.classes.TestClass;
import de.exlll.configlib.configs.yaml.YamlConfiguration.YamlProperties;
import de.exlll.configlib.format.FieldNameFormatters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -171,6 +172,27 @@ class YamlConfigurationTest {
assertThat(readConfig(testPath), is(FIELD_COMMENTS_YML));
}
@Test
void saveDumpsFormattedFieldComments() throws IOException {
class A extends YamlConfiguration {
@Comment("aB")
private int aB = 1;
@Comment({"cD", "dC"})
private int cD = 2;
protected A(YamlProperties properties) {
super(testPath, properties);
}
}
YamlProperties properties = YamlProperties.builder()
.setFormatter(FieldNameFormatters.LOWER_UNDERSCORE)
.build();
new A(properties).save();
assertThat(readConfig(testPath), is(FORMATTED_FIELD_COMMENTS_YML));
}
private String readConfig(Path path) throws IOException {
return Files.lines(path).collect(joining("\n"));
}
@ -194,6 +216,12 @@ class YamlConfigurationTest {
"c: 3\n" +
"d: 4";
private static final String FORMATTED_FIELD_COMMENTS_YML = "# aB\n" +
"a_b: 1\n" +
"# cD\n" +
"# dC\n" +
"c_d: 2";
private static final String CLASS_COMMENTS_YML = "# 1\n" +
"\n" +
"# 2\n" +

@ -439,14 +439,14 @@ public final class DatabasePlugin extends JavaPlugin {
<dependency>
<groupId>de.exlll</groupId>
<artifactId>configlib-bukkit</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
</dependency>
<!-- for Bungee plugins -->
<dependency>
<groupId>de.exlll</groupId>
<artifactId>configlib-bungee</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
</dependency>
```
#### Gradle
@ -458,9 +458,9 @@ repositories {
}
dependencies {
// for Bukkit plugins
compile group: 'de.exlll', name: 'configlib-bukkit', version: '2.0.1'
compile group: 'de.exlll', name: 'configlib-bukkit', version: '2.0.2'
// for Bungee plugins
compile group: 'de.exlll', name: 'configlib-bungee', version: '2.0.1'
compile group: 'de.exlll', name: 'configlib-bungee', version: '2.0.2'
}
```

@ -1,6 +1,6 @@
allprojects {
group 'de.exlll'
version '2.0.1'
version '2.0.2'
}
subprojects {
apply plugin: 'java'

Loading…
Cancel
Save