Skip to content

Commit

Permalink
🎨 Common accessor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Oct 30, 2023
1 parent 98029b8 commit a8cda1e
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 59 deletions.
28 changes: 26 additions & 2 deletions src/main/java/dev/ebullient/convert/tools/JsonTextConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,12 +28,36 @@ public interface JsonTextConverter<T extends IndexType> {

void appendToText(List<String> 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";
Expand Down Expand Up @@ -422,8 +448,6 @@ default String toTitleCase(String text) {
.collect(Collectors.joining(" "));
}

Tui tui();

enum SourceField implements JsonNodeReader {
abbreviation,
_class_("class"),
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/dev/ebullient/convert/tools/ToolsIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
22 changes: 2 additions & 20 deletions src/main/java/dev/ebullient/convert/tools/dnd5e/JsonSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -554,8 +536,8 @@ default void appendStatblockInline(List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/dev/ebullient/convert/tools/dnd5e/Tools5eIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 ||
Expand All @@ -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);
});

Expand Down Expand Up @@ -1105,7 +1111,7 @@ private boolean inSRD(String abbreviation) {
}

String getKey() {
return TtrpgValue.indexKey.getFromNode(featureTypeNode);
return TtrpgValue.indexKey.getTextOrNull(featureTypeNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -409,6 +409,7 @@ public boolean writeFile() {
hazard,
item,
legendaryGroup,
// magicvariant,
monster,
object,
optionalfeature,
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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);
}

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)");
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,7 +54,7 @@ public Pf2eQuteNote buildNote() {
}

private void appendElement(JsonNode entry, List<String> 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);

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ 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);
}

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();
Expand All @@ -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);
}
Expand Down

0 comments on commit a8cda1e

Please sign in to comment.