-
Notifications
You must be signed in to change notification settings - Fork 28
/
presets.clj
146 lines (123 loc) · 3.86 KB
/
presets.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(ns cli-matic.presets
(:require [clojure.string :as str]
[clojure.edn :as edn]))
;; Known presets
(defn parseInt
"Converts a string to an integer. "
[s]
(Integer/parseInt s))
(defn parseFloat
"Converts a string to a float."
[s]
(Float/parseFloat s))
(defn asDate
"Converts a string to a Date object; if conversion
fails, returns nil."
[s]
(try
(.parse
(java.text.SimpleDateFormat. "yyyy-MM-dd") s)
(catch Throwable t
nil)))
(defn asSingleString
"Turns a filename into a single string.
If argument is a String, it tries to resolve it first as a URI, then\n as a local file name. URIs with a 'file' protocol are converted to\n local file names.
"
[filename]
(cond
(nil? filename) ""
(empty? filename) ""
:else (slurp filename)))
(defn asLinesString
"Reads a text file and returns it as a collection of lines."
[filename]
(str/split-lines (asSingleString filename)))
(defn asDecodedEdnValue
"Decodes the value as an EDN structure."
[s]
;;(edn/read-string (if (string? s) s (str/join s)))
(edn/read-string s))
(defn asDecodedEdnFile
"Decodes the contents of a file as a JSON object."
[filename]
(edn/read-string (asSingleString filename)))
;; ---------------
;; 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 json-enabled?
(try
(require 'cheshire.core)
true
(catch Throwable _ false)))
(defn ^:dynamic json-decode
"Resolve and apply cheshire's json decoding dynamically."
[& args]
{:pre [json-enabled?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "decode")) args))
(defn asDecodedJsonValue
"Decodes the value as a JSON object."
[s]
(json-decode s))
(defn asDecodedJsonFile
"Decodes the contents of a file as a JSON object."
[filename]
(json-decode (asSingleString filename)))
;; ---------------
;; 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 yaml-enabled?
(try
(require 'yaml.core)
true
(catch Throwable _ false)))
(defn ^:dynamic yaml-decode
"Resolve and apply io.forward/yaml's yaml decoding dynamically."
[& args]
{:pre [yaml-enabled?]}
((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"))))
(defn asDecodedYamlValue
"Decodes the value as a YAML object."
[s]
(yaml-decode s))
(defn asDecodedYamlFile
"Decodes the contents of a file as a JSON object."
[filename]
(yaml-decode (asSingleString filename)))
;; Remember to add these to
;; ::S/type
(def known-presets
{:int {:parse-fn parseInt
:placeholder "N"}
:int-0 {:parse-fn parseInt
:placeholder "N"
:default 0}
:float {:parse-fn parseFloat
:placeholder "N.N"}
:float-0 {:parse-fn parseFloat
:placeholder "N.N"
:default 0.0}
:string {:placeholder "S"}
:slurp {:parse-fn asSingleString
:placeholder "f"}
:slurplines {:parse-fn asLinesString
:placeholder "f"}
:edn {:parse-fn asDecodedEdnValue
:placeholder "edn"}
:ednfile {:parse-fn asDecodedEdnFile
:placeholder "f"}
:json {:parse-fn asDecodedJsonValue
:placeholder "json"}
:jsonfile {:parse-fn asDecodedJsonFile
:placeholder "f"}
:yaml {:parse-fn asDecodedYamlValue
:placeholder "yaml"}
:yamlfile {:parse-fn asDecodedYamlFile
:placeholder "f"}
; dates
:yyyy-mm-dd {:placeholder "YYYY-MM-DD" :parse-fn asDate}
;;:validate [#(true)
;; "Must be a date in format YYYY-MM-DD"]
})