-
Notifications
You must be signed in to change notification settings - Fork 28
/
optionals.clj
40 lines (33 loc) · 1.15 KB
/
optionals.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
(ns cli-matic.optionals
(:require [clojure.string :as str]))
;; This namespace contains optional libraries.
;; CHESHIRE
;; ---------------
;; Cheshire is an optional dependency, so we check for it at compile time.
;; Taken from core.clj in https://github.com/dakrone/clj-http
(def with-cheshire?
(try
(require 'cheshire.core)
true
(catch Throwable _ false)))
(defn ^:dynamic json-decode-cheshire
"Resolve and apply cheshire's json decoding dynamically."
[& args]
{:pre [with-cheshire?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "decode")) args))
;; ---------------
;; YAML is an optional dependency, so we check for it at compile time.
;; Taken from core.clj in https://github.com/dakrone/clj-http
(def with-yaml?
(try
(require 'yaml.core)
true
(catch Throwable _ false)))
(defn ^:dynamic yaml-decode
"Resolve and apply io.forward/yaml's yaml decoding dynamically."
[& args]
{:pre [with-yaml?]}
((ns-resolve (symbol "yaml.core") (symbol "parse-string"))
(if (string? args) args (str/join args))
:keywords identity
:constructor (ns-resolve (symbol "yaml.reader") (symbol "passthrough-constructor"))))