|
8 | 8 |
|
9 | 9 | (ns |
10 | 10 | ^{:doc "Functions to turn objects into data. Alpha, subject to change"} |
11 | | - clojure.datafy) |
| 11 | + clojure.datafy |
| 12 | + (:require [clojure.core.protocols :as p])) |
12 | 13 |
|
13 | 14 | (defn datafy |
14 | | - "Attempts to return x as data. If :clojure.datafy/datafy is present |
15 | | - as metadata of x, it will be called with x as an argument, else |
16 | | - datafy will return the value of clojure.protocols/datafy. If the |
17 | | - value has been transformed and the result supports |
| 15 | + "Attempts to return x as data. |
| 16 | + datafy will return the value of clojure.protocols/datafy. If |
| 17 | + the value has been transformed and the result supports |
18 | 18 | metadata, :clojure.datafy/obj will be set on the metadata to the |
19 | 19 | original value of x." |
20 | 20 | [x] |
21 | | - (let [v ((or (-> x meta ::datafy) -datafy) x)] |
| 21 | + (let [v (p/datafy x)] |
22 | 22 | (if (identical? v x) |
23 | 23 | v |
24 | 24 | (if (implements? IWithMeta v) |
25 | | - (vary-meta v assoc ::obj x) |
| 25 | + (vary-meta v assoc ::obj x |
| 26 | + ;; Circling back to this at a later date per @dnolen |
| 27 | + ;; ::class (-> x .-constructor .-name symbol) |
| 28 | + ) |
26 | 29 | v)))) |
27 | 30 |
|
28 | 31 | (defn nav |
29 | 32 | "Returns (possibly transformed) v in the context of coll and k (a |
30 | 33 | key/index or nil). Callers should attempt to provide the key/index |
31 | 34 | context k for Indexed/Associative/ILookup colls if possible, but not |
32 | | - to fabricate one e.g. for sequences (pass nil). If :clojure.datafy/nav is |
33 | | - present as metadata on coll, it will be called with coll, k and v as |
34 | | - arguments, else nav will call :clojure.protocols/nav." |
| 35 | + to fabricate one e.g. for sequences (pass nil). nav will return the |
| 36 | + value of clojure.core.protocols/nav." |
35 | 37 | [coll k v] |
36 | | - ((or (-> coll meta ::nav) -nav) coll k v)) |
| 38 | + (p/nav coll k v)) |
37 | 39 |
|
38 | 40 | (defn- datify-ref [r] |
39 | 41 | (with-meta [(deref r)] (meta r))) |
40 | 42 |
|
41 | | -(extend-protocol IDatafiable |
| 43 | +(extend-protocol p/Datafiable |
42 | 44 | Var |
43 | | - (-datafy [r] (datify-ref r)) |
| 45 | + (datafy [r] (datify-ref r)) |
44 | 46 |
|
45 | 47 | Reduced |
46 | | - (-datafy [r] (datify-ref r)) |
| 48 | + (datafy [r] (datify-ref r)) |
47 | 49 |
|
48 | 50 | Atom |
49 | | - (-datafy [r] (datify-ref r)) |
| 51 | + (datafy [r] (datify-ref r)) |
50 | 52 |
|
51 | 53 | Volatile |
52 | | - (-datafy [r] (datify-ref r)) |
| 54 | + (datafy [r] (datify-ref r)) |
53 | 55 |
|
54 | 56 | Delay |
55 | | - (-datafy [r] (datify-ref r))) |
| 57 | + (datafy [r] (datify-ref r))) |
0 commit comments