Skip to content

Commit 8ee435f

Browse files
committed
This requires a huge rework to add arrays/list compatibility on decoder side
1 parent 49a3836 commit 8ee435f

File tree

9 files changed

+277
-123
lines changed

9 files changed

+277
-123
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private void load(ConfigGroup group, IFormatReader reader) {
119119
}
120120
}
121121

122-
void save() throws IOException {
122+
public void save() throws IOException {
123123
IFormatWriter writer = this.format.createWritter(this.filePath);
124124
this.save(this, writer);
125125
}
@@ -206,6 +206,10 @@ private void save(ConfigGroup group, IFormatWriter writer) {
206206
}
207207
}
208208

209+
if (field instanceof ListField<?> listField) {
210+
writer.write(field.name(), listField.get().toArray());
211+
}
212+
209213
writer.write(field.name(), OmegaConfig.tryEncode(field.get(), field.subType()));
210214
}
211215
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ public class OmegaConfig {
2828
public static Path getPath() { return CONFIG_PATH; }
2929
public static void setPath(Path configPath) { CONFIG_PATH = configPath; }
3030

31-
public static void register(ConfigSpec spec) {
31+
public static ConfigSpec register(ConfigSpec spec) {
3232
synchronized (SPECS) {
3333
SPECS.put(spec.name(), spec);
3434
}
35+
36+
return spec;
3537
}
3638

37-
public static void register(Class<?> clazz) {
39+
public static ConfigSpec register(Class<?> clazz) {
3840
Objects.requireNonNull(clazz, "Spec class cannot be null");
3941

4042
// RETRIEVE ANNOTATION
@@ -45,16 +47,15 @@ public static void register(Class<?> clazz) {
4547
register$iterateClass(clazz, clazz, builder, true);
4648

4749
// PUT ON OUR REGISTER
48-
register(builder.build());
50+
return register(builder.build());
4951
}
5052

51-
public static void register(Object instance) {
53+
public static ConfigSpec register(Object instance) {
5254
Objects.requireNonNull(instance, "Spec instance cannot be null");
5355

5456
// RETRIEVE ANNOTATION
5557
if (instance instanceof Class<?> clazz) {
56-
register(clazz);
57-
return;
58+
return register(clazz);
5859
}
5960
final Class<?> specClass = instance.getClass();
6061
final Spec spec = Tools.specOf(specClass);
@@ -66,7 +67,7 @@ public static void register(Object instance) {
6667
register$iterateClass(instance, specClass, builder, false);
6768

6869
// PUT ON OUR REGISTER
69-
register(builder.build());
70+
return register(builder.build());
7071
}
7172

7273
private static void register$iterateClass(Object instance, Class<?> specClass, ConfigSpec.SpecBuilder builder, boolean isStatic) {

src/main/java/org/omegaconfig/Tools.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import org.omegaconfig.api.annotations.Spec;
44

55
import java.io.Closeable;
6+
import java.io.FileInputStream;
67
import java.io.IOException;
78
import java.lang.reflect.Field;
89
import java.lang.reflect.TypeVariable;
10+
import java.nio.file.Path;
911
import java.util.Arrays;
1012
import java.util.Collection;
1113
import java.util.Iterator;
@@ -183,4 +185,10 @@ public static boolean contains(char c, char[] expected) {
183185
}
184186
return false;
185187
}
188+
189+
public static byte[] readAllBytes(Path path) throws IOException {
190+
try (var in = new FileInputStream(path.toFile());) {
191+
return in.readAllBytes();
192+
}
193+
}
186194
}

src/main/java/org/omegaconfig/api/formats/IFormatWriter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ public interface IFormatWriter extends Closeable {
1919
* @param fieldName the name of the field
2020
* @param value the boolean value to write
2121
*/
22-
void write(String fieldName, String value);
22+
void write(String fieldName, String value, Class<?> type, Class<?> subType);
23+
24+
/**
25+
* Write a field on the current position.
26+
* Writing is sequential, so the order of the write calls fields is important.
27+
*
28+
* @param fieldName the name of the field
29+
* @param values values to write
30+
*/
31+
void write(String fieldName, String[] values, Class<?> type, Class<?> subType);
2332

2433
/**
2534
* Creates a new group on the current position of the file.

0 commit comments

Comments
 (0)