Skip to content

Commit 63488cd

Browse files
authored
Merge pull request #10 from nwjsmith/tagged-literals
Print dates and UUIDs with tagged literals
2 parents 5e87d51 + fe93afb commit 63488cd

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/lambdaisland/deep_diff/printer.clj

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
[puget.dispatch]
66
[puget.printer :as puget]
77
[arrangement.core]
8-
[lambdaisland.deep-diff.diff :as diff]))
8+
[lambdaisland.deep-diff.diff :as diff])
9+
(:import (java.text SimpleDateFormat)
10+
(java.util TimeZone)
11+
(java.sql Timestamp)))
912

1013
(defn print-deletion [printer expr]
1114
(let [no-color (assoc printer :print-color false)]
@@ -34,6 +37,38 @@
3437
[:align (interpose [:span (:map-delimiter this) :line] entries)]
3538
(color/document this :delimiter "}")]))
3639

40+
(def ^:private ^ThreadLocal thread-local-utc-date-format
41+
(proxy [ThreadLocal] []
42+
(initialValue []
43+
(doto (SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ss.SSS-00:00")
44+
(.setTimeZone (TimeZone/getTimeZone "GMT"))))))
45+
46+
(def ^:private print-date
47+
(puget/tagged-handler
48+
'inst
49+
#(.format ^SimpleDateFormat (.get thread-local-utc-date-format) %)))
50+
51+
(def ^:private ^ThreadLocal thread-local-utc-timestamp-format
52+
(proxy [ThreadLocal] []
53+
(initialValue []
54+
(doto (SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ss")
55+
(.setTimeZone (TimeZone/getTimeZone "GMT"))))))
56+
57+
(def ^:private print-timestamp
58+
(puget/tagged-handler
59+
'inst
60+
#(str (.format ^SimpleDateFormat (.get thread-local-utc-timestamp-format) %)
61+
(format ".%09d-00:00" (.getNanos ^Timestamp %)))))
62+
63+
(def ^:private print-calendar
64+
(puget/tagged-handler
65+
'inst
66+
#(let [formatted (format "%1$tFT%1$tT.%1$tL%1$tz" %)
67+
offset-minutes (- (.length formatted) 2)]
68+
(str (subs formatted 0 offset-minutes)
69+
":"
70+
(subs formatted offset-minutes)))))
71+
3772
(def ^:private print-handlers
3873
{'lambdaisland.deep_diff.diff.Deletion
3974
print-deletion
@@ -72,7 +107,19 @@
72107
[:span
73108
(puget/format-doc printer k)
74109
(if (coll? v) (:map-coll-separator printer) " ")
75-
(puget/format-doc printer v)]))))})
110+
(puget/format-doc printer v)]))))
111+
112+
'java.util.Date
113+
print-date
114+
115+
'java.util.GregorianCalendar
116+
print-calendar
117+
118+
'java.sql.Timestamp
119+
print-timestamp
120+
121+
'java.util.UUID
122+
(puget/tagged-handler 'uuid str)})
76123

77124
(defn- print-handler-resolver [extra-handlers]
78125
(fn [^Class klz]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(ns lambdaisland.deep-diff.printer-test
2+
(:require [clojure.test :refer :all]
3+
[lambdaisland.deep-diff.diff :as diff]
4+
[lambdaisland.deep-diff.printer :as printer])
5+
(:import (java.sql Timestamp)
6+
(java.util Date
7+
GregorianCalendar
8+
TimeZone)))
9+
10+
(defn- printed
11+
[diff]
12+
(let [printer (printer/puget-printer {})]
13+
(with-out-str (-> diff
14+
(printer/format-doc printer)
15+
(printer/print-doc printer)))))
16+
17+
(defn- calendar
18+
[date]
19+
(doto (GregorianCalendar. (TimeZone/getTimeZone "GMT"))
20+
(.setTime date)))
21+
22+
(deftest print-doc-test
23+
(testing "date"
24+
(is (= "\u001B[31m-#inst \"2019-04-09T14:57:46.128-00:00\"\u001B[0m \u001B[32m+#inst \"2019-04-10T14:57:46.128-00:00\"\u001B[0m\n"
25+
(printed (diff/diff #inst "2019-04-09T14:57:46.128-00:00"
26+
#inst "2019-04-10T14:57:46.128-00:00")))))
27+
28+
(testing "timestamp"
29+
(is (= "\u001B[31m-#inst \"1970-01-01T00:00:00.000000000-00:00\"\u001B[0m \u001B[32m+#inst \"1970-01-01T00:00:01.000000101-00:00\"\u001B[0m\n"
30+
(printed (diff/diff (Timestamp. 0)
31+
(doto (Timestamp. 1000) (.setNanos 101)))))))
32+
33+
(testing "calendar"
34+
(is (= "\u001B[31m-#inst \"1970-01-01T00:00:00.000+00:00\"\u001B[0m \u001B[32m+#inst \"1970-01-01T00:00:01.001+00:00\"\u001B[0m\n"
35+
(printed (diff/diff (calendar (Date. 0)) (calendar (Date. 1001)))))))
36+
37+
(testing "uuid"
38+
(is (= "\u001B[31m-#uuid \"e41b325a-ce9d-4fdd-b51d-280d9c91314d\"\u001B[0m \u001B[32m+#uuid \"0400be9a-619f-4c6a-a735-6245e4955995\"\u001B[0m\n"
39+
(printed (diff/diff #uuid "e41b325a-ce9d-4fdd-b51d-280d9c91314d"
40+
#uuid "0400be9a-619f-4c6a-a735-6245e4955995"))))))

0 commit comments

Comments
 (0)