Permalink
Browse files

CLJ-1318 - add support for destructuring namespaced keys

Signed-off-by: Stuart Halloway <stu@cognitect.com>
  • Loading branch information...
1 parent d00efc0 commit 4dc388a9e6517c25fad90ee8db1807209fb87954 @puredanger puredanger committed with stuarthalloway Jan 24, 2014
Showing with 39 additions and 3 deletions.
  1. +7 −3 src/clj/clojure/core.clj
  2. +32 −0 test/clojure/test_clojure/special.clj
View
@@ -4040,7 +4040,8 @@
(dissoc bes (key entry))
((key entry) bes)))
(dissoc b :as :or)
- {:keys #(keyword (str %)), :strs str, :syms #(list `quote %)})]
+ {:keys #(if (keyword? %) % (keyword (str %))),
+ :strs str, :syms #(list `quote %)})]
(if (seq bes)
(let [bb (key (first bes))
bk (val (first bes))
@@ -4051,14 +4052,17 @@
(next bes)))
ret))))]
(cond
- (symbol? b) (-> bvec (conj b) (conj v))
+ (symbol? b) (-> bvec (conj (if (namespace b) (symbol (name b)) b)) (conj v))
+ (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 (str "Unsupported binding form: " b))))))
process-entry (fn [bvec b] (pb bvec (first b) (second b)))]
(if (every? symbol? (map first bents))
bindings
- (reduce1 process-entry [] bents))))
+ (if-let [kwbs (seq (filter #(keyword? (first %)) bents))]
+ (throw (new Exception (str "Unsupported binding key: " (ffirst kwbs))))
+ (reduce1 process-entry [] bents)))))
(defmacro let
"binding => binding-form init-expr
@@ -31,3 +31,35 @@
(deftest empty-list-with-:as-destructuring
(let [{:as x} '()]
(is (= {} x))))
+
+(deftest keywords-in-destructuring
+ (let [{:keys [:a :b]} {:a 1 :b 2}]
+ (is (= 1 a))
+ (is (= 2 b))))
+
+(deftest namespaced-keywords-in-destructuring
+ (let [{:keys [:a/b :c/d]} {:a/b 1 :c/d 2}]
+ (is (= 1 b))
+ (is (= 2 d))))
+
+(deftest namespaced-keys-in-destructuring
+ (let [{:keys [a/b c/d]} {:a/b 1 :c/d 2}]
+ (is (= 1 b))
+ (is (= 2 d))))
+
+(deftest namespaced-syms-in-destructuring
+ (let [{:syms [a/b c/d]} {'a/b 1 'c/d 2}]
+ (is (= 1 b))
+ (is (= 2 d))))
+
+(deftest keywords-not-allowed-in-let-bindings
+ (is (thrown-with-msg? Exception #"Unsupported binding key: :a"
+ (eval '(let [:a 1] a))))
+ (is (thrown-with-msg? Exception #"Unsupported binding key: :a/b"
+ (eval '(let [:a/b 1] b)))))
+
+(require '[clojure.string :as s])
+(deftest resolve-keyword-ns-alias-in-destructuring
+ (let [{:keys [::s/x ::s/y]} {:clojure.string/x 1 :clojure.string/y 2}]
+ (is (= x 1))
+ (is (= y 2))))

0 comments on commit 4dc388a

Please sign in to comment.