-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.clj
156 lines (133 loc) · 5.38 KB
/
install.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(ns java-time-dte.install
(:require [datomic-type-extensions.types :as types])
(:import [java.time DayOfWeek Duration Instant LocalDate LocalDateTime LocalTime Month MonthDay OffsetDateTime OffsetTime Period Year YearMonth ZonedDateTime ZoneId ZoneOffset]
[java.time.temporal ChronoField ChronoUnit]))
(defmacro define-dte [id backing-type serialize-sig serialize-body deserialize-sig deserialize-body]
`(do
(defmethod types/get-backing-datomic-type ~id [_#] ~backing-type)
(defmethod types/serialize ~id [_# ~@serialize-sig] ~serialize-body)
(defmethod types/deserialize ~id [_# ~@deserialize-sig] ~deserialize-body)))
(define-dte :java.time/duration :db.type/string
[^Duration this] (.toString this)
[^String s] (Duration/parse s))
(define-dte :java.time/instant :db.type/instant
[^Instant this] (java.util.Date/from this)
[^java.util.Date inst] (Instant/ofEpochMilli (.getTime inst)))
(define-dte :java.time/local-date :db.type/string
[^LocalDate this] (.toString this)
[^String s] (LocalDate/parse s))
(define-dte :java.time/local-date-time :db.type/string
[^LocalDateTime this] (.toString this)
[^String s] (LocalDateTime/parse s))
(define-dte :java.time/local-time :db.type/string
[^LocalTime this] (.toString this)
[^String s] (LocalTime/parse s))
(define-dte :java.time/month-day :db.type/string
[^MonthDay this] (.toString this)
[^String s] (MonthDay/parse s))
(define-dte :java.time/offset-date-time :db.type/string
[^OffsetDateTime this] (.toString this)
[^String s] (OffsetDateTime/parse s))
(define-dte :java.time/offset-time :db.type/string
[^OffsetTime this] (.toString this)
[^String s] (OffsetTime/parse s))
(define-dte :java.time/period :db.type/string
[^Period this] (.toString this)
[^String s] (Period/parse s))
(define-dte :java.time/year :db.type/string
[^Year this] (.toString this)
[^String s] (Year/parse s))
(define-dte :java.time/year-month :db.type/string
[^YearMonth this] (.toString this)
[^String s] (YearMonth/parse s))
(define-dte :java.time/zoned-date-time :db.type/string
[^ZonedDateTime this] (.toString this)
[^String s] (ZonedDateTime/parse s))
(define-dte :java.time/zone-id :db.type/string
[^ZoneId this] (.toString this)
[^String s] (ZoneId/of s))
(define-dte :java.time/zone-offset :db.type/string
[^ZoneOffset this] (.toString this)
[^String s] (ZoneOffset/of s))
;; enums
(defmacro define-enum [id class enum-map]
(let [lookup (gensym)
reverse-lookup (gensym)]
`(do
(def ~lookup ~enum-map)
(def ~reverse-lookup
(into {} (map (juxt second first) ~lookup)))
(define-dte ~id :db.type/keyword
[this#] (or (~reverse-lookup this#)
(throw (Exception. (str "Unknown constant " this# " for enum " ~(str class)))))
[kw#] (or (~lookup kw#)
(throw (Exception. (str kw# " is not a constant in enum " ~(str class)))))))))
(define-enum :java.time/chrono-unit ChronoUnit
{:centuries ChronoUnit/CENTURIES
:days ChronoUnit/DAYS
:decades ChronoUnit/DECADES
:eras ChronoUnit/ERAS
:forever ChronoUnit/FOREVER
:half-days ChronoUnit/HALF_DAYS
:hours ChronoUnit/HOURS
:micros ChronoUnit/MICROS
:millennia ChronoUnit/MILLENNIA
:millis ChronoUnit/MILLIS
:minutes ChronoUnit/MINUTES
:months ChronoUnit/MONTHS
:nanos ChronoUnit/NANOS
:seconds ChronoUnit/SECONDS
:weeks ChronoUnit/WEEKS
:years ChronoUnit/YEARS})
(define-enum :java.time/chrono-field ChronoField
{:aligned-day-of-week-in-month ChronoField/ALIGNED_DAY_OF_WEEK_IN_MONTH
:aligned-day-of-week-in-year ChronoField/ALIGNED_DAY_OF_WEEK_IN_YEAR
:aligned-week-of-month ChronoField/ALIGNED_WEEK_OF_MONTH
:aligned-week-of-year ChronoField/ALIGNED_WEEK_OF_YEAR
:ampm-of-day ChronoField/AMPM_OF_DAY
:clock-hour-of-ampm ChronoField/CLOCK_HOUR_OF_AMPM
:clock-hour-of-day ChronoField/CLOCK_HOUR_OF_DAY
:day-of-month ChronoField/DAY_OF_MONTH
:day-of-week ChronoField/DAY_OF_WEEK
:day-of-year ChronoField/DAY_OF_YEAR
:epoch-day ChronoField/EPOCH_DAY
:era ChronoField/ERA
:hour-of-ampm ChronoField/HOUR_OF_AMPM
:hour-of-day ChronoField/HOUR_OF_DAY
:instant-seconds ChronoField/INSTANT_SECONDS
:micro-of-day ChronoField/MICRO_OF_DAY
:micro-of-second ChronoField/MICRO_OF_SECOND
:milli-of-day ChronoField/MILLI_OF_DAY
:milli-of-second ChronoField/MILLI_OF_SECOND
:minute-of-day ChronoField/MINUTE_OF_DAY
:minute-of-hour ChronoField/MINUTE_OF_HOUR
:month-of-year ChronoField/MONTH_OF_YEAR
:nano-of-day ChronoField/NANO_OF_DAY
:nano-of-second ChronoField/NANO_OF_SECOND
:offset-seconds ChronoField/OFFSET_SECONDS
:proleptic-month ChronoField/PROLEPTIC_MONTH
:second-of-day ChronoField/SECOND_OF_DAY
:second-of-minute ChronoField/SECOND_OF_MINUTE
:year ChronoField/YEAR
:year-of-era ChronoField/YEAR_OF_ERA})
(define-enum :java.time/month Month
{:january Month/JANUARY
:february Month/FEBRUARY
:march Month/MARCH
:april Month/APRIL
:may Month/MAY
:june Month/JUNE
:july Month/JULY
:august Month/AUGUST
:september Month/SEPTEMBER
:october Month/OCTOBER
:november Month/NOVEMBER
:december Month/DECEMBER})
(define-enum :java.time/day-of-week DayOfWeek
{:monday DayOfWeek/MONDAY
:tuesday DayOfWeek/TUESDAY
:wednesday DayOfWeek/WEDNESDAY
:thursday DayOfWeek/THURSDAY
:friday DayOfWeek/FRIDAY
:saturday DayOfWeek/SATURDAY
:sunday DayOfWeek/SUNDAY})