Skip to content

Commit 7f8442a

Browse files
committed
Add load-annotations function
1 parent a39a96c commit 7f8442a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/integrant/core.cljc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@
266266
(doseq [[tag parents] hierarchy, parent parents]
267267
(derive tag parent)))))))
268268

269+
#?(:clj
270+
(defn load-annotations
271+
"Search the base classpath for all resources that share the same path
272+
(by default `integrant/annotations.edn`), and use their contents to
273+
add annotations to namespaced keywords via [[annotate]]. This allows
274+
annotations to be specified without needing to load every namespace.
275+
276+
The annoation resources should be edn files that map namespaced keywords
277+
to maps of annotation metadata. For example:
278+
279+
{:example/keyword {:doc \"An example keyword.\"}}
280+
281+
This is equivalent to:
282+
283+
(annotate :example/keyword {:doc \"An example keyword.\"})"
284+
([] (load-annotations "integrant/annotations.edn"))
285+
([path]
286+
(doseq [url (resources path)]
287+
(let [annotations (edn/read-string (slurp url))]
288+
(doseq [[kw metadata] annotations]
289+
(annotate kw metadata)))))))
290+
269291
(defn- missing-refs-exception [config refs]
270292
(ex-info (str "Missing definitions for refs: " (str/join ", " refs))
271293
{:reason ::missing-refs

test/integrant/annotations.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:integrant.core-test/a {:doc "A test keyword."}}

test/integrant/core_test.cljc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@
129129
(underive :example/child :example/father)
130130
(underive :example/child :example/mother)))))
131131

132+
#?(:clj
133+
(deftest load-annotations-test
134+
(try
135+
(ig/load-annotations)
136+
(is (= {:doc "A test keyword."} (ig/describe ::a)))
137+
(finally
138+
(ig/annotate ::a {})))))
139+
132140
#?(:clj
133141
(deftest load-namespaces-test
134142
(testing "all namespaces"

0 commit comments

Comments
 (0)