Skip to content

Commit

Permalink
Add a method to validate the config spec (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt authored Jul 17, 2024
1 parent c2eb8f1 commit 3d7a560
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public ModConfig registerConfig(ModConfig.Type type, IConfigSpec spec, ModContai
public ModConfig registerConfig(ModConfig.Type type, IConfigSpec spec, ModContainer container, String fileName) {
var lock = locksByMod.computeIfAbsent(container.getModId(), m -> new ReentrantLock());
var modConfig = new ModConfig(type, spec, container, fileName, lock);
spec.validateSpec(modConfig);

trackConfig(modConfig);

if (modConfig.getType() == ModConfig.Type.STARTUP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public interface IConfigSpec {
*/
boolean isEmpty();

/**
* Validate this specification in the context of the given {@code config}.
*
* @param config the configuration this spec is used by
*/
void validateSpec(ModConfig config);

/**
* Checks that a config is correct.
* If this function returns {@code false},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ void testCorrectOnReload() throws Exception {
});
}

@Test
void testValidation() {
var spec = new SimpleConfigSpec() {
@Override
public void validateSpec(ModConfig config) {
throw new IllegalArgumentException("Config is bad.");
}
};
Assertions.assertThatIllegalArgumentException()
.isThrownBy(() -> configTracker.registerConfig(ModConfig.Type.COMMON, spec, modContainer, "test.toml"));
}

private void waitUntil(Runnable assertion) throws InterruptedException {
for (int i = 0; i < 1000; ++i) {
try {
Expand All @@ -192,6 +204,9 @@ public boolean isEmpty() {
return false;
}

@Override
public void validateSpec(ModConfig config) {}

@Override
public boolean isCorrect(UnmodifiableCommentedConfig config) {
return correct(config, true);
Expand Down

0 comments on commit 3d7a560

Please sign in to comment.