From d1f4fb942d2c76ae45c3b3840b0b8c642a66b3c5 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 8 Nov 2022 12:57:54 +0100 Subject: [PATCH] [WIP] Refactor yaml.cljs Move redundand functionality from modules to common. --- src/main/cljs/dda/c4k_common/yaml.cljs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/cljs/dda/c4k_common/yaml.cljs b/src/main/cljs/dda/c4k_common/yaml.cljs index c8ec13b..11a39e8 100644 --- a/src/main/cljs/dda/c4k_common/yaml.cljs +++ b/src/main/cljs/dda/c4k_common/yaml.cljs @@ -3,6 +3,7 @@ ["js-yaml" :as yaml] [clojure.string :as st] [orchestra.core :refer-macros [defn-spec]] + [shadow.resource :as rc] [dda.c4k-common.predicate :as cp])) (defn-spec from-string cp/map-or-seq? @@ -10,14 +11,24 @@ (js->clj (yaml/load input) :keywordize-keys true)) -(defn-spec to-string string? +(defn-spec to-string string? [edn cp/map-or-seq?] (yaml/dump (clj->js edn))) (defn-spec dispatch-by-resource-name keyword? [resource string?] - (keyword (first (st/split resource #"/")))) + :cljs) (defmulti load-resource dispatch-by-resource-name) -(defmulti load-as-edn dispatch-by-resource-name) \ No newline at end of file +(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) \ No newline at end of file