From a8cda1e6aa59d764304febcd7033ec5d1aa29ba7 Mon Sep 17 00:00:00 2001 From: Erin Schnabel Date: Mon, 30 Oct 2023 10:27:29 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Common=20accessor=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../convert/tools/JsonTextConverter.java | 28 +++++++++++++++++-- .../ebullient/convert/tools/ToolsIndex.java | 15 ++-------- .../convert/tools/dnd5e/Json2QuteCompose.java | 4 +-- .../convert/tools/dnd5e/JsonSource.java | 22 ++------------- .../tools/dnd5e/JsonTextReplacement.java | 5 ---- .../convert/tools/dnd5e/Tools5eIndex.java | 16 +++++++---- .../convert/tools/dnd5e/Tools5eIndexType.java | 4 ++- .../convert/tools/dnd5e/Tools5eSources.java | 6 ++-- .../convert/tools/pf2e/Json2QuteCompose.java | 4 +-- .../convert/tools/pf2e/Pf2eIndex.java | 6 ++-- .../convert/tools/pf2e/Pf2eSources.java | 6 ++-- 11 files changed, 57 insertions(+), 59 deletions(-) diff --git a/src/main/java/dev/ebullient/convert/tools/JsonTextConverter.java b/src/main/java/dev/ebullient/convert/tools/JsonTextConverter.java index 44150912a..240726199 100644 --- a/src/main/java/dev/ebullient/convert/tools/JsonTextConverter.java +++ b/src/main/java/dev/ebullient/convert/tools/JsonTextConverter.java @@ -12,7 +12,9 @@ import java.util.stream.Stream; import java.util.stream.StreamSupport; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -26,12 +28,36 @@ public interface JsonTextConverter { void appendToText(List inner, JsonNode target, String heading); + Tui tui(); + CompendiumConfig cfg(); + default ObjectMapper mapper() { + return Tui.MAPPER; + } + default ParseState parseState() { return cfg().parseState(); } + default JsonNode copyNode(JsonNode sourceNode) { + try { + return mapper().readTree(sourceNode.toString()); + } catch (JsonProcessingException ex) { + tui().errorf(ex, "Unable to copy %s", sourceNode.toString()); + throw new IllegalStateException("JsonProcessingException processing " + sourceNode); + } + } + + default JsonNode createNode(String source) { + try { + return mapper().readTree(source); + } catch (JsonProcessingException ex) { + tui().errorf(ex, "Unable to create node from %s", source); + throw new IllegalStateException("JsonProcessingException processing " + source); + } + } + default String formatDice(String diceRoll) { // needs to be escaped: \\ to escape the \\ so it is preserved in the output String avg = parseState().inMarkdownTable() ? "\\\\|avg" : "|avg"; @@ -422,8 +448,6 @@ default String toTitleCase(String text) { .collect(Collectors.joining(" ")); } - Tui tui(); - enum SourceField implements JsonNodeReader { abbreviation, _class_("class"), diff --git a/src/main/java/dev/ebullient/convert/tools/ToolsIndex.java b/src/main/java/dev/ebullient/convert/tools/ToolsIndex.java index 6a5c146f0..ad070c329 100644 --- a/src/main/java/dev/ebullient/convert/tools/ToolsIndex.java +++ b/src/main/java/dev/ebullient/convert/tools/ToolsIndex.java @@ -6,7 +6,6 @@ import java.util.Objects; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; import dev.ebullient.convert.config.CompendiumConfig; import dev.ebullient.convert.config.Datasource; @@ -19,18 +18,8 @@ public interface ToolsIndex { // Special one-offs for accounting/tracking enum TtrpgValue implements JsonNodeReader { indexKey, - indexInputType; - - public void addToNode(JsonNode node, String value) { - ((ObjectNode) node).put(this.name(), value); - } - - public String getFromNode(JsonNode node) { - if (node == null) { - return null; - } - return this.getTextOrNull(node); - } + indexInputType, + indexBaseItem; } static ToolsIndex createIndex() { diff --git a/src/main/java/dev/ebullient/convert/tools/dnd5e/Json2QuteCompose.java b/src/main/java/dev/ebullient/convert/tools/dnd5e/Json2QuteCompose.java index 0cab46a90..6f41c60b6 100644 --- a/src/main/java/dev/ebullient/convert/tools/dnd5e/Json2QuteCompose.java +++ b/src/main/java/dev/ebullient/convert/tools/dnd5e/Json2QuteCompose.java @@ -13,7 +13,7 @@ import dev.ebullient.convert.config.TtrpgConfig; import dev.ebullient.convert.tools.JsonNodeReader; import dev.ebullient.convert.tools.Tags; -import dev.ebullient.convert.tools.ToolsIndex; +import dev.ebullient.convert.tools.ToolsIndex.TtrpgValue; import dev.ebullient.convert.tools.dnd5e.qute.Tools5eQuteNote; public class Json2QuteCompose extends Json2QuteCommon { @@ -33,7 +33,7 @@ public Json2QuteCompose(Tools5eIndexType type, Tools5eIndex index, String title, } public void add(JsonNode node) { - String key = ToolsIndex.TtrpgValue.indexKey.getFromNode(node); + String key = TtrpgValue.indexKey.getTextOrEmpty(node); if (index.isIncluded(key)) { nodes.add(node); } else { diff --git a/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonSource.java b/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonSource.java index a484579a7..679d582ab 100644 --- a/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonSource.java +++ b/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonSource.java @@ -70,24 +70,6 @@ default int intOrThrow(JsonNode source, String key) { return result.asInt(); } - default JsonNode copyNode(JsonNode sourceNode) { - try { - return mapper().readTree(sourceNode.toString()); - } catch (JsonProcessingException ex) { - tui().errorf(ex, "Unable to copy %s", sourceNode.toString()); - throw new IllegalStateException("JsonProcessingException processing " + sourceNode); - } - } - - default JsonNode createNode(String source) { - try { - return mapper().readTree(source); - } catch (JsonProcessingException ex) { - tui().errorf(ex, "Unable to create node from %s", source); - throw new IllegalStateException("JsonProcessingException processing " + source); - } - } - default String getSourceText(JsonNode node) { return getSourceText(Tools5eSources.findOrTemporary(node)); } @@ -554,8 +536,8 @@ default void appendStatblockInline(List text, JsonNode entry, String hea String finalKey = type.createKey(data); JsonNode existingNode = index().getNode(finalKey); - TtrpgValue.indexKey.addToNode(data, finalKey); - TtrpgValue.indexInputType.addToNode(data, type.name()); + TtrpgValue.indexKey.setIn(data, finalKey); + TtrpgValue.indexInputType.setIn(data, type.name()); // TODO: Remove me. JsonNode copy = MetaFields._copy.getFrom(data); diff --git a/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonTextReplacement.java b/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonTextReplacement.java index c53f01653..0498c8308 100644 --- a/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonTextReplacement.java +++ b/src/main/java/dev/ebullient/convert/tools/dnd5e/JsonTextReplacement.java @@ -10,7 +10,6 @@ import java.util.stream.Collectors; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import dev.ebullient.convert.config.CompendiumConfig; @@ -55,10 +54,6 @@ default CompendiumConfig cfg() { return index().cfg(); } - default ObjectMapper mapper() { - return Tui.MAPPER; - } - default boolean isPresent(String s) { return s != null && !s.isBlank(); } diff --git a/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndex.java b/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndex.java index 9bdbea8a7..6fc4e8c36 100644 --- a/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndex.java +++ b/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndex.java @@ -19,6 +19,7 @@ import java.util.stream.Stream; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.BooleanNode; import com.fasterxml.jackson.databind.node.ObjectNode; import dev.ebullient.convert.config.CompendiumConfig; @@ -176,6 +177,11 @@ public Tools5eIndex importTree(String filename, JsonNode node) { // Index content types indexTypes(filename, node); + // base items are special: add an additional flag + Tools5eIndexType.item.withArrayFrom(node, "baseitem", (type, x) -> { + TtrpgValue.indexBaseItem.setIn(x, BooleanNode.TRUE); + }); + return this; } @@ -259,8 +265,8 @@ void addToIndex(Tools5eIndexType type, JsonNode node) { return; } nodeIndex.put(key, node); - TtrpgValue.indexInputType.addToNode(node, type.name()); - TtrpgValue.indexKey.addToNode(node, key); + TtrpgValue.indexInputType.setIn(node, type.name()); + TtrpgValue.indexKey.setIn(node, key); if (type == Tools5eIndexType.classtype && !booleanOrDefault(node, "isReprinted", false)) { @@ -369,7 +375,7 @@ public void prepare() { JsonNode jsonSource = copier.handleCopy(type, node); entry.setValue(jsonSource); // update with resolved copy - TtrpgValue.indexKey.addToNode(node, key); + TtrpgValue.indexKey.setIn(node, key); Tools5eSources sources = Tools5eSources.constructSources(node); if (type == Tools5eIndexType.subrace || @@ -395,7 +401,7 @@ public void prepare() { tui().errorf("Duplicate key: %s", v.key); } // store unique key / construct sources for variants - TtrpgValue.indexKey.addToNode(v.node, v.key); + TtrpgValue.indexKey.setIn(v.node, v.key); Tools5eSources.constructSources(v.node); }); @@ -1105,7 +1111,7 @@ private boolean inSRD(String abbreviation) { } String getKey() { - return TtrpgValue.indexKey.getFromNode(featureTypeNode); + return TtrpgValue.indexKey.getTextOrNull(featureTypeNode); } } diff --git a/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndexType.java b/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndexType.java index 44cb1cc6c..39219a5de 100644 --- a/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndexType.java +++ b/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndexType.java @@ -118,7 +118,7 @@ public static Tools5eIndexType getTypeFromKey(String key) { } public static Tools5eIndexType getTypeFromNode(JsonNode node) { - String typeKey = TtrpgValue.indexInputType.getFromNode(node); + String typeKey = TtrpgValue.indexInputType.getTextOrEmpty(node); return fromText(typeKey); } @@ -409,6 +409,7 @@ public boolean writeFile() { hazard, item, legendaryGroup, + // magicvariant, monster, object, optionalfeature, @@ -469,6 +470,7 @@ public String getRelativePath() { case card, deck -> "decks"; case deity -> "deities"; case legendaryGroup -> "bestiary/legendary-group"; + // case magicvariant -> "items"; case monster -> "bestiary"; case optionalfeature, optionalFeatureTypes -> "optional-features"; case race, subrace -> "races"; diff --git a/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eSources.java b/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eSources.java index c68be3878..590f84229 100644 --- a/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eSources.java +++ b/src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eSources.java @@ -39,7 +39,7 @@ public static Tools5eSources findSources(String key) { } public static Tools5eSources findSources(JsonNode node) { - String key = TtrpgValue.indexKey.getFromNode(node); + String key = TtrpgValue.indexKey.getTextOrEmpty(node); return keyToSources.get(key); } @@ -47,7 +47,7 @@ public static Tools5eSources constructSources(JsonNode node) { if (node == null) { throw new IllegalArgumentException("Must pass a JsonNode"); } - String key = TtrpgValue.indexKey.getFromNode(node); + String key = TtrpgValue.indexKey.getTextOrEmpty(node); if (key == null) { throw new IllegalArgumentException("Node has not been indexed (no key)"); } @@ -69,7 +69,7 @@ public static Tools5eSources findOrTemporary(JsonNode node) { ? Tools5eIndexType.reference : Tools5eIndexType.syntheticGroup; } - String key = TtrpgValue.indexKey.getFromNode(node); + String key = TtrpgValue.indexKey.getTextOrNull(node); if (key == null) { key = type.createKey(node); } diff --git a/src/main/java/dev/ebullient/convert/tools/pf2e/Json2QuteCompose.java b/src/main/java/dev/ebullient/convert/tools/pf2e/Json2QuteCompose.java index 1c23713e5..a205a4acf 100644 --- a/src/main/java/dev/ebullient/convert/tools/pf2e/Json2QuteCompose.java +++ b/src/main/java/dev/ebullient/convert/tools/pf2e/Json2QuteCompose.java @@ -9,7 +9,7 @@ import com.fasterxml.jackson.databind.JsonNode; import dev.ebullient.convert.tools.Tags; -import dev.ebullient.convert.tools.ToolsIndex; +import dev.ebullient.convert.tools.ToolsIndex.TtrpgValue; import dev.ebullient.convert.tools.pf2e.qute.Pf2eQuteNote; public class Json2QuteCompose extends Json2QuteBase { @@ -54,7 +54,7 @@ public Pf2eQuteNote buildNote() { } private void appendElement(JsonNode entry, List text, Tags tags) { - String key = ToolsIndex.TtrpgValue.indexKey.getFromNode(entry); + String key = TtrpgValue.indexKey.getTextOrEmpty(entry); currentSources = Pf2eSources.findSources(key); String name = SourceField.name.getTextOrEmpty(entry); diff --git a/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eIndex.java b/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eIndex.java index f10d2b161..75655ec35 100644 --- a/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eIndex.java +++ b/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eIndex.java @@ -90,7 +90,7 @@ void addToIndex(Pf2eIndexType type, JsonNode node) { // always use item (baseitem is a detail that we have remembered if we need it) type = Pf2eIndexType.item; } - TtrpgValue.indexInputType.addToNode(node, type.name()); + TtrpgValue.indexInputType.setIn(node, type.name()); // TODO: Variants? Reprints? String key = type.createKey(node); String hash = Field.add_hash.getTextOrNull(node); @@ -114,7 +114,7 @@ void addToIndex(Pf2eIndexType type, JsonNode node) { return; } imported.put(key, node); - TtrpgValue.indexKey.addToNode(node, key); + TtrpgValue.indexKey.setIn(node, key); } String prepareTrait(String key, JsonNode node) { @@ -173,7 +173,7 @@ void addDataToIndex(JsonNode data, String filename) { if (dash >= 0) { newNode.put("source", name.substring(dash + 1)); } - TtrpgValue.indexKey.addToNode(newNode, key); // backlink + TtrpgValue.indexKey.setIn(newNode, key); // backlink imported.put(key, newNode); } diff --git a/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eSources.java b/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eSources.java index 176b5263b..41049695e 100644 --- a/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eSources.java +++ b/src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eSources.java @@ -25,7 +25,7 @@ public static Pf2eSources findSources(String key) { } public static Pf2eSources findSources(JsonNode node) { - String key = TtrpgValue.indexKey.getFromNode(node); + String key = TtrpgValue.indexKey.getTextOrEmpty(node); return keyToSources.get(key); } @@ -33,7 +33,7 @@ public static Pf2eSources constructSources(Pf2eIndexType type, JsonNode node) { if (node == null) { throw new IllegalArgumentException("Must pass a JsonNode"); } - String key = TtrpgValue.indexKey.getFromNode(node); + String key = TtrpgValue.indexKey.getTextOrNull(node); return keyToSources.computeIfAbsent(key, k -> { Pf2eSources s = new Pf2eSources(type, key, node); s.checkKnown(); @@ -58,7 +58,7 @@ public static Pf2eSources findOrTemporary(Pf2eIndexType type, JsonNode node) { if (node == null) { throw new IllegalArgumentException("Must pass a JsonNode"); } - String key = TtrpgValue.indexKey.getFromNode(node); + String key = TtrpgValue.indexKey.getTextOrNull(node); if (key == null) { key = type.createKey(node); }