Skip to content

Commit 2124238

Browse files
committed
Bunch of changes
- fixed PROP Format writer and reader - enhance test so it actually tests for something - rename test specs so it can bare fit on the main focus of what is "spec-ing"
1 parent d571d50 commit 2124238

File tree

19 files changed

+212
-209
lines changed

19 files changed

+212
-209
lines changed

build.gradle

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ println("Gradle Java: ${JavaVersion.current()}")
2323
println("Building project: ${properties.simplename}")
2424
println("Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}, Encoding: ${System.getProperty "file.encoding"}")
2525

26-
configurations {
27-
library
28-
}
29-
30-
sourceSets {
31-
// SHADOW
32-
// test.compileClasspath += configurations.shadow
33-
// test.runtimeClasspath += configurations.shadow
34-
35-
// LIBRARY
36-
main.compileClasspath += configurations.library
37-
test.compileClasspath += configurations.library
38-
test.runtimeClasspath += configurations.library
39-
}
40-
4126
repositories {
4227
google()
4328
mavenCentral()
@@ -46,12 +31,9 @@ repositories {
4631
}
4732

4833
dependencies {
49-
implementation 'it.unimi.dsi:fastutil:8.5.13'
50-
compileOnly 'com.github.WaterMediaTeam:modloaders:1.0.1'
51-
compileOnly "net.fabricmc:fabric-loader:0.14.19"
5234

53-
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
5435
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
36+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
5537
}
5638

5739
// Process target resources with mod info

src/main/java/org/omegaconfig/ConfigSpec.java

Lines changed: 87 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,18 @@ public void setDirty(boolean dirty) {
121121
this.dirty = dirty;
122122
}
123123

124-
void load() throws IOException {
124+
boolean load() throws IOException {
125125
if (!this.filePath.toFile().exists()) {
126-
this.save();
126+
return false;
127127
}
128-
IFormatReader reader = this.format.createReader(this.filePath);
129-
this.load(this, reader);
130-
reader.close();
128+
try {
129+
IFormatReader reader = this.format.createReader(this.filePath);
130+
this.load(this, reader);
131+
reader.close();
132+
} catch (Exception e) {
133+
return false;
134+
}
135+
return true;
131136
}
132137

133138
private void load(ConfigGroup group, IFormatReader reader) {
@@ -166,92 +171,97 @@ void save() throws IOException {
166171

167172
private void save(ConfigGroup group, IFormatWriter writer) {
168173
for (IConfigField<?, ?> field: group.getFields()) {
169-
for (String c: field.comments()) {
170-
writer.write(c);
171-
}
172-
173-
if (field instanceof ConfigGroup g) {
174-
writer.push(g.name());
175-
this.save(g, writer);
176-
writer.pop();
177-
continue;
178-
}
174+
try {
175+
for (String c: field.comments()) {
176+
writer.write(c);
177+
}
179178

180-
if (field instanceof BaseNumberField<?> numberField) {
181-
// SET MATH ALLOW COMMENTS
182-
if (numberField.math()) {
183-
writer.write(COMMENT_ALLOWS_MATH + (numberField.strictMath() ? COMMENT_ALLOWS_MATH_STRICT : ""));
179+
if (field instanceof ConfigGroup g) {
180+
writer.push(g.name());
181+
this.save(g, writer);
182+
writer.pop();
183+
continue;
184184
}
185185

186-
// SET MIN/MAX VALUE COMMENTS
187-
String min = numberField.minValueString();
188-
String max = numberField.maxValueString();
186+
if (field instanceof BaseNumberField<?> numberField) {
187+
// SET MATH ALLOW COMMENTS
188+
if (numberField.math()) {
189+
writer.write(COMMENT_ALLOWS_MATH + (numberField.strictMath() ? COMMENT_ALLOWS_MATH_STRICT : ""));
190+
}
189191

190-
if (min == null && max != null) {
191-
writer.write(String.format(COMMENT_MUST_BE_GREATER_THAN, max));
192-
} else if (min != null && max == null) {
193-
writer.write(String.format(COMMENT_MUST_BE_LESS_THAN, min));
194-
} else if (min != null) {
195-
writer.write(String.format(COMMENT_MUST_BE_IN_RANGE, min, max));
196-
}
197-
}
192+
// SET MIN/MAX VALUE COMMENTS
193+
String min = numberField.minValueString();
194+
String max = numberField.maxValueString();
198195

199-
if (field instanceof StringField stringField) {
200-
// SET STRING STARTWITH COMMENT
201-
if (stringField.startsWith.isEmpty() && !stringField.endsWith.isEmpty()) {
202-
writer.write(String.format(COMMENT_MUST_END_WITH, stringField.endsWith));
203-
} else if (!stringField.startsWith.isEmpty() && stringField.endsWith.isEmpty()) {
204-
writer.write(String.format(COMMENT_MUST_START_WITH, stringField.startsWith));
205-
} else if (!stringField.startsWith.isEmpty()) {
206-
writer.write(String.format(COMMENT_MUST_START_AND_END_WITH, stringField.startsWith, stringField.endsWith));
196+
if (min == null && max != null) {
197+
writer.write(String.format(COMMENT_MUST_BE_GREATER_THAN, max));
198+
} else if (min != null && max == null) {
199+
writer.write(String.format(COMMENT_MUST_BE_LESS_THAN, min));
200+
} else if (min != null) {
201+
writer.write(String.format(COMMENT_MUST_BE_IN_RANGE, min, max));
202+
}
207203
}
208204

209-
// SET STRING CONDITION AND MODE COMMENT
210-
if (stringField.condition != null) {
211-
String comment = switch (stringField.mode) {
212-
case CONTAINS -> COMMENT_MUST_CONTAIN;
213-
case EQUALS -> COMMENT_MUST_EQUALS;
214-
case REGEX -> COMMENT_MUST_MATCH;
215-
case NOT_CONTAINS -> COMMENT_MUST_NOT_CONTAIN;
216-
case NOT_EQUALS -> COMMENT_MUST_NOT_EQUALS;
217-
case NOT_REGEX -> COMMENT_MUST_NOT_MATCH;
218-
};
219-
writer.write(String.format(comment, stringField.condition));
220-
}
205+
if (field instanceof StringField stringField) {
206+
// SET STRING STARTWITH COMMENT
207+
if (stringField.startsWith.isEmpty() && !stringField.endsWith.isEmpty()) {
208+
writer.write(String.format(COMMENT_MUST_END_WITH, stringField.endsWith));
209+
} else if (!stringField.startsWith.isEmpty() && stringField.endsWith.isEmpty()) {
210+
writer.write(String.format(COMMENT_MUST_START_WITH, stringField.startsWith));
211+
} else if (!stringField.startsWith.isEmpty()) {
212+
writer.write(String.format(COMMENT_MUST_START_AND_END_WITH, stringField.startsWith, stringField.endsWith));
213+
}
221214

222-
// SET STRING ALLOW EMPTY COMMENT
223-
writer.write(stringField.allowEmpty ? COMMENT_ALLOW_EMPTY : COMMENT_DENY_EMPTY);
224-
}
215+
// SET STRING CONDITION AND MODE COMMENT
216+
if (stringField.condition != null) {
217+
String comment = switch (stringField.mode) {
218+
case CONTAINS -> COMMENT_MUST_CONTAIN;
219+
case EQUALS -> COMMENT_MUST_EQUALS;
220+
case REGEX -> COMMENT_MUST_MATCH;
221+
case NOT_CONTAINS -> COMMENT_MUST_NOT_CONTAIN;
222+
case NOT_EQUALS -> COMMENT_MUST_NOT_EQUALS;
223+
case NOT_REGEX -> COMMENT_MUST_NOT_MATCH;
224+
};
225+
writer.write(String.format(comment, stringField.condition));
226+
}
225227

226-
if (field instanceof ListField<?> listField) {
227-
// SET LIST ALLOW EMPTY AND UNIQUE COMMENT
228-
writer.write((listField.allowEmpty
229-
? COMMENT_ARRAY_ALLOW_EMPTY
230-
: COMMENT_ARRAY_DENY_EMPTY)
231-
+ (listField.unique ? " " + COMMENT_ARRAY_VALUES_MUST_BE_UNIQUE : "")
232-
);
228+
// SET STRING ALLOW EMPTY COMMENT
229+
writer.write(stringField.allowEmpty ? COMMENT_ALLOW_EMPTY : COMMENT_DENY_EMPTY);
230+
}
233231

234-
// SET LIST LIMIT COMMENT
235-
writer.write(String.format(COMMENT_ARRAY_SIZE_MUST_BE_GREATER_THAN, listField.limit));
236-
}
232+
if (field instanceof ListField<?> listField) {
233+
// SET LIST ALLOW EMPTY AND UNIQUE COMMENT
234+
writer.write((listField.allowEmpty
235+
? COMMENT_ARRAY_ALLOW_EMPTY
236+
: COMMENT_ARRAY_DENY_EMPTY)
237+
+ (listField.unique ? " " + COMMENT_ARRAY_VALUES_MUST_BE_UNIQUE : "")
238+
);
237239

238-
if (field instanceof EnumField<?> enumField) {
239-
writer.write(String.format(COMMENT_ENUM_VALID_VALUES, Arrays.toString(enumField.type().getEnumConstants())));
240-
}
240+
// SET LIST LIMIT COMMENT
241+
writer.write(String.format(COMMENT_ARRAY_SIZE_MUST_BE_GREATER_THAN, listField.limit));
242+
}
241243

242-
if (field instanceof PathField pathField) {
243-
writer.write(pathField.runtimePath ? COMMENT_PATH_RUNTIME : COMMENT_PATH_STATIC);
244-
if (pathField.fileExists) {
245-
writer.write(COMMENT_PATH_FILE_EXISTS);
244+
if (field instanceof EnumField<?> enumField) {
245+
writer.write(String.format(COMMENT_ENUM_VALID_VALUES, Arrays.toString(enumField.type().getEnumConstants())));
246246
}
247-
}
248247

249-
if (field instanceof ListField<?> listField) {
250-
writer.write(field.name(), OmegaConfig.tryEncode(listField.get().toArray(), field.type(), field.subType()), field.type(), field.subType());
251-
} else if (field instanceof ArrayField<?> arrayField) {
252-
writer.write(field.name(), OmegaConfig.tryEncode(arrayField.get(), field.type(), field.subType()), field.type(), field.subType());
253-
} else {
254-
writer.write(field.name(), OmegaConfig.tryEncode(field.get(), field.subType()), field.type(), field.subType());
248+
if (field instanceof PathField pathField) {
249+
writer.write(pathField.runtimePath ? COMMENT_PATH_RUNTIME : COMMENT_PATH_STATIC);
250+
if (pathField.fileExists) {
251+
writer.write(COMMENT_PATH_FILE_EXISTS);
252+
}
253+
}
254+
255+
256+
if (field instanceof ListField<?> listField) {
257+
writer.write(field.name(), OmegaConfig.tryEncode(listField.get().toArray(), field.type(), field.subType()), field.type(), field.subType());
258+
} else if (field instanceof ArrayField<?> arrayField) {
259+
writer.write(field.name(), OmegaConfig.tryEncode(arrayField.get(), field.type(), field.subType()), field.type(), field.subType());
260+
} else {
261+
writer.write(field.name(), OmegaConfig.tryEncode(field.get(), field.subType()), field.type(), field.subType());
262+
}
263+
} catch (Exception e) {
264+
throw new RuntimeException("Failed to save field '" + field.id() + "' in config spec '" + this.name() + "'", e);
255265
}
256266
}
257267
this.dirty = false;

src/main/java/org/omegaconfig/OmegaConfig.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public class OmegaConfig {
3030
public static Path getPath() { return CONFIG_PATH; }
3131
public static void setPath(Path configPath) { CONFIG_PATH = configPath; }
3232

33+
public static boolean isRegistered(String name) {
34+
synchronized (SPECS) {
35+
return SPECS.containsKey(name);
36+
}
37+
}
38+
3339
public static ConfigSpec register(ConfigSpec spec) {
3440
synchronized (SPECS) {
3541
SPECS.put(spec.name(), spec);
@@ -343,28 +349,12 @@ static void run() {
343349
while (!Thread.interrupted()) {
344350
synchronized (SPECS) {
345351
for (ConfigSpec spec: SPECS.values()) {
346-
if (!spec.isLoaded()) {
347-
try {
348-
spec.load();
349-
} catch (Exception e) {
350-
e.printStackTrace();
351-
spec.setDirty(true); // mark as dirty to re-save
352-
}
353-
}
354-
if (spec.isDirty()) {
355-
try {
356-
spec.save();
357-
358-
} catch (Exception e) {
359-
throw new IllegalStateException("Failed to save spec '" + spec.name() + "'", e);
360-
}
361-
}
362-
if (spec.isReload()) {
363-
try {
364-
spec.load();
365-
} catch (Exception e) {
366-
throw new IllegalStateException("Failed to reload spec '" + spec.name() + "'", e);
367-
}
352+
try {
353+
if (!spec.isLoaded() && !spec.load()) spec.save();
354+
if (spec.isDirty()) spec.save();
355+
if (spec.isReload()) spec.load();
356+
} catch (Exception e) {
357+
throw new IllegalStateException("Failed to process spec '" + spec.name() + "', spec flags=[loaded=" + spec.isLoaded() + ", dirty=" + spec.isDirty() + ", reload=" + spec.isReload() + "]", e);
368358
}
369359
}
370360
}

src/main/java/org/omegaconfig/api/IConfigField.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public interface IConfigField<T, S> extends Consumer<T>, Supplier<T> {
1818
*/
1919
default String id() {
2020
final ConfigGroup group = this.group();
21+
final String groupId = group != null ? group.id() : "";
2122
final String name = this.name();
22-
final String parentName = group != null ? (group.id() + ".") : ""; // spec:parent.config or spec:config
23+
final String parentName = group != null ? (groupId + (groupId.endsWith(":") ? "" : ".")) : ""; // spec:parent.config or spec:config
2324
return parentName + name + (group != null ? "" : ":");
2425
}
2526

src/main/java/org/omegaconfig/impl/fields/ArrayField.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.lang.reflect.Array;
66
import java.lang.reflect.Field;
77
import java.util.Arrays;
8-
import java.util.Collections;
98
import java.util.Set;
109
import java.util.function.Predicate;
1110

src/main/java/org/omegaconfig/impl/formats/PROPFormat.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,28 @@ public void write(String comment) {
120120
@Override
121121
public void write(String fieldName, String value, Class<?> type, Class<?> subType) {
122122
this.data.append(Tools.concat("", "", FORMAT_KEY_GROUP_SPLIT, groups))
123+
.append(groups.isEmpty() ? fieldName : FORMAT_KEY_GROUP_SPLIT + fieldName)
123124
.append(FORMAT_KEY_DEF_SPLIT)
124125
.append(value)
125126
.append(FORMAT_KEY_BREAKLINE);
126127
}
127128

128129
@Override
129130
public void write(String fieldName, String[] values, Class<?> type, Class<?> subType) {
130-
131+
String arrayString = "[" + String.join(", ", values) + "]";
132+
this.data.append(Tools.concat("", "", FORMAT_KEY_GROUP_SPLIT, groups))
133+
.append(groups.isEmpty() ? fieldName : FORMAT_KEY_GROUP_SPLIT + fieldName)
134+
.append(FORMAT_KEY_DEF_SPLIT)
135+
.append(arrayString)
136+
.append(FORMAT_KEY_BREAKLINE);
131137
}
132138

133139
@Override
134140
// TODO: implement a check for re-pushing a wrote group
135141
public void push(String groupName) {
136142
this.groups.push(groupName);
137-
this.data.append(Tools.concat("", "", FORMAT_KEY_GROUP_SPLIT, groups));
143+
this.data.append(FORMAT_KEY_BREAKLINE);
144+
this.write("Begin of group " + Tools.concat("", "", FORMAT_KEY_GROUP_SPLIT, groups));
138145
}
139146

140147
@Override

src/main/java/org/omegaconfig/impl/formats/TOMLFormat.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void write(String fieldName, String[] values, Class<?> type, Class<?> sub
7474
ensureTableHeader();
7575

7676
// Write comments
77-
for (String comment : this.comments) {
77+
for (String comment: this.comments) {
7878
this.buffer.append("# ").append(comment).append("\n");
7979
}
8080
this.comments.clear();
@@ -120,7 +120,7 @@ public void close() throws IOException {
120120
private void ensureTableHeader() {
121121
String tableName = buildTableName();
122122
if (!tableName.equals(currentTable) || !tableHeaderWritten) {
123-
if (buffer.length() > 0) {
123+
if (!buffer.isEmpty()) {
124124
buffer.append("\n");
125125
}
126126
if (!tableName.isEmpty()) {
@@ -296,12 +296,11 @@ private int parseKeyValue(char[] data, int start) throws IOException {
296296
if (i >= len || data[i] != '=') {
297297
throw new IOException("Expected '=' after key");
298298
}
299-
i++;
300299

301300
// Skip whitespace
302-
while (i < len && Character.isWhitespace(data[i])) {
301+
do {
303302
i++;
304-
}
303+
} while (i < len && Character.isWhitespace(data[i]));
305304

306305
// Parse value
307306
String fullKey = buildFullKey(key.toString());
@@ -345,7 +344,6 @@ private int parseKey(char[] data, int start, StringBuilder key) throws IOExcepti
345344

346345
private int parseValue(char[] data, int start, String key) throws IOException {
347346
int i = start;
348-
int len = data.length;
349347
char c = data[i];
350348

351349
// String
@@ -491,9 +489,8 @@ private int parseArrayElement(char[] data, int start, StringBuilder element) thr
491489

492490
// String
493491
if (c == '"' || c == '\'') {
494-
char quote = c;
495492
i++;
496-
while (i < len && data[i] != quote) {
493+
while (i < len && data[i] != c) {
497494
if (data[i] == '\\' && i + 1 < len) {
498495
i++;
499496
element.append(unescapeChar(data[i]));
@@ -556,12 +553,11 @@ private int parseInlineTable(char[] data, int start, String key) throws IOExcept
556553
if (i >= len || data[i] != '=') {
557554
throw new IOException("Expected '=' in inline table");
558555
}
559-
i++;
560556

561557
// Skip whitespace
562-
while (i < len && Character.isWhitespace(data[i])) {
558+
do {
563559
i++;
564-
}
560+
} while (i < len && Character.isWhitespace(data[i]));
565561

566562
// Parse value
567563
String fullKey = key + "." + subKey;

0 commit comments

Comments
 (0)