Skip to content

Commit e8debf6

Browse files
committed
feat(config): use HOCON format instead of YAML
The motivation to change the config format come from a limitation of Configurate about YAML format. It is not possible to serialize a YAML content with comments whereas this is a requirement for the project. More details can be found here about Configurate and YAML module: SpongePowered/Configurate#175
1 parent b8a1c3f commit e8debf6

100 files changed

Lines changed: 455 additions & 395 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@
272272
</dependency>
273273
<dependency>
274274
<groupId>org.spongepowered</groupId>
275-
<artifactId>configurate-yaml</artifactId>
275+
<artifactId>configurate-hocon</artifactId>
276276
<version>${configurate.version}</version>
277277
</dependency>
278278
<dependency>

src/patch-place-break/patch-place-break-config/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
</dependency>
8080
<dependency>
8181
<groupId>org.spongepowered</groupId>
82-
<artifactId>configurate-yaml</artifactId>
82+
<artifactId>configurate-hocon</artifactId>
8383
<scope>compile</scope>
8484
</dependency>
8585
<dependency>

src/patch-place-break/patch-place-break-config/src/main/java/fr/djaytan/minecraft/jobsreborn/patchplacebreak/config/ConfigManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
@Singleton
5757
public class ConfigManager {
5858

59-
public static final String CONFIG_FILE_NAME = "config.yml";
59+
public static final String CONFIG_FILE_NAME = "config.conf";
6060

6161
private final ClassLoader classLoader;
6262
private final Path configFile;

src/patch-place-break/patch-place-break-config/src/main/java/fr/djaytan/minecraft/jobsreborn/patchplacebreak/config/serialization/ConfigLoaderFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@
3030

3131
import org.apache.commons.io.IOUtils;
3232
import org.spongepowered.configurate.CommentedConfigurationNode;
33+
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
3334
import org.spongepowered.configurate.loader.ConfigurationLoader;
3435
import org.spongepowered.configurate.loader.HeaderMode;
3536
import org.spongepowered.configurate.objectmapping.ObjectMapper;
3637
import org.spongepowered.configurate.util.NamingSchemes;
37-
import org.spongepowered.configurate.yaml.NodeStyle;
38-
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
3938

4039
import lombok.NonNull;
4140

@@ -66,8 +65,8 @@ private ConfigLoaderFactory() {
6665

6766
String configHeader = createConfigHeader();
6867

69-
return YamlConfigurationLoader.builder().path(configFile).nodeStyle(NodeStyle.BLOCK)
70-
.headerMode(HeaderMode.PRESET)
68+
return HoconConfigurationLoader.builder().path(configFile).prettyPrinting(true)
69+
.emitComments(true).emitJsonCompatible(false).headerMode(HeaderMode.PRESET)
7170
.defaultOptions(
7271
opts -> opts.serializers(builder -> builder.registerAnnotatedObjects(customFactory))
7372
.header(configHeader))

src/patch-place-break/patch-place-break-config/src/main/java/fr/djaytan/minecraft/jobsreborn/patchplacebreak/config/serialization/ConfigSerializer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public final class ConfigSerializer {
5252
* @param object The object to serialize.
5353
* @throws ConfigSerializationException If something prevent the serialization.
5454
*/
55-
public void serialize(@NonNull Path destConfigFile, Object object) {
56-
55+
public void serialize(@NonNull Path destConfigFile, @NonNull Object object)
56+
throws ConfigSerializationException {
5757
try {
5858
ConfigurationLoader<CommentedConfigurationNode> loader =
5959
ConfigLoaderFactory.createLoader(destConfigFile);
@@ -62,7 +62,7 @@ public void serialize(@NonNull Path destConfigFile, Object object) {
6262
throw ConfigSerializationException.failToSerialize();
6363
}
6464

65-
ConfigurationNode configurationNode = loader.createNode(node -> node.set(object));
65+
CommentedConfigurationNode configurationNode = loader.createNode(node -> node.set(object));
6666
loader.save(configurationNode);
6767
} catch (IOException e) {
6868
throw ConfigSerializationException.failToSerialize(e);

src/patch-place-break/patch-place-break-config/src/test/java/fr/djaytan/minecraft/jobsreborn/patchplacebreak/config/ConfigManagerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void withExistingConfigFile_shouldDoNothing() throws IOException {
115115
@DisplayName("With IOException thrown")
116116
void withIOExceptionThrown_shouldThrowException() throws IOException {
117117
// Given
118-
String defaultConfigFileName = "whenCreatingConfig_withIOExceptionThrown.yml";
118+
String defaultConfigFileName = "whenCreatingConfig_withIOExceptionThrown.conf";
119119
Path defaultConfigFile = TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(),
120120
defaultConfigFileName);
121121

@@ -163,7 +163,7 @@ class AndWithDefaultOne {
163163
void beingNotEmpty_thenShouldNotThrow() throws IOException {
164164
// Given
165165
String defaultConfigFileName =
166-
"whenCreatingConfig_withoutExistingOne_andWithDefaultOne_beingNotEmpty.yml";
166+
"whenCreatingConfig_withoutExistingOne_andWithDefaultOne_beingNotEmpty.conf";
167167
Path defaultConfigFile = TestResourcesHelper
168168
.getClassResourceAsAbsolutePath(this.getClass(), defaultConfigFileName);
169169

@@ -182,7 +182,7 @@ void beingNotEmpty_thenShouldNotThrow() throws IOException {
182182
void beingEmpty_thenShouldThrowConfigException() throws IOException {
183183
// Given
184184
String defaultConfigFileName =
185-
"whenCreatingConfig_withoutExistingOne_andWithDefaultOne_beingEmpty.yml";
185+
"whenCreatingConfig_withoutExistingOne_andWithDefaultOne_beingEmpty.conf";
186186
Path defaultConfigFile = TestResourcesHelper
187187
.getClassResourceAsAbsolutePath(this.getClass(), defaultConfigFileName);
188188

@@ -208,7 +208,7 @@ class WhenReadingAndValidatingConfig {
208208
@DisplayName("With nominal config file")
209209
void withNominalConfigFile_shouldSuccess() throws IOException {
210210
// Given
211-
String nominalConfigFileName = "whenReadingAndValidatingConfig_withNominalConfigFile.yml";
211+
String nominalConfigFileName = "whenReadingAndValidatingConfig_withNominalConfigFile.conf";
212212
Path nominalConfigFile = TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(),
213213
nominalConfigFileName);
214214
Files.copy(nominalConfigFile, configFile);
@@ -229,7 +229,7 @@ void withNominalConfigFile_shouldSuccess() throws IOException {
229229
void withMissingRequiredFieldsInConfigFile_shouldThrowException() throws IOException {
230230
// Given
231231
String invalidConfigFileName =
232-
"whenReadingAndValidatingConfig_withMissingRequiredFieldsInConfigFile.yml";
232+
"whenReadingAndValidatingConfig_withMissingRequiredFieldsInConfigFile.conf";
233233
Path invalidConfigFile = TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(),
234234
invalidConfigFileName);
235235
Files.copy(invalidConfigFile, configFile);
@@ -246,7 +246,7 @@ void withMissingRequiredFieldsInConfigFile_shouldThrowException() throws IOExcep
246246
@DisplayName("With invalid config file")
247247
void withInvalidConfigFile_shouldThrowException() throws IOException {
248248
// Given
249-
String invalidConfigFileName = "whenReadingAndValidatingConfig_withInvalidConfigFile.yml";
249+
String invalidConfigFileName = "whenReadingAndValidatingConfig_withInvalidConfigFile.conf";
250250
Path invalidConfigFile = TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(),
251251
invalidConfigFileName);
252252
Files.copy(invalidConfigFile, configFile);
@@ -262,7 +262,7 @@ void withInvalidConfigFile_shouldThrowException() throws IOException {
262262
@DisplayName("With empty config file")
263263
void withEmptyConfigFile_shouldThrowException() throws IOException {
264264
// Given
265-
String emptyConfigFileName = "whenReadingAndValidatingConfig_withEmptyConfigFile.yml";
265+
String emptyConfigFileName = "whenReadingAndValidatingConfig_withEmptyConfigFile.conf";
266266
Path emptyConfigFile =
267267
TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(), emptyConfigFileName);
268268
Files.copy(emptyConfigFile, configFile);

src/patch-place-break/patch-place-break-config/src/test/java/fr/djaytan/minecraft/jobsreborn/patchplacebreak/config/annotated/ConfigValidatingPropertiesTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import fr.djaytan.minecraft.jobsreborn.patchplacebreak.internal.storage.api.properties.DbmsHostProperties;
5959
import fr.djaytan.minecraft.jobsreborn.patchplacebreak.internal.storage.api.properties.DbmsServerProperties;
6060
import jakarta.validation.ConstraintViolation;
61+
import lombok.NonNull;
6162
import nl.jqno.equalsverifier.EqualsVerifier;
6263
import nl.jqno.equalsverifier.Warning;
6364

@@ -230,8 +231,8 @@ class WhenDeserializingFromYaml {
230231
@ParameterizedTest(name = "{index} - {0}")
231232
@MethodSource
232233
@DisplayName("With valid content")
233-
void withValidContent_shouldMatchExpectedValue(String yamlFileName,
234-
ConfigValidatingProperties expectedValue) {
234+
void withValidContent_shouldMatchExpectedValue(@NonNull String yamlFileName,
235+
@NonNull ConfigValidatingProperties expectedValue) {
235236
// Given
236237
Path yamlFile =
237238
TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(), yamlFileName);
@@ -244,25 +245,25 @@ void withValidContent_shouldMatchExpectedValue(String yamlFileName,
244245
assertThat(optionalConfigValidatingProperties).isPresent().get().isEqualTo(expectedValue);
245246
}
246247

247-
private Stream<Arguments> withValidContent_shouldMatchExpectedValue() {
248+
private @NonNull Stream<Arguments> withValidContent_shouldMatchExpectedValue() {
248249
return Stream.of(
249-
Arguments.of(Named.of("With valid values", "whenDeserializing_withValidValues.yml"),
250+
Arguments.of(Named.of("With valid values", "whenDeserializing_withValidValues.conf"),
250251
ConfigValidatingProperties
251252
.of(DataSourceValidatingProperties.of(DataSourceType.MYSQL, "patch_place_break",
252253
DbmsServerValidatingProperties.of(
253254
DbmsHostValidatingProperties.of("example.com", 1234, true),
254255
CredentialsValidatingProperties.of("foo", "bar"), "patch_database"),
255256
ConnectionPoolValidatingProperties.of(60000, 10)))),
256257
Arguments.of(
257-
Named.of("With unexpected field", "whenDeserializing_withUnexpectedField.yml"),
258+
Named.of("With unexpected field", "whenDeserializing_withUnexpectedField.conf"),
258259
ConfigValidatingProperties
259260
.of(DataSourceValidatingProperties.of(DataSourceType.MYSQL, "patch_place_break",
260261
DbmsServerValidatingProperties.of(
261262
DbmsHostValidatingProperties.of("example.com", 1234, true),
262263
CredentialsValidatingProperties.of("foo", "bar"), "patch_database"),
263264
ConnectionPoolValidatingProperties.of(60000, 10)))),
264265
Arguments.of(
265-
Named.of("With 'isValidated' field", "whenDeserializing_withIsValidatedField.yml"),
266+
Named.of("With 'isValidated' field", "whenDeserializing_withIsValidatedField.conf"),
266267
ConfigValidatingProperties
267268
.of(DataSourceValidatingProperties.of(DataSourceType.MYSQL, "patch_place_break",
268269
DbmsServerValidatingProperties.of(
@@ -275,7 +276,7 @@ private Stream<Arguments> withValidContent_shouldMatchExpectedValue() {
275276
@DisplayName("With empty content")
276277
void withEmptyContent_shouldGenerateNullValue() {
277278
// Given
278-
String yamlFileName = "whenDeserializing_withEmptyContent.yml";
279+
String yamlFileName = "whenDeserializing_withEmptyContent.conf";
279280
Path yamlFile =
280281
TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(), yamlFileName);
281282

src/patch-place-break/patch-place-break-config/src/test/java/fr/djaytan/minecraft/jobsreborn/patchplacebreak/config/annotated/ConnectionPoolValidatingPropertiesTest.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import fr.djaytan.minecraft.jobsreborn.patchplacebreak.config.testutils.ValidatorTestWrapper;
5757
import fr.djaytan.minecraft.jobsreborn.patchplacebreak.internal.storage.api.properties.ConnectionPoolProperties;
5858
import jakarta.validation.ConstraintViolation;
59+
import lombok.NonNull;
5960
import nl.jqno.equalsverifier.EqualsVerifier;
6061
import nl.jqno.equalsverifier.Warning;
6162

@@ -209,7 +210,7 @@ void withValidValues_shouldNotGenerateConstraintViolations(long validConnectionT
209210
assertThat(constraintViolations).isEmpty();
210211
}
211212

212-
private Stream<Arguments> withValidValues_shouldNotGenerateConstraintViolations() {
213+
private @NonNull Stream<Arguments> withValidValues_shouldNotGenerateConstraintViolations() {
213214
return Stream.of(Arguments.of(Named.of("Highest allowed value", 600000)),
214215
Arguments.of(Named.of("Lowest allowed value", 1)));
215216
}
@@ -236,7 +237,7 @@ void withInvalidValues_shouldGenerateConstraintViolations(long invalidConnection
236237
.equals("connectionTimeout"));
237238
}
238239

239-
private Stream<Arguments> withInvalidValues_shouldGenerateConstraintViolations() {
240+
private @NonNull Stream<Arguments> withInvalidValues_shouldGenerateConstraintViolations() {
240241
return Stream.of(Arguments.of(Named.of("Null value", 0)),
241242
Arguments.of(Named.of("Too high value", 600001)));
242243
}
@@ -263,7 +264,7 @@ void withValidValues_shouldNotGenerateConstraintViolations(int validPoolSize) {
263264
assertThat(constraintViolations).isEmpty();
264265
}
265266

266-
private Stream<Arguments> withValidValues_shouldNotGenerateConstraintViolations() {
267+
private @NonNull Stream<Arguments> withValidValues_shouldNotGenerateConstraintViolations() {
267268
return Stream.of(Arguments.of(Named.of("Highest allowed value", 100)),
268269
Arguments.of(Named.of("Lowest allowed value", 1)));
269270
}
@@ -290,7 +291,7 @@ void withInvalidValues_shouldGenerateConstraintViolations(int invalidPoolSize) {
290291
.equals("poolSize"));
291292
}
292293

293-
private Stream<Arguments> withInvalidValues_shouldGenerateConstraintViolations() {
294+
private @NonNull Stream<Arguments> withInvalidValues_shouldGenerateConstraintViolations() {
294295
return Stream.of(Arguments.of(Named.of("Null value", 0)),
295296
Arguments.of(Named.of("Too high value", 101)));
296297
}
@@ -305,8 +306,8 @@ class WhenDeserializingFromYaml {
305306
@ParameterizedTest(name = "{index} - {0}")
306307
@MethodSource
307308
@DisplayName("With valid content")
308-
void withValidContent_shouldMatchExpectedValue(String yamlFileName,
309-
ConnectionPoolValidatingProperties expectedValue) {
309+
void withValidContent_shouldMatchExpectedValue(@NonNull String yamlFileName,
310+
@NonNull ConnectionPoolValidatingProperties expectedValue) {
310311
// Given
311312
Path yamlFile =
312313
TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(), yamlFileName);
@@ -321,22 +322,22 @@ void withValidContent_shouldMatchExpectedValue(String yamlFileName,
321322
.isEqualTo(expectedValue);
322323
}
323324

324-
private Stream<Arguments> withValidContent_shouldMatchExpectedValue() {
325+
private @NonNull Stream<Arguments> withValidContent_shouldMatchExpectedValue() {
325326
return Stream.of(
326-
Arguments.of(Named.of("With valid values", "whenDeserializing_withValidValues.yml"),
327+
Arguments.of(Named.of("With valid values", "whenDeserializing_withValidValues.conf"),
327328
ConnectionPoolValidatingProperties.of(60000, 10)),
328329
Arguments.of(
329-
Named.of("With unexpected field", "whenDeserializing_withUnexpectedField.yml"),
330+
Named.of("With unexpected field", "whenDeserializing_withUnexpectedField.conf"),
330331
ConnectionPoolValidatingProperties.of(60000, 10)),
331332
Arguments.of(
332-
Named.of("With 'isValidated' field", "whenDeserializing_withIsValidatedField.yml"),
333+
Named.of("With 'isValidated' field", "whenDeserializing_withIsValidatedField.conf"),
333334
ConnectionPoolValidatingProperties.of(60000, 10)));
334335
}
335336

336337
@ParameterizedTest(name = "{index} - {0}")
337338
@MethodSource
338339
@DisplayName("With invalid content")
339-
void withInvalidContent_shouldThrowException(String yamlFileName) {
340+
void withInvalidContent_shouldThrowException(@NonNull String yamlFileName) {
340341
// Given
341342
Path yamlFile =
342343
TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(), yamlFileName);
@@ -350,19 +351,19 @@ void withInvalidContent_shouldThrowException(String yamlFileName) {
350351
.hasCauseExactlyInstanceOf(SerializationException.class);
351352
}
352353

353-
private Stream<Arguments> withInvalidContent_shouldThrowException() {
354+
private @NonNull Stream<Arguments> withInvalidContent_shouldThrowException() {
354355
return Stream.of(
355356
Arguments.of(Named.of("With missing 'connectionTimeout' field",
356-
"whenDeserializing_withMissingConnectionTimeoutField.yml")),
357+
"whenDeserializing_withMissingConnectionTimeoutField.conf")),
357358
Arguments.of(Named.of("With missing 'poolSize' field",
358-
"whenDeserializing_withMissingPoolSizeField.yml")));
359+
"whenDeserializing_withMissingPoolSizeField.conf")));
359360
}
360361

361362
@Test
362363
@DisplayName("With empty content")
363364
void withEmptyContent_shouldGenerateNullValue() {
364365
// Given
365-
String yamlFileName = "whenDeserializing_withEmptyContent.yml";
366+
String yamlFileName = "whenDeserializing_withEmptyContent.conf";
366367
Path yamlFile =
367368
TestResourcesHelper.getClassResourceAsAbsolutePath(this.getClass(), yamlFileName);
368369

0 commit comments

Comments
 (0)