-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
edn.cljc
36 lines (32 loc) · 1.05 KB
/
edn.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(ns portal.runtime.edn
(:refer-clojure :exclude [read-string])
(:require [clojure.edn :as edn]
[clojure.string :as str]
[clojure.walk :as w]
[portal.runtime.cson :as cson]))
;; Discard metadata on tagged-literals to improve success rate of read-string.
;; Consider using a different type in the future.
#?(:cljs
(extend-type cljs.core/TaggedLiteral
IMeta (-meta [_this] nil)
IWithMeta (-with-meta [this _m] this)))
(defn- with-file [v]
#?(:cljs v
:default (cond-> v
*file*
(vary-meta assoc ::file *file*))))
(defn- ->var [var-symbol]
(with-file (cson/tagged-value "portal/var" var-symbol)))
(defn read-string [edn-string]
(w/postwalk
(fn [v]
(if-let [file (::file (meta v))]
(-> v
(vary-meta dissoc ::file)
(vary-meta assoc :file file))
v))
(edn/read-string
{:readers {'portal/var ->var
'portal/bin cson/base64-decode}
:default tagged-literal}
(str/replace edn-string #"#'" "#portal/var "))))