Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support renaming with :as in import-vars #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The former approach places an onus on the creator of the library; the various or
```clojure
(import-vars
[clojure.walk
prewalk
[prewalk :as walk-pre]
postwalk]
[clojure.data
diff])
Expand Down
31 changes: 19 additions & 12 deletions src/potemkin/namespaces.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,33 @@
"Imports a list of vars from other namespaces."
[& syms]
(let [unravel (fn unravel [x]
(if (sequential? x)
(cond
(and (vector? x) (#{:as} (second x)))
[(with-meta (first x) {:name (nth x 2)})]

(sequential? x)
(->> x
rest
(mapcat unravel)
(map
#(symbol
(str (first x)
(when-let [n (namespace %)]
(str "." n)))
(name %))))
[x]))
#(with-meta (symbol
(str (first x)
(when-let [n (namespace %)]
(str "." n)))
(name %))
(meta %))))

:else [x]))
syms (mapcat unravel syms)]
`(do
~@(map
(fn [sym]
(let [vr (resolve sym)
m (meta vr)]
m (meta vr)
?name (-> sym meta :name)]
(cond
(nil? vr) `(throw (ex-info (format "`%s` does not exist" '~sym) {}))
(:macro m) `(import-macro ~sym)
(:arglists m) `(import-fn ~sym)
:else `(import-def ~sym))))
(nil? vr) `(throw (ex-info (format "`%s` does not exist" '~sym) {}))
(:macro m) `(import-macro ~sym ~?name)
(:arglists m) `(import-fn ~sym ~?name)
:else `(import-def ~sym ~?name))))
syms))))
11 changes: 11 additions & 0 deletions test/potemkin/namespaces_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
(import-fn i/protocol-function)
(import-fn i/inlined-fn)
(import-def i/some-value)
(import-vars [potemkin.imports-test
[multi-arity-macro :as renamed-multi-arity-macro]
[multi-arity-fn :as renamed-multi-arity-fn]
[some-value :as renamed-some-value]])


(defn drop-lines [n s]
Expand Down Expand Up @@ -50,3 +54,10 @@
(is false "`import-vars` should have thrown an exception")
(catch Exception ex
(is "`clojure.set/onion-misspelled` does not exist" (.getMessage ex)))))

(deftest import-vars-allows-renaming
(is (= 1 renamed-some-value))
(is (out= (source i/multi-arity-fn) (source renamed-multi-arity-fn)))
(is (rest-out= (doc i/multi-arity-fn) (doc renamed-multi-arity-fn)))
(is (out= (source i/multi-arity-macro) (source renamed-multi-arity-macro)))
(is (rest-out= (doc i/multi-arity-macro) (doc renamed-multi-arity-macro))))