Skip to content

Commit ee53d0c

Browse files
committed
Fix: Comments not saved if FieldNameFormatter is used
1 parent 9681114 commit ee53d0c

File tree

8 files changed

+48
-16
lines changed

8 files changed

+48
-16
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: ConfigLib
22
author: Exlll
33

4-
version: 2.0.1
4+
version: 2.0.2
55
main: de.exlll.configlib.ConfigLib
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: ConfigLib
22
author: Exlll
33

4-
version: 2.0.1
4+
version: 2.0.2
55
main: de.exlll.configlib.ConfigLib

ConfigLib-Core/src/main/java/de/exlll/configlib/configs/yaml/YamlComments.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.exlll.configlib.configs.yaml;
22

33
import de.exlll.configlib.Comments;
4+
import de.exlll.configlib.format.FieldNameFormatter;
45

56
import java.util.List;
67
import java.util.Map;
@@ -20,18 +21,19 @@ String classCommentsAsString() {
2021
return commentListToString(classComments);
2122
}
2223

23-
Map<String, String> fieldCommentAsStrings() {
24+
Map<String, String> fieldCommentAsStrings(FieldNameFormatter formatter) {
2425
Map<String, List<String>> fieldComments = comments.getFieldComments();
2526
return fieldComments.entrySet().stream()
26-
.map(this::toStringCommentEntry)
27+
.map(e -> toFormattedStringCommentEntry(e, formatter))
2728
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
2829
}
2930

30-
private Map.Entry<String, String> toStringCommentEntry(
31-
Map.Entry<String, List<String>> entry
31+
private Map.Entry<String, String> toFormattedStringCommentEntry(
32+
Map.Entry<String, List<String>> entry, FieldNameFormatter formatter
3233
) {
3334
String fieldComments = commentListToString(entry.getValue());
34-
return new MapEntry<>(entry.getKey(), fieldComments);
35+
String formattedKey = formatter.fromFieldName(entry.getKey());
36+
return new MapEntry<>(formattedKey, fieldComments);
3537
}
3638

3739
private String commentListToString(List<String> comments) {

ConfigLib-Core/src/main/java/de/exlll/configlib/configs/yaml/YamlSource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ private void addDumpLines(List<String> dumpLines) {
122122
}
123123

124124
private void addFieldComment(String dumpLine) {
125-
Map<String, String> map = yamlComments.fieldCommentAsStrings();
125+
Map<String, String> map = yamlComments.fieldCommentAsStrings(
126+
props.getFormatter()
127+
);
126128
for (Map.Entry<String, String> entry : map.entrySet()) {
127129
String prefix = entry.getKey() + ":";
128130
if (dumpLine.startsWith(prefix)) {

ConfigLib-Core/src/test/java/de/exlll/configlib/ValidatorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ class C {
112112
}
113113
Map<String, Object> m = mapOf("l", listOf("s"));
114114
String msg = "Can not set field 'l' with type 'CopyOnWriteArrayList' " +
115-
"to 'List1'.";
115+
"to 'ArrayList'.";
116116
assertIfmCfgExceptionMessage(new A(), m, msg);
117117

118118
m = mapOf("s", setOf("s"));
119119
msg = "Can not set field 's' with type 'ConcurrentSkipListSet' " +
120-
"to 'Set1'.";
120+
"to 'HashSet'.";
121121
assertIfmCfgExceptionMessage(new B(), m, msg);
122122

123123
m = mapOf("m", mapOf(1, "s"));
124124
msg = "Can not set field 'm' with type 'ConcurrentHashMap' " +
125-
"to 'Map1'.";
125+
"to 'HashMap'.";
126126
assertIfmCfgExceptionMessage(new C(), m, msg);
127127

128128
}

ConfigLib-Core/src/test/java/de/exlll/configlib/configs/yaml/YamlConfigurationTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import de.exlll.configlib.annotation.Comment;
66
import de.exlll.configlib.classes.TestClass;
77
import de.exlll.configlib.configs.yaml.YamlConfiguration.YamlProperties;
8+
import de.exlll.configlib.format.FieldNameFormatters;
89
import org.junit.jupiter.api.AfterEach;
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.Test;
@@ -171,6 +172,27 @@ class A extends YamlConfiguration {
171172
assertThat(readConfig(testPath), is(FIELD_COMMENTS_YML));
172173
}
173174

175+
@Test
176+
void saveDumpsFormattedFieldComments() throws IOException {
177+
class A extends YamlConfiguration {
178+
@Comment("aB")
179+
private int aB = 1;
180+
@Comment({"cD", "dC"})
181+
private int cD = 2;
182+
183+
protected A(YamlProperties properties) {
184+
super(testPath, properties);
185+
}
186+
}
187+
188+
YamlProperties properties = YamlProperties.builder()
189+
.setFormatter(FieldNameFormatters.LOWER_UNDERSCORE)
190+
.build();
191+
192+
new A(properties).save();
193+
assertThat(readConfig(testPath), is(FORMATTED_FIELD_COMMENTS_YML));
194+
}
195+
174196
private String readConfig(Path path) throws IOException {
175197
return Files.lines(path).collect(joining("\n"));
176198
}
@@ -194,6 +216,12 @@ private String readConfig(Path path) throws IOException {
194216
"c: 3\n" +
195217
"d: 4";
196218

219+
private static final String FORMATTED_FIELD_COMMENTS_YML = "# aB\n" +
220+
"a_b: 1\n" +
221+
"# cD\n" +
222+
"# dC\n" +
223+
"c_d: 2";
224+
197225
private static final String CLASS_COMMENTS_YML = "# 1\n" +
198226
"\n" +
199227
"# 2\n" +

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,14 @@ public final class DatabasePlugin extends JavaPlugin {
439439
<dependency>
440440
<groupId>de.exlll</groupId>
441441
<artifactId>configlib-bukkit</artifactId>
442-
<version>2.0.1</version>
442+
<version>2.0.2</version>
443443
</dependency>
444444

445445
<!-- for Bungee plugins -->
446446
<dependency>
447447
<groupId>de.exlll</groupId>
448448
<artifactId>configlib-bungee</artifactId>
449-
<version>2.0.1</version>
449+
<version>2.0.2</version>
450450
</dependency>
451451
```
452452
#### Gradle
@@ -458,9 +458,9 @@ repositories {
458458
}
459459
dependencies {
460460
// for Bukkit plugins
461-
compile group: 'de.exlll', name: 'configlib-bukkit', version: '2.0.1'
461+
compile group: 'de.exlll', name: 'configlib-bukkit', version: '2.0.2'
462462
463463
// for Bungee plugins
464-
compile group: 'de.exlll', name: 'configlib-bungee', version: '2.0.1'
464+
compile group: 'de.exlll', name: 'configlib-bungee', version: '2.0.2'
465465
}
466466
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
allprojects {
22
group 'de.exlll'
3-
version '2.0.1'
3+
version '2.0.2'
44
}
55
subprojects {
66
apply plugin: 'java'

0 commit comments

Comments
 (0)