-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
248 lines (209 loc) · 8.13 KB
/
core.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(ns cruler.core
(:require [clojure.java.io :as io]
[clojure.pprint :as pprint]
[clojure.string :as string]
[cruler.spec-parser :as sp]
[cruler.parser :as parser]
[cruler.config :as config]
[cruler.classpath :as classpath]
[io.aviso.ansi :as ansi]))
(defmulti validate
"Validates `data`, returning a result map.
`data` is a vector containing maps. Each map has a file information as
follows:
{:file-path The file path relative to the base directory.
:file-type The file type, any of `:csv`, `:text`, and `:yaml`.
:raw-content The raw content of the file.
:parsed-content The parsed data of `:raw-content`. The structure of
the data depends on `:file-type`.}
`validate` multimethods returns the result map consisting of the following
keys:
{:errors A vector of maps indicating errors. Each error map consists of
{:file-path The file path in which an error occurs.
:error-value The error value.
:error-block A block in `:parsed-content` containing the
error location.
:error-keys Keys in `:error-block` for indicating the
specific error location.}
If there are no errors, set `nil` or an empty sequence.
:message An optional error message.}"
{:arglists '([key data])}
(fn [key _] key))
(defn- filter-files [dir xs]
(->> (file-seq (io/file dir))
(filter (fn [file]
(some #(re-find % (.getPath file)) xs)))
(sort-by #(.getPath %))))
(defn- error-lines
[error-block error-keys]
(let [children-starts (:children-starts (meta error-block))]
(->> (keep #(get children-starts %) error-keys)
(map :line)
distinct)))
(defn- error-block-lines
[error-block error-keys]
(when-let [lines (seq (error-lines error-block error-keys))]
(map inc lines)))
(defn- error-block-message
[lines]
(if lines
(str " line: " (string/join "," lines))
" line: unknown"))
(defn- pprint-str [x]
(pprint/write x :stream nil))
(def ^:private shape-type
(atom :pprint))
(defn- shape-str [x]
(case @shape-type
:print (print-str x)
(pprint-str x)))
(defn- indent [s n]
(->> (string/split-lines s)
(map #(str (apply str (repeat n " ")) %))
(string/join \newline)))
(defn- build-error-str [err]
(str (if (symbol? (:pred err))
(str (:pred err))
(str (apply list (:pred err))))
\newline
(indent (shape-str (:val err)) 4)))
(defn- error-value-message
[error-value]
(if (sp/spec-problem? error-value)
(if-let [readable-error (sp/readable-error error-value)]
(str (apply str readable-error) \newline
(indent (shape-str (:val error-value)) 4))
(build-error-str error-value))
(shape-str error-value)))
(defn- calc-range
[line-num content num]
(let [min-line-num (min (count content) (max (- line-num num) 1))
max-line-num (min (+ line-num num) (count content))]
{:min-line min-line-num :max-line max-line-num}))
(defn- take-range
[content min-line-num max-line-num]
(->> (drop (dec min-line-num) content)
(take (inc (- max-line-num min-line-num)))))
(defn- preview
[content line-num preview-num colorize?]
(let [{:keys [min-line max-line]} (calc-range line-num content preview-num)
line-fmt (str "%" (count (str max-line)) "s")]
(map (fn [l s]
(if (= l line-num)
(cond-> (str (format line-fmt l) " " s)
colorize? ansi/yellow)
(str (cond-> (format line-fmt l)
colorize? ansi/green) " " s)))
(iterate inc min-line)
(take-range content min-line max-line))))
(def ^:private preview-range 2)
(defn- build-preview
[raw-content indices colorize?]
(let [content (string/split-lines raw-content)
valid-indices (->> (remove nil? indices)
(remove zero?)
(remove #(< (count content) %)))]
(str " preview:\n-----\n"
(->> valid-indices
(map #(preview content % preview-range colorize?))
(map #(string/join \newline %))
(string/join "\n-----\n"))
"\n-----")))
(defn- error-indices [error-block error-keys]
(if (seq error-block)
(error-block-lines error-block error-keys)
error-keys))
(defn- build-error-message
[errors message data]
(let [message (when message
(str message \newline))]
(->> errors
(mapcat (fn [{:keys [file-path error-block error-keys error-value]}]
(let [indices (error-indices error-block error-keys)
raw-content (->> (filter #(= file-path (:file-path %)) data)
first
:raw-content)]
[""
file-path
(when error-value
(str " error: " (error-value-message error-value)))
(when (seq error-keys)
(str (error-block-message indices) "\n"
(when (and indices raw-content)
(build-preview raw-content indices @config/colorize))))])))
(remove nil?)
(string/join \newline)
(str message))))
(defn- error-positions
[error-block error-keys]
(let [children-starts (:children-starts (meta error-block))]
(->> (keep #(get children-starts %) error-keys)
(map (fn [{:keys [line column]}]
{:line (inc line)
:column (inc column)})))))
(defn- build-errors [errors message]
(for [{:keys [file-path error-block error-keys error-value]} errors
position (error-positions error-block error-keys)]
{:file-path file-path
:key (:path error-value)
:error (or message (error-value-message error-value))
:line (:line position)
:column (:column position)}))
(defn- build-result
[{:keys [errors message]} key data]
{:type (if (empty? errors) :pass :fail)
:validator key
:message (build-error-message errors message data)
:errors (build-errors errors message)})
(defn- file-type [file]
(condp re-find (.getName file)
#"\.csv$" :csv
#"\.ya?ml$" :yaml
:text))
(defn- build-data1 [file]
(let [file-type (file-type file)
raw-content (slurp file)]
{:file-path (.getPath file)
:file-type file-type
:raw-content raw-content
:parsed-content (case file-type
:csv (parser/parse-csv raw-content)
:text (parser/parse-text raw-content)
:yaml (parser/parse-yaml raw-content))}))
(defn run-validators
[validators base-dir]
(for [[rule patterns] validators]
(do
(require (symbol (namespace rule)))
(let [data (->> (map re-pattern patterns)
(filter-files base-dir)
(mapv build-data1))
result (-> (validate rule data)
(build-result rule data))]
result))))
(defn- match-patterns? [file patterns]
(seq (->> (map re-pattern patterns)
(filter #(re-find % (.getPath file))))))
(defn run-validators-single-file
[validators base-dir filepath]
(let [file (io/file filepath)
file (if (.isAbsolute file)
file
(io/file base-dir file))
data [(build-data1 file)]]
(->> (for [[rule patterns] validators]
(when (match-patterns? file patterns)
(require (symbol (namespace rule)))
(let [result (-> (validate rule data)
(build-result rule data))]
result)))
(remove nil?))))
(defn setup-config
[dir filepath]
(let [[absolute-filepath config] (config/load-config dir filepath)]
(classpath/ensure-dynamic-classloader)
(classpath/add-classpaths dir (:paths config))
(classpath/add-deps (:deps config))
(reset! config/colorize (:colorize config))
(reset! shape-type (get-in config [:format :error-value]))
[absolute-filepath config]))