-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessors.clj
41 lines (35 loc) · 1.18 KB
/
preprocessors.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
(ns hicosql.preprocessors
(:require [hicosql.utils :as utils]
[flatland.ordered.map :refer [ordered-map]]
[clojure.string :as string])
(:import (java.io File)))
(defn relative-path
"Get relative source path, drop filename and add another relative path:
\"sql/test.sql\", \"base/common.sql\" => \"sql/base/common.sql\"
"
[source-path path]
(let [parts (string/split source-path (re-pattern File/separator))
path (string/join File/separator (conj (vec (butlast parts)) path))]
path))
(defn include
"Read included file and it values to main values"
[path result include-path]
(if (string? include-path)
(let [include-data (utils/read-yaml (relative-path path include-path))]
(merge result include-data))
;; if it's vector, include each file alternately
(reduce
(fn [res item]
(include path res item ))
result
include-path)))
(defn preprocess
"Preproccess key-values pairs for key started with '__', e.g.: '__include'"
[path data]
(reduce
(fn [result [key value]]
(case key
"__include" (include path result value)
(assoc result key value)))
(ordered-map)
data))