make source type configurable

This commit is contained in:
jem 2020-02-07 20:22:53 +01:00
parent b6d1b9adcd
commit 24b55040f0
2 changed files with 38 additions and 30 deletions

View file

@ -35,21 +35,23 @@
(defn create-resource (defn create-resource
([virtual-path ([virtual-path
java-path] java-path
source-type]
(if (nil? java-path) (if (nil? java-path)
nil nil
{:virtual-path virtual-path {:virtual-path virtual-path
:java-uri (.toUri java-path) :java-uri (.toUri java-path)
:java-path java-path :java-path java-path
:source-type :filesystem :source-type source-type
:resource-type (cond :resource-type (cond
(Files/isDirectory java-path follow-link-option) :dir (Files/isDirectory java-path follow-link-option) :dir
(Files/isRegularFile java-path follow-link-option) :file (Files/isRegularFile java-path follow-link-option) :file
:else :unknown)})) :else :unknown)}))
([fs-prefix ([fs-prefix
base-path base-path
virtual-path] virtual-path
(create-resource virtual-path (path-if-exists fs-prefix base-path virtual-path)))) source-type]
(create-resource virtual-path (path-if-exists fs-prefix base-path virtual-path) source-type)))
(defn (defn
list-entries-for-dir list-entries-for-dir
@ -57,28 +59,34 @@
(.list (.toFile (:java-path resource)))) (.list (.toFile (:java-path resource))))
(defn get-resources ;:- [Resource] (defn get-resources ;:- [Resource]
[fs-prefix ;:- Prefix ([fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
paths ;:- [VirtualPath] paths ;:- [VirtualPath]
] source-type]
(loop [paths paths (loop [paths paths
result []] result []]
(if (not (empty? paths)) (if (not (empty? paths))
(let [path-to-work-with (first paths) (let [path-to-work-with (first paths)
resource-to-work-with (create-resource resource-to-work-with (create-resource
fs-prefix fs-prefix
base-path base-path
path-to-work-with) path-to-work-with
result (into result source-type)
[resource-to-work-with])] result (into result
(cond [resource-to-work-with])]
(nil? resource-to-work-with) (recur (drop 1 paths) result) (cond
(type/is-file? resource-to-work-with) (nil? resource-to-work-with) (recur (drop 1 paths) result)
(recur (drop 1 paths) result) (type/is-file? resource-to-work-with)
(type/is-dir? resource-to-work-with) (recur (drop 1 paths) result)
(recur (into (drop 1 paths) (type/is-dir? resource-to-work-with)
(map #(str path-to-work-with "/" %) (recur (into (drop 1 paths)
(list-entries-for-dir resource-to-work-with))) (map #(str path-to-work-with "/" %)
result) (list-entries-for-dir resource-to-work-with)))
:else [])) result)
(remove nil? result)))) :else []))
(remove nil? result))))
([fs-prefix ;:- Prefix
base-path ;:- VirtualPath
paths ;:- [VirtualPath]
]
(get-resources fs-prefix base-path paths :filesystem)))

View file

@ -35,7 +35,7 @@
(= ["dummy2" "dummy_from_fs"] (= ["dummy2" "dummy_from_fs"]
(seq (seq
(sut/list-entries-for-dir (sut/list-entries-for-dir
(sut/create-resource "dummy" (sut/path-if-exists fs-root "dummy"))))))) (sut/create-resource "dummy" (sut/path-if-exists fs-root "dummy") :filesytem))))))
(deftest test-get-resources (deftest test-get-resources
(is (is