[WIP] Refactor yaml.cljs

Move redundand functionality from modules to common.
This commit is contained in:
erik 2022-11-08 12:57:54 +01:00
parent ce73df117c
commit d1f4fb942d

View file

@ -3,6 +3,7 @@
["js-yaml" :as yaml] ["js-yaml" :as yaml]
[clojure.string :as st] [clojure.string :as st]
[orchestra.core :refer-macros [defn-spec]] [orchestra.core :refer-macros [defn-spec]]
[shadow.resource :as rc]
[dda.c4k-common.predicate :as cp])) [dda.c4k-common.predicate :as cp]))
(defn-spec from-string cp/map-or-seq? (defn-spec from-string cp/map-or-seq?
@ -10,14 +11,24 @@
(js->clj (yaml/load input) (js->clj (yaml/load input)
:keywordize-keys true)) :keywordize-keys true))
(defn-spec to-string string? (defn-spec to-string string?
[edn cp/map-or-seq?] [edn cp/map-or-seq?]
(yaml/dump (clj->js edn))) (yaml/dump (clj->js edn)))
(defn-spec dispatch-by-resource-name keyword? (defn-spec dispatch-by-resource-name keyword?
[resource string?] [resource string?]
(keyword (first (st/split resource #"/")))) :cljs)
(defmulti load-resource dispatch-by-resource-name) (defmulti load-resource dispatch-by-resource-name)
(defmulti load-as-edn dispatch-by-resource-name) (defmethod load-resource :cljs [allowed-resources resource-name]
(if (some #(= % resource-name) allowed-resources)
(rc/inline resource-name)
(throw (js/Error. "Undefined Resource!"))))
(defmulti load-as-edn dispatch-by-resource-name)
(defmethod load-as-edn :cljs [resource-name]
(yaml/from-string (yaml/load-resource resource-name)))
(defmulti allowed-resources dispatch-by-resource-name)