|
14 | 14 | (java.util TimeZone) |
15 | 15 | (java.sql Timestamp)))) |
16 | 16 |
|
| 17 | +(defn get-type-name |
| 18 | + "Get the type of the given object as a string. For Clojure, gets the name of |
| 19 | + the class of the object. For ClojureScript, gets either the `name` attribute |
| 20 | + or the protocol name if the `name` attribute doesn't exist." |
| 21 | + [x] |
| 22 | + #?(:clj (.getName (class x)) |
| 23 | + :cljs (let [t (type x) |
| 24 | + n (.-name t)] |
| 25 | + (if (empty? n) |
| 26 | + (pr-str t) |
| 27 | + n)))) |
| 28 | + |
17 | 29 | (defn print-deletion [printer expr] |
18 | 30 | (let [no-color (assoc printer :print-color false)] |
19 | 31 | (color/document printer ::deletion [:span "-" (puget/format-doc no-color (:- expr))]))) |
|
41 | 53 | [:align (interpose [:span (:map-delimiter this) :line] entries)] |
42 | 54 | (color/document this :delimiter "}")])) |
43 | 55 |
|
| 56 | +(defn- map-entry-handler [printer value] |
| 57 | + (let [k (key value) |
| 58 | + v (val value)] |
| 59 | + (let [no-color (assoc printer :print-color false)] |
| 60 | + (cond |
| 61 | + (instance? lambdaisland.deep_diff.diff.Insertion k) |
| 62 | + [:span |
| 63 | + (print-insertion printer k) |
| 64 | + (if (coll? v) (:map-coll-separator printer) " ") |
| 65 | + (color/document printer ::insertion (puget/format-doc no-color v))] |
| 66 | + |
| 67 | + (instance? lambdaisland.deep_diff.diff.Deletion k) |
| 68 | + [:span |
| 69 | + (print-deletion printer k) |
| 70 | + (if (coll? v) (:map-coll-separator printer) " ") |
| 71 | + (color/document printer ::deletion (puget/format-doc no-color v))] |
| 72 | + |
| 73 | + :else |
| 74 | + [:span |
| 75 | + (puget/format-doc printer k) |
| 76 | + (if (coll? v) (:map-coll-separator printer) " ") |
| 77 | + (puget/format-doc printer v)])))) |
| 78 | + |
44 | 79 | (def ^:private ^ThreadLocal thread-local-utc-date-format |
45 | 80 | (proxy [ThreadLocal] [] |
46 | 81 | (initialValue [] |
|
70 | 105 | (format ".%09d-00:00" (.getNanos ^Timestamp %))) |
71 | 106 | :cljs (fn [input-date] |
72 | 107 | (let [dt (from-date input-date)] |
73 | | - (cljs-time.format/unparse thread-local-utc-timestamp-format dt)))))) ;;TODO format ".%09d-00:00" |
| 108 | + (cljs-time.format/unparse thread-local-utc-timestamp-format dt)))))) |
74 | 109 |
|
75 | 110 | (def ^:private print-calendar |
76 | 111 | (puget/tagged-handler |
|
81 | 116 | ":" |
82 | 117 | (subs formatted offset-minutes))))) |
83 | 118 |
|
84 | | -(def ^:private print-handlers |
| 119 | +(def ^:private common-handlers |
85 | 120 | {'lambdaisland.deep_diff.diff.Deletion |
86 | 121 | print-deletion |
87 | 122 |
|
88 | 123 | 'lambdaisland.deep_diff.diff.Insertion |
89 | 124 | print-insertion |
90 | 125 |
|
91 | 126 | 'lambdaisland.deep_diff.diff.Mismatch |
92 | | - print-mismatch |
93 | | - |
94 | | - #?(:clj 'clojure.lang.PersistentArrayMap |
95 | | - :cljs 'cljs.core/PersistentArrayMap) |
96 | | - map-handler |
97 | | - |
98 | | - #?(:clj 'clojure.lang.PersistentHashMap |
99 | | - :cljs cljs.core/PersistentHashMap) |
100 | | - map-handler |
101 | | - |
102 | | - #?(:clj 'clojure.lang.MapEntry |
103 | | - :cljs 'cljs.core/MapEntry) |
104 | | - (fn [printer value] |
105 | | - (let [k (key value) |
106 | | - v (val value)] |
107 | | - (let [no-color (assoc printer :print-color false)] |
108 | | - (cond |
109 | | - (instance? lambdaisland.deep_diff.diff.Insertion k) |
110 | | - [:span |
111 | | - (print-insertion printer k) |
112 | | - (if (coll? v) (:map-coll-separator printer) " ") |
113 | | - (color/document printer ::insertion (puget/format-doc no-color v))] |
114 | | - |
115 | | - (instance? lambdaisland.deep_diff.diff.Deletion k) |
116 | | - [:span |
117 | | - (print-deletion printer k) |
118 | | - (if (coll? v) (:map-coll-separator printer) " ") |
119 | | - (color/document printer ::deletion (puget/format-doc no-color v))] |
120 | | - |
121 | | - :else |
122 | | - [:span |
123 | | - (puget/format-doc printer k) |
124 | | - (if (coll? v) (:map-coll-separator printer) " ") |
125 | | - (puget/format-doc printer v)])))) |
126 | | - |
127 | | - #?(:clj 'java.util.Date |
128 | | - :cljs 'js/Date) |
129 | | - print-date |
130 | | - |
131 | | - ;; 'java.util.GregorianCalendar |
132 | | - ;; print-calendar |
133 | | - |
134 | | - ;; 'java.sql.Timestamp |
135 | | - ;; print-timestamp |
136 | | - |
137 | | - #?(:clj 'java.util.UUID |
138 | | - :cljs 'cljs.core.uuid) |
139 | | - (tagged-handler 'uuid str)}) |
| 127 | + print-mismatch}) |
| 128 | + |
| 129 | +#?(:clj |
| 130 | + (def ^:private print-handlers |
| 131 | + {'clojure.lang.PersistentArrayMap |
| 132 | + map-handler |
| 133 | + |
| 134 | + 'clojure.lang.PersistentHashMap |
| 135 | + map-handler |
| 136 | + |
| 137 | + 'clojure.lang.MapEntry |
| 138 | + map-entry-handler |
| 139 | + |
| 140 | + 'java.util.Date |
| 141 | + print-date |
| 142 | + |
| 143 | + 'java.util.GregorianCalendar |
| 144 | + print-calendar |
| 145 | + |
| 146 | + 'java.sql.Timestamp |
| 147 | + print-timestamp}) |
| 148 | + :cljs |
| 149 | + (def ^:private print-handlers |
| 150 | + {'cljs.core.PersistentArrayMap |
| 151 | + map-handler |
| 152 | + |
| 153 | + 'cljs.core.PersistentHashMap |
| 154 | + map-handler |
| 155 | + |
| 156 | + 'cljs.core.MapEntry |
| 157 | + map-entry-handler |
| 158 | + |
| 159 | + 'js/Date |
| 160 | + print-date |
| 161 | + |
| 162 | + 'cljs.core.uuid |
| 163 | + (puget/tagged-handler 'uuid str)})) |
| 164 | + |
140 | 165 |
|
141 | 166 | (defn- print-handler-resolver [extra-handlers] |
142 | 167 | (fn [^Class klz] |
143 | | - (and klz (get (merge @#'print-handlers extra-handlers) |
144 | | - (symbol (.getName klz)))))) |
| 168 | + (and klz (get (merge @#'common-handlers @#'print-handlers extra-handlers) |
| 169 | + (symbol (get-type-name klz)))))) |
145 | 170 |
|
146 | | -(defn register-print-handler! |
147 | | - "Register an extra print handler. |
| 171 | +;; (defn register-print-handler! |
| 172 | +;; "Register an extra print handler. |
148 | 173 |
|
149 | | - `type` must be a symbol of the fully qualified class name. `handler` is a |
150 | | - Puget handler function of two arguments, `printer` and `value`." |
151 | | - [type handler] |
152 | | - (alter-var-root #'print-handlers assoc type handler)) |
| 174 | +;; `type` must be a symbol of the fully qualified class name. `handler` is a |
| 175 | +;; Puget handler function of two arguments, `printer` and `value`." |
| 176 | +;; [type handler] |
| 177 | +;; (alter-var-root #'print-handlers assoc type handler)) |
153 | 178 |
|
154 | 179 | (defn puget-printer |
155 | 180 | ([] |
|
0 commit comments