Skip to content

Commit caa4be8

Browse files
author
Yannick Scherer
committed
[#54] allow namespaced maps with namespace alias.
1 parent 771aae9 commit caa4be8

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/rewrite_clj/node/seq.clj

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
"can only contain 2 non-whitespace forms.")
4242
(assert (keyword? (first exs))
4343
"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.")
4644
(assert (map? (second exs))
4745
"second form in namespaced map needs to be map.")))
4846

@@ -52,12 +50,20 @@
5250
:namespaced-map)
5351
(printable-only? [_] false)
5452
(sexpr [this]
55-
(let [[ns m] (node/sexprs children)
56-
ns (name ns)]
53+
(let [[nspace' m] (node/sexprs children)
54+
nspace (if (namespace nspace')
55+
(-> (ns-aliases *ns*)
56+
(get (symbol (name nspace')))
57+
(ns-name)
58+
(name))
59+
(name nspace'))]
60+
(assert nspace
61+
(str "could not resolve namespace alias for namespaced map: "
62+
(namespace nspace')))
5763
(->> (for [[k v] m
5864
:let [k' (cond (not (keyword? k)) k
5965
(namespace k) k
60-
:else (keyword ns (name k)))]]
66+
:else (keyword nspace (name k)))]]
6167
[k' v])
6268
(into {}))))
6369
(length [_]

test/rewrite_clj/parser_test.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@
241241
"#:abc{:x 1, :y 1}"
242242
"#:abc {:x 1, :y 1}"))
243243

244+
(deftest t-parsing-namespaced-maps-with-namespace-alias
245+
(are [?s]
246+
(let [n (p/parse-string ?s)]
247+
(is (= :namespaced-map (node/tag n)))
248+
(is (= (count ?s) (node/length n)))
249+
(is (= ?s (node/string n)))
250+
(is (= {::node/x 1, ::node/y 1} (node/sexpr n))))
251+
"#::node{:x 1, :y 1}"
252+
"#::node {:x 1, :y 1}"))
253+
244254
(deftest t-parsing-exceptions
245255
(are [?s ?p]
246256
(is (thrown? Exception ?p (p/parse-string ?s)))

0 commit comments

Comments
 (0)