Permalink
Browse files

CLJS-745: Support destructuring maps with namespaced keywords

port CLJ-1138 patch to ClojureScript
  • Loading branch information...
swannodette committed Feb 23, 2014
1 parent b33bde3 commit 95d60acf610b255dbb4079ccf60739c81a611922
Showing with 31 additions and 4 deletions.
  1. +7 −3 src/clj/cljs/core.clj
  2. +24 −1 test/cljs/cljs/core_test.cljs
View
@@ -115,7 +115,8 @@
(dissoc bes (key entry))
((key entry) bes)))
(dissoc b :as :or)
- {:keys #(keyword (core/str %)), :strs core/str, :syms #(core/list `quote %)})]
+ {:keys #(if (core/keyword? %) % (keyword (core/str %))),
+ :strs core/str, :syms #(core/list `quote %)})]
(if (seq bes)
(core/let [bb (key (first bes))
bk (val (first bes))
@@ -126,14 +127,17 @@
(next bes)))
ret))))]
(core/cond
- (core/symbol? b) (-> bvec (conj b) (conj v))
+ (core/symbol? b) (-> bvec (conj (if (namespace b) (symbol (name b)) b)) (conj v))
+ (core/keyword? b) (-> bvec (conj (symbol (name b))) (conj v))
(vector? b) (pvec bvec b v)
(map? b) (pmap bvec b v)
:else (throw (new Exception (core/str "Unsupported binding form: " b))))))
process-entry (fn [bvec b] (pb bvec (first b) (second b)))]
(if (every? core/symbol? (map first bents))
bindings
- (reduce process-entry [] bents))))
+ (if-let [kwbs (seq (filter #(core/keyword? (first %)) bents))]
+ (throw (new Exception (core/str "Unsupported binding key: " (ffirst kwbs))))
+ (reduce process-entry [] bents)))))
(defmacro let
"binding => binding-form init-expr
@@ -1,4 +1,5 @@
-(ns cljs.core-test)
+(ns cljs.core-test
+ (:require [clojure.string :as s]))
(defn test-stuff []
;; js primitives
@@ -2092,5 +2093,27 @@
(assert (= :fail (try (assoc! (transient [1 2]) n 4)
(catch js/Error e :fail)))))
+ ;; Namespaced destructuring
+
+ (let [{:keys [:a :b]} {:a 1 :b 2}]
+ (assert (= 1 a))
+ (assert (= 2 b)))
+
+ (let [{:keys [:a/b :c/d]} {:a/b 1 :c/d 2}]
+ (assert (= 1 b))
+ (assert (= 2 d)))
+
+ (let [{:keys [a/b c/d]} {:a/b 1 :c/d 2}]
+ (assert (= 1 b))
+ (assert (= 2 d)))
+
+ (let [{:syms [a/b c/d]} {'a/b 1 'c/d 2}]
+ (assert (= 1 b))
+ (assert (= 2 d)))
+
+ (let [{:keys [::s/x ::s/y]} {:clojure.string/x 1 :clojure.string/y 2}]
+ (assert (= x 1))
+ (assert (= y 2)))
+
:ok
)

0 comments on commit 95d60ac

Please sign in to comment.