|
34 | 34 | (toString [this]
|
35 | 35 | (node/string this)))
|
36 | 36 |
|
37 |
| -(defrecord NamespacedMapNode [wrap-length children] |
| 37 | +(defn- assert-namespaced-map-children |
| 38 | + [children] |
| 39 | + (let [exs (node/sexprs children)] |
| 40 | + (assert (= (count exs) 2) |
| 41 | + "can only contain 2 non-whitespace forms.") |
| 42 | + (assert (keyword? (first exs)) |
| 43 | + "first form in namespaced map needs to be keyword.") |
| 44 | + (assert (not (namespace (first exs))) |
| 45 | + "keyword for namespaced map may not be already namespaced.") |
| 46 | + (assert (map? (second exs)) |
| 47 | + "second form in namespaced map needs to be map."))) |
| 48 | + |
| 49 | +(defrecord NamespacedMapNode [children] |
38 | 50 | node/Node
|
39 | 51 | (tag [this]
|
40 | 52 | :namespaced-map)
|
41 | 53 | (printable-only? [_] false)
|
42 | 54 | (sexpr [this]
|
43 |
| - (let [n (node/sexpr (first children))] |
44 |
| - (reduce-kv |
45 |
| - (fn [m k v] |
46 |
| - (if (keyword? k) |
47 |
| - (cond (namespace k) |
48 |
| - (assoc m k v) |
49 |
| - (namespace n) |
50 |
| - (assoc m (keyword |
51 |
| - (str (namespace n) "/" (name n)) |
52 |
| - (name k)) v) |
53 |
| - :else |
54 |
| - (assoc m (keyword (name n) (name k)) v)) |
55 |
| - (assoc m k v))) |
56 |
| - {} (apply hash-map (node/sexprs (-> children second :children)))))) |
| 55 | + (let [[ns m] (node/sexprs children) |
| 56 | + ns (name ns)] |
| 57 | + (->> (for [[k v] m |
| 58 | + :let [k' (cond (not (keyword? k)) k |
| 59 | + (namespace k) k |
| 60 | + :else (keyword ns (name k)))]] |
| 61 | + [k' v]) |
| 62 | + (into {})))) |
57 | 63 | (length [_]
|
58 |
| - (+ wrap-length (node/sum-lengths children))) |
| 64 | + (+ 1 (node/sum-lengths children))) |
59 | 65 | (string [this]
|
60 |
| - (format "#%s{%s}" |
61 |
| - (node/string (first children)) |
62 |
| - (node/concat-strings (-> children second :children)))) |
| 66 | + (str "#" (node/concat-strings children))) |
63 | 67 |
|
64 | 68 | node/InnerNode
|
65 | 69 | (inner? [_] true)
|
66 | 70 | (children [_] children)
|
67 | 71 | (replace-children [this children']
|
68 |
| - (node/assert-sexpr-count children' 2) |
| 72 | + (assert-namespaced-map-children children') |
69 | 73 | (assoc this :children children'))
|
70 | 74 | (leader-length [_]
|
71 |
| - (dec wrap-length)) |
| 75 | + 1) |
72 | 76 |
|
73 | 77 | Object
|
74 | 78 | (toString [this]
|
|
102 | 106 | (defn namespaced-map-node
|
103 | 107 | "Create a node representing an EDN map namespace."
|
104 | 108 | [children]
|
105 |
| - (node/assert-sexpr-count children 2) |
106 |
| - (->NamespacedMapNode 2 children)) |
| 109 | + (assert-namespaced-map-children children) |
| 110 | + (->NamespacedMapNode children)) |
0 commit comments