Skip to content

Commit 7cd90a5

Browse files
committed
Make it actually work
1 parent a237cf1 commit 7cd90a5

File tree

8 files changed

+69
-29
lines changed

8 files changed

+69
-29
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"chicken": false,
3+
"cow5": true
4+
}

modules/config-impl/src/main/java/org/polyfrost/oneconfig/api/config/v1/Config.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.polyfrost.oneconfig.api.config.v1.annotations.Include;
3434
import org.polyfrost.polyui.data.PolyImage;
3535

36-
import java.nio.file.Files;
3736
import java.nio.file.Path;
3837
import java.util.ArrayList;
3938
import java.util.Arrays;
@@ -200,7 +199,7 @@ protected void loadFrom(String id) {
200199
if (tree == null) initialize(false);
201200
Tree in = ConfigManager.active().get(id);
202201
if (in == null) return;
203-
tree.overwrite(in, false, true);
202+
tree.overwrite(in, false, true, tree);
204203
}
205204

206205
protected void loadFrom(Path p) {
@@ -212,7 +211,7 @@ protected void loadFrom(Path p) {
212211
return;
213212
}
214213
if (in == null) return;
215-
tree.overwrite(in, false, true);
214+
tree.overwrite(in, false, true, tree);
216215
}
217216

218217
protected Property<?> getProperty(String option) {

modules/config-impl/src/test/java/org/polyfrost/oneconfig/api/config/v1/MainTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,28 @@
2626

2727
package org.polyfrost.oneconfig.api.config.v1;
2828

29-
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import java.io.File;
30+
31+
import static org.junit.jupiter.api.Assertions.*;
3132

3233
public class MainTest {
3334
@org.junit.jupiter.api.Test
3435
void test() {
36+
File configFile = new File("config/test_mod.json");
37+
assertTrue(!configFile.exists() || configFile.delete());
3538
TestConfig config = new TestConfig();
3639
config.initialize(true);
3740
Tree t = config.tree;
3841
assertEquals(t, ConfigManager.active().get(t.getID()));
3942
assertNotNull(t.get("chicken").getMetadata("visualizer"));
43+
assertNull(t.get("reserved:overwritten"));
44+
System.err.println(t);
45+
assertTrue(TestConfig.chicken);
46+
assertFalse(TestConfig.cow5);
47+
config.loadFrom(new File(new File(new File(".").getParentFile(), "config-to-migrate-for-test"), "test_migration_mod.json").toPath());
48+
assertNotNull(t.get("reserved:overwritten"));
49+
assertFalse(TestConfig.chicken);
50+
assertTrue(TestConfig.cow5);
4051
System.err.println(t);
4152
}
4253
}

modules/config/api/config.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Node {
1515
public final fun hasMetadata ()Z
1616
public final fun hashCode ()I
1717
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
18-
public abstract fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZ)V
18+
public abstract fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
1919
public final fun removeMetadata (Ljava/lang/String;)Z
2020
public fun setID (Ljava/lang/String;)V
2121
public fun setTitle (Ljava/lang/String;)V
@@ -71,7 +71,7 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Property : org/polyf
7171
public final fun isArray ()Z
7272
public final fun isPrimitive ()Z
7373
public fun onDisplayChange (Ljava/util/function/Consumer;)V
74-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZ)V
74+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
7575
public static fun recast (Lorg/polyfrost/oneconfig/api/config/v1/Property;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
7676
public fun revaluateDisplay ()V
7777
public final fun set (Ljava/lang/Object;)V
@@ -113,8 +113,8 @@ public class org/polyfrost/oneconfig/api/config/v1/Tree : org/polyfrost/oneconfi
113113
public fun getProp ([Ljava/lang/String;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
114114
public fun onAll (Ljava/util/function/BiConsumer;)V
115115
public fun onAllProps (Ljava/util/function/BiConsumer;)V
116-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZ)V
117-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;ZZ)V
116+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
117+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
118118
public fun put (Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
119119
public fun put ([Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
120120
public fun set (Ljava/lang/String;Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/Node.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public final boolean hasMetadata() {
188188
}
189189

190190
public void overwrite(Node with, boolean preserveMissingOptions) {
191-
overwrite(with, preserveMissingOptions, false);
191+
overwrite(with, preserveMissingOptions, false, null);
192192
}
193193

194194
/**
@@ -201,8 +201,10 @@ public void overwrite(Node with, boolean preserveMissingOptions) {
201201
*
202202
* @param with the node to overwrite this with
203203
* @param preserveMissingOptions if true, any properties that are missing in THIS tree that are present in the input tree, will be added to this tree.
204+
* @param skipOverwritten **DEPENDS ON PARAM "ROOT" BEING NOT NULL** - skips being overwritten if a child node has already been marked as overwritten in the root tree.
205+
* @param root The root tree used to mark overwritten nodes for skipOverwritten. May be null if skipOverwritten is false.
204206
*/
205-
public abstract void overwrite(Node with, boolean preserveMissingOptions, boolean markOverwritten);
207+
public abstract void overwrite(Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root);
206208

207209
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
208210
@VisibleForTesting

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/Property.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,28 @@ public final Property<T> addCallback(@NotNull Collection<Predicate<T>> callbacks
181181

182182
@Override
183183
@SuppressWarnings({"unchecked", "rawtypes"})
184-
public void overwrite(Node with, boolean preserveMissingOptions, boolean markOverwritten) {
184+
public void overwrite(Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
185185
if (!(with instanceof Property)) throw new IllegalArgumentException("Cannot overwrite a property with a non-property");
186186
if (!Objects.equals(this.getID(), with.getID())) throw new IllegalArgumentException("ID should be the same for overwrite");
187-
if (markOverwritten && Objects.equals(getMetadata("overwritten"), true)) return;
187+
if (skipOverwritten && root == null) {
188+
throw new IllegalArgumentException("Cannot mark overwritten without a root tree!");
189+
}
190+
Node overwritten = null;
191+
if (root != null) {
192+
overwritten = root.get("reserved:overwritten");
193+
}
194+
if (skipOverwritten && overwritten instanceof Tree && !Objects.equals(((Tree) overwritten).get(with.getID()), null)) {
195+
return;
196+
}
188197
Property<?> that = (Property<?>) with;
189198
this.addMetadata(that.getMetadata());
190199
Object in = that.get();
191200
if (in != null) this.setAsReferential(in);
192201
if (that.conditions != null) this.addDisplayCondition(that.conditions);
193202
if (that.callbacks != null) addCallback((Collection) that.callbacks);
194-
if (markOverwritten) {
195-
addMetadata("overwritten", true);
203+
if (root != null) {
204+
overwritten = root.getOrPutChild("reserved:overwritten");
205+
((Tree) overwritten).put(new Tree(that.getID(), null, null, null));
196206
}
197207
}
198208

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/Tree.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ private static void _onAllProp(Tree t, BiConsumer<String, Property<?>> action) {
6969
}
7070
}
7171

72-
private static void _overwrite(Tree self, Tree in, Function<String, String> keyMapper, boolean preserveMissingOptions, boolean markOverwritten) {
72+
private static void _overwrite(Tree self, Tree in, Function<String, String> keyMapper, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
73+
Node overwritten = null;
74+
if (root != null) {
75+
overwritten = root.get("reserved:overwritten");
76+
}
7377
for (Map.Entry<String, Node> from : in.theMap.entrySet()) {
7478
String key = keyMapper == null ? from.getKey() : keyMapper.apply(from.getKey());
7579
Node _this = self.get(key);
@@ -83,16 +87,23 @@ private static void _overwrite(Tree self, Tree in, Function<String, String> keyM
8387
continue;
8488
}
8589
if (_this instanceof Tree) {
86-
if (that instanceof Tree && Objects.equals(_this.getMetadata("overwritten"), true)) {
90+
if (skipOverwritten && overwritten instanceof Tree && !Objects.equals(((Tree) overwritten).get(that.getID()), null)) {
91+
// this node was previously overwritten, skip
92+
continue;
93+
}
94+
if (that instanceof Tree) {
8795
// if both are trees, recursively overwrite
88-
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions, markOverwritten);
96+
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions, skipOverwritten, root);
8997
_this.addMetadata(that.getMetadata());
90-
_this.addMetadata("overwritten", true);
98+
if (root != null) {
99+
overwritten = root.getOrPutChild("reserved:overwritten");
100+
((Tree) overwritten).put(new Tree(that.getID(), null, null, null));
101+
}
91102
}
92103
// nop. do not attempt to overwrite a tree with a property
93104
else continue;
94105
}
95-
_this.overwrite(that, preserveMissingOptions, markOverwritten);
106+
_this.overwrite(that, preserveMissingOptions, skipOverwritten, root);
96107
}
97108
}
98109

@@ -275,18 +286,21 @@ public boolean deepEquals(@Nullable Object obj) {
275286
* @param with the tree to overwrite with
276287
* @param keyMapper the key mapper function to use
277288
*/
278-
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions, boolean markOverwritten) {
279-
_overwrite(this, with, keyMapper, preserveMissingOptions, markOverwritten);
289+
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
290+
_overwrite(this, with, keyMapper, preserveMissingOptions, skipOverwritten, root);
280291
}
281292

282293
@Override
283-
public void overwrite(@NotNull Node with, boolean preserveMissingOptions, boolean markOverwritten) {
294+
public void overwrite(@NotNull Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
284295
if (!(with instanceof Tree)) throw new IllegalArgumentException("Cannot overwrite a tree with a non-tree node!");
296+
if (skipOverwritten && root == null) {
297+
throw new IllegalArgumentException("Cannot mark overwritten without a root tree!");
298+
}
285299
Map<String, String> migrationMap = getMetadata("migrationMap");
286300
if (migrationMap == null) {
287-
_overwrite(this, (Tree) with, null, preserveMissingOptions, markOverwritten);
301+
_overwrite(this, (Tree) with, null, preserveMissingOptions, skipOverwritten, root);
288302
} else {
289-
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions, markOverwritten);
303+
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions, skipOverwritten, root);
290304
}
291305
}
292306

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/backend/Backend.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
import org.polyfrost.oneconfig.api.config.v1.Node;
3636
import org.polyfrost.oneconfig.api.config.v1.Tree;
3737

38-
import java.util.Collection;
39-
import java.util.Collections;
40-
import java.util.HashMap;
41-
import java.util.Map;
38+
import java.util.*;
4239

4340
/**
4441
* A backend is a storage system for ConfigTrees.
@@ -114,6 +111,9 @@ public final boolean load(Tree tree) {
114111
return false;
115112
}
116113
if (t == null) return false;
114+
if (t.get("reserved:overwritten") != null) {
115+
tree.put(Objects.requireNonNull(t.get("reserved:overwritten")));
116+
}
117117
tree.overwrite(t, false);
118118

119119
putSafe(tree);

0 commit comments

Comments
 (0)