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

Handle reader macros when merging EDN files #39

Merged
merged 2 commits into from
Oct 16, 2024
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Cache Maven deps
uses: actions/cache@v1
uses: actions/cache@v4
env:
cache-name: maven-deps
with:
Expand Down Expand Up @@ -46,10 +46,10 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Cache Maven deps
uses: actions/cache@v1
uses: actions/cache@v4
env:
cache-name: maven-deps
with:
Expand Down
15 changes: 12 additions & 3 deletions src/vessel/resource_merge.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns vessel.resource-merge
"Support for merging duplicated resources on the classpath."
(:require [clojure.java.io :as io]
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[vessel.misc :as misc])
(:import (java.io File InputStream)))
(:import (java.io File InputStream PushbackReader)))


(defn- write-edn
Expand All @@ -26,6 +27,14 @@
:else
right))

(defn read-edn
"Reads an InputStream as EDN but uses a tagged literal for any reader macros found in the stream."
[input-stream]
(->> input-stream
io/reader
PushbackReader.
(edn/read {:default tagged-literal})))

;; A rule is a map:
;; :match-fn (fn [File]) -> boolean (File is the output file)
;; :read-fn (fn [InputStream]) -> value
Expand All @@ -42,7 +51,7 @@
(def edn-base-rule
"*.edn - deep merged together"
{:match-fn #(.endsWith (.getPath ^File %) ".edn")
:read-fn misc/read-edn
:read-fn read-edn
:merge-fn deep-merge
:write-fn write-edn})

Expand Down
2 changes: 2 additions & 0 deletions test/resources/lib5/with-reader-macros.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:k1 :v2
:k2 #unknown/macro :v2}
2 changes: 2 additions & 0 deletions test/resources/lib6/with-reader-macros.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:k1 :override-k1
:k3 #weird/macro :v3}
11 changes: 11 additions & 0 deletions test/unit/vessel/builder_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@
(is (= (.lastModified (io/file src "lib2/resource1.edn"))
(.lastModified (io/file target "classes/resource1.edn")))))))

(deftest merge-with-reader-macros
(let [src (io/file "test/resources")
target (io/file "target/tests/builder-test/reader-macros")
_ (builder/copy-files #{(io/file src "lib5")
(io/file src "lib6")}
target)]

(testing "edn files with reader macros are merged"
(is (= (slurp (io/file target "classes" "with-reader-macros.edn"))
"{:k1 :override-k1, :k2 #unknown/macro :v2, :k3 #weird/macro :v3}")))))

(defn get-file-names
[^File dir]
(map #(.getName %) (.listFiles dir)))
Expand Down