-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
row.clj
56 lines (45 loc) · 1.62 KB
/
row.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
(ns toucan2.jdbc.row
(:require [potemkin :as p]
[pretty.core :as pretty]
[toucan2.realize :as realize]
#_[toucan2.result-row :as result-row]
[toucan2.util :as u]))
(defn- realize-column-with-thunk [k thunk]
(u/try-with-error-context ["realize column with thunk" {::k k, ::thunk thunk}]
(thunk)))
;; TODO -- instead of thunks, should these just be DELAYS????
(p/def-map-type Row [col-name->thunk mta]
(get [_ k default-value]
(if-let [thunk (get col-name->thunk k)]
(realize-column-with-thunk k thunk)
default-value))
(assoc [_ k v]
(Row. (assoc col-name->thunk k (constantly v)) mta))
(dissoc [_ k]
(Row. (dissoc col-name->thunk k) mta))
(keys [_]
(keys col-name->thunk))
(meta [_]
mta)
(with-meta [_ new-meta]
(Row. col-name->thunk new-meta))
;; result-row/ResultRow
;; (thunks [_]
;; col-name->thunk)
;; (with-thunks [_ new-thunks]
;; (Row. new-thunks mta))
realize/Realize
(realize [_this]
(u/with-debug-result "Realize entire row"
(-> (into {} (for [[col-name thunk] col-name->thunk]
[col-name (realize-column-with-thunk col-name thunk)]))
(with-meta mta))))
pretty/PrettyPrintable
(pretty [_]
(list (pretty/qualify-symbol-for-*ns* `row) col-name->thunk)))
(defn row [col-name->thunk]
;; wrap the thunks in delays so we don't end up invoking them multiple times, since they are potentially expensive.
(Row. (into {} (for [[k thunk] col-name->thunk
:let [dlay (delay (thunk))]]
[k (fn cached-thunk [] @dlay)]))
nil))