Skip to content

Commit b9578eb

Browse files
author
Paula Gearon
committed
Tightened up internal node serialization
1 parent 8524277 commit b9578eb

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/asami/graph.cljc

+21-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#?(:clj
4040
(deftype InternalNode [^long id]
4141
Object
42-
(toString [_] (str "#a/n \"" id "\""))
42+
(toString [_] (str "#a/n[" id "]"))
4343
(equals [_ o] (and (instance? InternalNode o) (= id (.id ^InternalNode o))))
4444
(hashCode [_] (hash id))
4545
IdCheck
@@ -48,7 +48,7 @@
4848
:cljs
4949
(deftype InternalNode [^long id]
5050
Object
51-
(toString [_] (str "#a/n \"" id "\""))
51+
(toString [_] (str "#a/n[" id "]"))
5252

5353
IEquiv
5454
(-equiv [_ o] (and (instance? InternalNode o) (= id (.-id o))))
@@ -64,16 +64,26 @@
6464

6565
#?(:clj
6666
(defmethod clojure.core/print-method InternalNode [^InternalNode o ^Writer w]
67-
(.write w "#a/n \"")
67+
(.write w "#a/n[")
6868
(.write w (str (.id o)))
69-
(.write w "\"")))
70-
71-
(defn node-read
72-
"Reads a node from a string"
73-
[s]
74-
(InternalNode.
75-
#?(:clj (Long/parseLong s)
76-
:cljs (long s))))
69+
(.write w "]")))
70+
71+
(defprotocol NodeData
72+
(node-read [data] "Reads an internal node out of data"))
73+
74+
#?(:clj
75+
(extend-protocol NodeData
76+
String
77+
(node-read [s] (InternalNode. (Long/parseLong s)))
78+
clojure.lang.Indexed
79+
(node-read [v] (InternalNode. (nth v 0))))
80+
81+
:cljs
82+
(extend-protocol NodeData
83+
string
84+
(node-read [s] (InternalNode. (long s)))
85+
PersistentVector
86+
(node-read [v] (InternalNode. (nth v 0)))))
7787

7888
;; can set this at a Clojure repl:
7989
;; (set! *data-readers* graph/node-reader)

0 commit comments

Comments
 (0)