-
Notifications
You must be signed in to change notification settings - Fork 13
/
yaml.clj
53 lines (43 loc) · 1.94 KB
/
yaml.clj
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns tupelo.parse.yaml
(:use tupelo.core)
(:require
[clojure.walk :as walk]
[schema.core :as s]
[tupelo.schema :as tsk]
)
(:import ; #todo maybe switch to jackson for yaml ???
[org.snakeyaml.engine.v1.api LoadSettingsBuilder Load]
[org.snakeyaml.engine.v1.api DumpSettingsBuilder Dump]
))
;-----------------------------------------------------------------------------
(def ^:private load-settings (-> (LoadSettingsBuilder.)
;(.setLabel "Custom user configuration")
(.build)))
(def ^:private yaml-load (Load. load-settings))
(s/defn parse-all-raw
"Parses a String containing multiple YAML objects, returning a vector of normalized Clojure data structure."
[str-in :- s/Str]
(unlazy (.loadAllFromString yaml-load (s/validate s/Str str-in))))
(s/defn parse-all
"Parses a String containing multiple YAML objects, returning a vector of normalized Clojure data structure (with keywordized map keys)."
[str-in :- s/Str]
(walk/keywordize-keys
(parse-all-raw (s/validate s/Str str-in))))
(s/defn parse-raw
"Parses a String containing a single YAML object, returning a normalized Clojure data structure."
[str-in :- s/Str]
(unlazy (.loadFromString yaml-load (s/validate s/Str str-in))))
(s/defn parse
"Parses a String containing a single YAML object, returning a normalized Clojure data structure (with keywordized map keys)."
[str-in :- s/Str]
(walk/keywordize-keys
(parse-raw (s/validate s/Str str-in))))
;-----------------------------------------------------------------------------
(def ^:private dump-settings (it-> (DumpSettingsBuilder.)
;(.setDefaultScalarStyle it ScalarStyle.DOUBLE_QUOTED)
(.build it)))
(def ^:private yaml-dump (Dump. dump-settings))
(defn encode [it]
"Serializes a Clojure data structure into a YAML string."
(.dumpToString yaml-dump
(walk/stringify-keys it)))