|
1 | 1 | (ns clj-yaml.core
|
2 | 2 | (:require [flatland.ordered.map :refer (ordered-map)]
|
3 | 3 | [flatland.ordered.set :refer (ordered-set)])
|
4 |
| - (:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle) |
| 4 | + (:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle LoaderOptions) |
5 | 5 | (org.yaml.snakeyaml.constructor Constructor SafeConstructor BaseConstructor)
|
6 | 6 | (org.yaml.snakeyaml.representer Representer)
|
7 | 7 | (org.yaml.snakeyaml.error Mark)
|
|
27 | 27 | (doto (default-dumper-options)
|
28 | 28 | (.setDefaultFlowStyle (flow-styles flow-style))))
|
29 | 29 |
|
| 30 | +(defn ^LoaderOptions default-loader-options |
| 31 | + [] |
| 32 | + (LoaderOptions.)) |
| 33 | + |
| 34 | +(defn ^LoaderOptions make-loader-options |
| 35 | + [& {:keys [max-aliases-for-collections allow-recursive-keys]}] |
| 36 | + (let [loader (default-loader-options)] |
| 37 | + (when max-aliases-for-collections |
| 38 | + (.setMaxAliasesForCollections loader max-aliases-for-collections)) |
| 39 | + (when allow-recursive-keys |
| 40 | + (.setAllowRecursiveKeys loader allow-recursive-keys)) |
| 41 | + loader)) |
| 42 | + |
30 | 43 | (defn ^Yaml make-yaml
|
31 | 44 | "Make a yaml encoder/decoder with some given options."
|
32 |
| - [& {:keys [dumper-options unsafe mark]}] |
33 |
| - (let [^BaseConstructor constructor |
34 |
| - (if unsafe (Constructor.) |
35 |
| - (if mark (MarkedConstructor.) (SafeConstructor.))) |
| 45 | + [& {:keys [dumper-options unsafe mark max-aliases-for-collections allow-recursive-keys]}] |
| 46 | + (let [loader (make-loader-options :max-aliases-for-collections max-aliases-for-collections |
| 47 | + :allow-recursive-keys allow-recursive-keys) |
| 48 | + ^BaseConstructor constructor |
| 49 | + (if unsafe (Constructor. loader) |
| 50 | + (if mark |
| 51 | + ;; construct2ndStep isn't implemented by MarkedConstructor, |
| 52 | + ;; causing an exception to be thrown before loader options are |
| 53 | + ;; used |
| 54 | + (MarkedConstructor.) |
| 55 | + (SafeConstructor. loader))) |
36 | 56 | ;; TODO: unsafe marked constructor
|
37 | 57 | dumper (if dumper-options
|
38 | 58 | (make-dumper-options :flow-style (:flow-style dumper-options))
|
39 | 59 | (default-dumper-options))]
|
40 |
| - (Yaml. constructor (Representer.) dumper))) |
| 60 | + (Yaml. constructor (Representer.) dumper loader))) |
41 | 61 |
|
42 | 62 | (defrecord Marked
|
43 | 63 | [start end unmark])
|
|
126 | 146 | (encode data)))
|
127 | 147 |
|
128 | 148 | (defn parse-string
|
129 |
| - [^String string & {:keys [unsafe mark keywords] :or {keywords true}}] |
130 |
| - (decode (.load (make-yaml :unsafe unsafe :mark mark) string) keywords)) |
| 149 | + [^String string & {:keys [unsafe mark keywords max-aliases-for-collections allow-recursive-keys] :or {keywords true}}] |
| 150 | + (decode (.load (make-yaml :unsafe unsafe |
| 151 | + :mark mark |
| 152 | + :max-aliases-for-collections max-aliases-for-collections |
| 153 | + :allow-recursive-keys allow-recursive-keys) |
| 154 | + string) keywords)) |
0 commit comments