Skip to content

Commit df9e69a

Browse files
authored
dot export: escape \ (#356)
* only logInfo about existing storage if it's non-empty * dot export: escape `\` https://discord.com/channels/832209896089976854/832209896089976857/1075959071976464464 * fixup imports
1 parent 379a2d3 commit df9e69a

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

core/src/main/java/overflowdb/Graph.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ private Graph(Config config,
9393
private void initElementCollections(OdbStorage storage) {
9494
long start = System.currentTimeMillis();
9595
final Set<Map.Entry<Long, byte[]>> serializedNodes = storage.allNodes();
96-
logger.info(String.format("initializing %d nodes from existing storage", serializedNodes.size()));
96+
final int serializedNodesCount = serializedNodes.size();
97+
if (serializedNodesCount > 0) {
98+
logger.info(String.format("initializing %d nodes from existing storage", serializedNodesCount));
99+
}
97100
int importCount = 0;
98101
long maxId = currentId.get();
99102

formats/src/main/scala/overflowdb/formats/dot/DotExporter.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ object DotExporter extends Exporter {
7171
private def encodePropertyValue(value: Object): String = {
7272
value match {
7373
case value: String =>
74-
// escape double quotes, because we use them to enclose strings
75-
val escaped = value.replace("\"", "\\\"")
74+
val escaped = value
75+
.replace("""\""", """\\""") // escape escape chars - this should come first
76+
.replace("\"", "\\\"") // escape double quotes, because we use them to enclose strings
7677
s"\"$escaped\""
7778
case list if iterableForList.isDefinedAt(list) =>
7879
val values = iterableForList(list).mkString(";")

formats/src/test/scala/overflowdb/formats/dot/DotTests.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DotTests extends AnyWordSpec {
1414
"Exporter should export valid dot" in {
1515
val graph = SimpleDomain.newGraph()
1616

17-
val node2 = graph.addNode(2, TestNode.LABEL, TestNode.STRING_PROPERTY, "string\"Prop2")
17+
val node2 = graph.addNode(2, TestNode.LABEL, TestNode.STRING_PROPERTY, """string"Prop2\""")
1818
val node3 = graph.addNode(3, TestNode.LABEL, TestNode.INT_PROPERTY, 13)
1919

2020
// only allows values defined in FunkyList.funkyWords
@@ -24,7 +24,7 @@ class DotTests extends AnyWordSpec {
2424
val node1 = graph.addNode(1, TestNode.LABEL,
2525
TestNode.INT_PROPERTY, 11,
2626
TestNode.STRING_PROPERTY, "<stringProp1>",
27-
TestNode.STRING_LIST_PROPERTY, List("stringListProp1a", "stringListProp1b").asJava,
27+
TestNode.STRING_LIST_PROPERTY, List("stringListProp1a", "stringList\\Prop1b").asJava,
2828
TestNode.FUNKY_LIST_PROPERTY, funkyList,
2929
TestNode.INT_LIST_PROPERTY, List(21, 31, 41).asJava,
3030
)
@@ -42,9 +42,9 @@ class DotTests extends AnyWordSpec {
4242
withClue(s"actual result was: `$result`") {
4343
result shouldBe
4444
"""digraph {
45-
| 2 [label=testNode StringProperty="string\"Prop2"]
45+
| 2 [label=testNode StringProperty="string\"Prop2\\"]
4646
| 3 [label=testNode StringProperty="DEFAULT_STRING_VALUE" IntProperty=13]
47-
| 1 [label=testNode FunkyListProperty="apoplectic;bucolic" StringProperty="<stringProp1>" StringListProperty="stringListProp1a;stringListProp1b" IntProperty=11 IntListProperty="21;31;41"]
47+
| 1 [label=testNode FunkyListProperty="apoplectic;bucolic" StringProperty="<stringProp1>" StringListProperty="stringListProp1a;stringList\Prop1b" IntProperty=11 IntListProperty="21;31;41"]
4848
| 2 -> 3 [label=testEdge longProperty=-99]
4949
| 1 -> 2 [label=testEdge longProperty=9223372036854775807]
5050
|}""".stripMargin.trim

0 commit comments

Comments
 (0)