-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for loading modules as json or yaml
- Loading branch information
1 parent
9db5a3e
commit eb9dc62
Showing
6 changed files
with
87 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
(ns k16.kl.api.module.loader | ||
(:require | ||
[clj-yaml.core :as yaml] | ||
[clojure.edn :as edn] | ||
[clojure.string :as str] | ||
[clojure.walk :as walk] | ||
[jsonista.core :as json] | ||
[k16.kl.api.module.schema :as module.schema] | ||
[malli.core :as m] | ||
[malli.error :as me] | ||
[malli.transform :as mt])) | ||
|
||
(defn parse-module-contents [file-name module-contents] | ||
(let [ext (-> file-name (str/split #"\.") last keyword) | ||
|
||
module-data | ||
(cond | ||
(= ext :edn) | ||
(edn/read-string module-contents) | ||
|
||
(= ext :json) | ||
(json/read-value module-contents) | ||
|
||
(or (= ext :yml) | ||
(= ext :yaml)) | ||
(yaml/parse-string module-contents) | ||
|
||
:else (throw (ex-info "Failed to parse module. Unsupported file format" file-name)))] | ||
|
||
(try | ||
(m/coerce module.schema/?PartialModule | ||
(walk/keywordize-keys module-data) | ||
mt/json-transformer) | ||
(catch Exception e | ||
(let [{:keys [type data]} (ex-data e)] | ||
(when-not (= type :malli.core/invalid-input) | ||
(throw e)) | ||
|
||
(throw (ex-info "Module invalid" {:reason (me/humanize (:explain data))}))))))) | ||
|
||
(defn read-module-file [^java.io.File file] | ||
(let [name (.getName file) | ||
contents (slurp file)] | ||
(parse-module-contents name contents))) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters