Skip to content

Commit

Permalink
♻️ Make mergeNodes accept an ObjectNode
Browse files Browse the repository at this point in the history
Mostly to minimize the stuff that needs to be repeated for the Pf2e
implementation
  • Loading branch information
miscoined authored and ebullient committed Jul 8, 2024
1 parent f4873d6 commit 674ccc6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ public JsonNode handleCopy(T type, JsonNode copyTo) {
// is the copy a copy?
copyFrom = handleCopy(type, copyFrom);
try {
copyTo = mergeNodes(type, copyToKey, copyFrom, copyTo);
// edit in place: if you don't, lower-level copies will keep being revisted.
ObjectNode target = (ObjectNode) copyTo;

copyTo = mergeNodes(type, copyToKey, copyFrom, target);
} catch (JsonCopyException | StackOverflowError | UnsupportedOperationException e) {
tui().errorf(e, "Error (%s): Unable to merge nodes. CopyTo: %s, CopyFrom: %s", copyToKey, copyTo, copyFrom);
}
}
return copyTo;
}

protected JsonNode mergeNodes(T type, String copyToKey, JsonNode copyFrom, JsonNode copyTo) {
return copyTo;
protected JsonNode mergeNodes(T type, String copyToKey, JsonNode copyFrom, ObjectNode target) {
return target;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,8 @@ public JsonNode mergeSubrace(JsonNode subraceNode, JsonNode raceNode) {

// utils.js: static getCopy (impl, copyFrom, copyTo, templateData,...) {
@Override
protected JsonNode mergeNodes(Tools5eIndexType type, String originKey, JsonNode copyFrom, JsonNode copyTo) {
// edit in place: if you don't, lower-level copies will keep being revisted.
ObjectNode target = (ObjectNode) copyTo;

JsonNode _copy = MetaFields._copy.getFromOrEmptyObjectNode(copyTo);
protected JsonNode mergeNodes(Tools5eIndexType type, String originKey, JsonNode copyFrom, ObjectNode target) {
JsonNode _copy = MetaFields._copy.getFromOrEmptyObjectNode(target);
normalizeMods(_copy);

// fetch and apply any external template
Expand Down Expand Up @@ -242,7 +239,7 @@ protected JsonNode mergeNodes(Tools5eIndexType type, String originKey, JsonNode
copyValues(type, copyFrom, target, _copy);

// apply any root template properties after doing base copy
List<String> copyToRootProps = streamOfFieldNames(copyTo).toList();
List<String> copyToRootProps = streamOfFieldNames(target).toList();
for (JsonNode template : templates) {
if (!MetaFields._root.nestedExistsIn(MetaFields.apply, template)) {
continue;
Expand Down

0 comments on commit 674ccc6

Please sign in to comment.