Skip to content

Commit 0456893

Browse files
committed
Replaced the function inline-json-resource by a more generic and versatile version of inline-resource. See the useful examples in the tests.
1 parent 9032d00 commit 0456893

File tree

6 files changed

+64
-55
lines changed

6 files changed

+64
-55
lines changed

deps.edn

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{:paths ["src"]
22
:deps {} ;; This part intentionally left blank
3-
:aliases {:dev {:extra-deps {org.clojure/clojure {:mvn/version "1.10.3"}
4-
org.clojure/clojurescript {:mvn/version "1.11.4"}}}
5-
:test {:extra-paths ["test"]
6-
:extra-deps {lambdaisland/kaocha {:mvn/version "1.63.998"}
3+
:aliases {:dev {:extra-deps {org.clojure/clojure {:mvn/version "1.11.1"}
4+
org.clojure/clojurescript {:mvn/version "1.11.54"}}}
5+
:test {:extra-paths ["test" "test-resource"]
6+
:extra-deps {lambdaisland/kaocha {:mvn/version "1.66.1034"}
77
lambdaisland/kaocha-cljs {:mvn/version "1.0.107"}
88
lambdaisland/kaocha-junit-xml {:mvn/version "0.0.76"}
9-
org.clojure/test.check {:mvn/version "1.1.1"}}}
9+
org.clojure/test.check {:mvn/version "1.1.1"}
10+
org.clojure/data.json {:mvn/version "2.4.0"}}}
1011

1112
; clojure -M:outdated --upgrade
12-
:outdated {:extra-deps {com.github.liquidz/antq {:mvn/version "1.5.1"}}
13+
:outdated {:extra-deps {com.github.liquidz/antq {:mvn/version "1.6.774"}}
1314
:main-opts ["-m" "antq.core"]}
1415

1516
:depstar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.303"}}

src/mate/io.cljc

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
11
(ns mate.io
22
(:require [clojure.string :as str]
3-
#?(:clj [clojure.java.io :as io])
4-
#?(:clj [clojure.data.json :as json]))
3+
#?(:clj [clojure.java.io :as io]))
54
#?(:cljs (:require-macros [mate.io])))
65

7-
(defn resource-path
8-
"Returns the path to a resource.
6+
(defn- resource-path
7+
"Returns the path of a resource.
98
10-
If the resource name starts with a '/', the path is
11-
the resource name with a dropped leading \"/\".
12-
Otherwise, the path is built using the parent directory
13-
of the namespace from which this macro is invoked."
14-
[namespace resource-name]
15-
(if (str/starts-with? resource-name "/")
16-
(subs resource-name 1) ;; drop the leading "/"
17-
(-> (name (ns-name namespace))
18-
(str/split #"\.")
19-
butlast
20-
(->> (str/join "/"))
21-
(str/replace #"-" {"-" "_"})
22-
(str "/" resource-name))))
9+
If the resource name starts with a './', the path is built using
10+
the parent directory of the namespace from which this macro is invoked."
11+
[namespace-obj resource-name]
12+
(if (str/starts-with? resource-name "./")
13+
(let [namespace-str (name (ns-name namespace-obj))
14+
namespace-dir-path (-> namespace-str
15+
(str/split #"\.")
16+
butlast
17+
(->> (str/join "/"))
18+
(str/replace #"-" {"-" "_"}))]
19+
(str namespace-dir-path "/" (subs resource-name (count "./"))))
20+
resource-name))
2321

24-
#?(:clj
25-
(defmacro inline-resource
26-
"Inlines the content of a resource."
27-
[resource-name]
28-
(let [resource-name (eval resource-name)
29-
path (resource-path *ns* resource-name)
30-
resource (io/resource path)]
31-
(assert (some? resource)
32-
(str "Resource \"" path "\" not found."))
33-
(slurp resource))))
22+
#_(resource-path *ns* "foo")
23+
#_(resource-path *ns* "./foo")
24+
25+
(defn inline-resource*
26+
([namespace-obj resource-name]
27+
(inline-resource* namespace-obj resource-name identity))
28+
([namespace-obj resource-name f & args]
29+
(let [resource-name (eval resource-name)
30+
path (resource-path namespace-obj resource-name)
31+
resource (io/resource path)
32+
_ (assert (some? resource)
33+
(str "Resource at path \"" path "\" not found."))
34+
resource-content (slurp resource)]
35+
(apply f resource-content args))))
36+
37+
#_(inline-resource* *ns* "./test-resource.txt" identity)
38+
#_(inline-resource* *ns* "./test-resource.txt" str/upper-case)
3439

3540
#?(:clj
36-
(defmacro inline-json-resource
37-
"Inlines the JSON content of a resource as EDN."
38-
[resource-name]
39-
(let [resource-name (eval resource-name)
40-
path (resource-path *ns* resource-name)
41-
resource (io/resource path)]
42-
(assert (some? resource)
43-
(str "Resource \"" path "\" not found."))
44-
(-> (slurp resource)
45-
json/read-json))))
41+
(defmacro inline-resource
42+
"Inlines the content of a resource at 'macro expansion' time."
43+
([resource-name]
44+
(inline-resource* *ns* (eval resource-name)))
45+
([resource-name f & args]
46+
(apply inline-resource*
47+
*ns*
48+
(eval resource-name)
49+
(eval f)
50+
(map eval args)))))

test-resource/public/foobar.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Content of foobar.txt

test/mate/io_test.cljc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
(ns mate.io-test
22
(:require [clojure.test :refer [deftest is testing]]
3-
[mate.io :as mio]))
3+
[mate.io :as mio]
4+
#?(:clj [clojure.data.json :as json])))
45

56
(deftest inline-resource-test
6-
(testing "if we can find the resource using a relative path."
7-
(is (= (mio/inline-resource (str "test-re" "source.txt"))
7+
(testing "Finds the resource using a relative path."
8+
(is (= (mio/inline-resource (str "./" "test-resource.txt"))
89
"Content of test-resource.txt")))
910

10-
(testing "if we can find the resource using an absolute path."
11-
(is (= (mio/inline-resource (str "/mate/test-re" "source.txt"))
11+
(testing "Finds the resource using an absolute path."
12+
(is (= (mio/inline-resource (str "mate/" "test-resource.txt"))
1213
"Content of test-resource.txt")))
1314

14-
(testing "if we can find the JSON resource using a relative path."
15-
(is (= (mio/inline-json-resource (str "test-json-re" "source.txt"))
16-
[{:a 1} "hello"])))
15+
(testing "Reads and transform the content using multiple arguments."
16+
(is (= (mio/inline-resource "./test-resource.txt"
17+
str " FOO" " BAR")
18+
"Content of test-resource.txt FOO BAR")))
1719

18-
(testing "if we can find the JSON resource using an absolute path."
19-
(is (= (mio/inline-json-resource (str "/mate/test-json-re" "source.txt"))
20+
(testing "Reads the JSON content as string and parse it to get a Clojure data structure."
21+
(is (= (mio/inline-resource "./test-resource.json"
22+
json/read-json)
2023
[{:a 1} "hello"]))))

test/mate/test-json-resource.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/mate/test-resource.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"a": 1}, "hello"]

0 commit comments

Comments
 (0)