Skip to content

Commit f4c082d

Browse files
committed
Switch from java.time to clj-time to keep JVM 1.7 compatibility
Updated dependencies and fixed an Eastwood issue.
1 parent 781907d commit f4c082d

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
lines changed

project.clj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject monglorious "0.5.1"
1+
(defproject monglorious "0.6.0"
22
:author "Dave Bauman"
33
:description "Query MongoDB using strings!"
44
:url "https://github.com/baumandm/monglorious"
@@ -7,19 +7,20 @@
77
:distribution :repo}
88

99
:dependencies [[org.clojure/clojure "1.8.0"]
10-
[instaparse "1.4.3"]
10+
[instaparse "1.4.5"]
1111
[com.novemberain/monger "3.1.0"]
12-
[org.clojars.frozenlock/commons-lang "3.3.0"]]
12+
[org.clojars.frozenlock/commons-lang "3.3.0"]
13+
[clj-time "0.13.0"]]
1314

1415
:target-path "target/%s"
1516
:javac-options ["-target" "1.6" "-source" "1.6" "-Xlint:-options"]
1617

1718
:profiles {:dev {:dependencies [[midje "1.8.3"]
18-
[org.slf4j/slf4j-nop "1.7.12"]]
19+
[org.slf4j/slf4j-nop "1.7.24"]]
1920
:plugins [[lein-midje "3.2.1"]
20-
[lein-kibit "0.1.2"]
21+
[lein-kibit "0.1.3"]
2122
[jonase/eastwood "0.2.3"]
22-
[lein-codox "0.10.2"]]}
23+
[lein-codox "0.10.3"]]}
2324

2425
:uberjar {:aot :all}}
2526

src/monglorious/parser.clj

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
[instaparse.transform :as insta-transform]
55
[monger.conversion :refer [from-db-object]]
66
[clojure.string :refer [lower-case]]
7-
[clojure.walk :refer [postwalk]])
7+
[clojure.walk :refer [postwalk]]
8+
[clj-time.core :as t]
9+
[clj-time.format :as f]
10+
[clj-time.coerce :as c])
811
(:import (org.apache.commons.lang3 StringEscapeUtils)
912
(java.util Date)
10-
(java.time Instant ZonedDateTime)
11-
(java.time.format DateTimeFormatter)))
13+
(java.text SimpleDateFormat)))
1214

1315
(defn- flatten-map
1416
[form]
@@ -62,14 +64,13 @@
6264
:objectid (fn [value] (if (nil? value)
6365
(monger.util/object-id)
6466
(monger.util/object-id value)))
65-
:date-string (fn [& _] (let [now (ZonedDateTime/now)
66-
dtf (DateTimeFormatter/ofPattern "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('zzz')'")]
67-
(.format now dtf)))
68-
:new-date (fn [& _] (Date.))
67+
:date-string (fn [& _] (.format (SimpleDateFormat. "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('zzz')'") (Date.)))
68+
:new-date (fn [& _] (c/to-date (t/now)))
6969
:isodate (fn [& args]
70-
(if (zero? (count args))
71-
(Date.)
72-
(Date/from (Instant/parse (first args)))))
70+
(c/to-date
71+
(if (zero? (count args))
72+
(t/now)
73+
(f/parse (first args)))))
7374
:db-object (fn [db-object] (first db-object))
7475
:function-application (fn [name & args] (into [(lower-case name)] args))
7576
:regex (fn [& args] (zipmap ["$regex" "$options"] args))
@@ -90,7 +91,8 @@
9091
([query start]
9192
(let [tree (monglorious-parser query :start start)]
9293
(if (insta/failure? tree)
93-
(throw (Exception. ^String (with-out-str (print tree))))
94+
(let [^String tree-str (with-out-str (print tree))]
95+
(throw (Exception. tree-str)))
9496
tree))))
9597

9698
(defn parse-query

test/monglorious/parser_test.clj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
(ns monglorious.parser-test
2-
(:require [monglorious.parser :refer :all])
2+
(:require [monglorious.parser :refer :all]
3+
[clj-time.format :as f]
4+
[clj-time.coerce :as c])
35
(:use midje.sweet)
46
(:import (java.util Date)
5-
(java.time Instant ZonedDateTime)
6-
(java.time.format DateTimeFormatter)))
7+
(java.text SimpleDateFormat)))
78

89
;; Test parse-query()
910

@@ -51,11 +52,11 @@
5152
(parse-query "Date()" :literal) => (fn [[date]]
5253
(and
5354
(string? date)
54-
(= (.format (ZonedDateTime/now) (DateTimeFormatter/ofPattern "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('zzz')'")) date)))
55+
(= (.format (SimpleDateFormat. "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('zzz')'") (Date.)) date)))
5556
(parse-query "new Date()" :literal) => (fn [[date]] (instance? Date date))
5657
(parse-query "ISODate()" :literal) => (fn [[date]] (instance? Date date))
5758
(parse-query "new ISODate()" :literal) => (fn [[date]] (instance? Date date))
58-
(parse-query "ISODate('2017-02-25T15:23:59.340Z')" :literal) => (fn [[date]] (= date (Date/from (Instant/parse "2017-02-25T15:23:59.340Z")))))
59+
(parse-query "ISODate('2017-02-25T15:23:59.340Z')" :literal) => (fn [[date]] (= date (c/to-date (f/parse "2017-02-25T15:23:59.340Z")))))
5960

6061
(fact "Monglorious parses SHOW ___ commands"
6162
(parse-query "show dbs") => [:show-command :dbs]

test/monglorious/transforms_test.clj

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
(:require [monglorious.core :refer :all]
33
[monger.core :as mg]
44
[monger.collection :as mc]
5-
[monglorious.test-helpers :refer :all])
5+
[monglorious.test-helpers :refer :all]
6+
[clj-time.core :as t]
7+
[clj-time.format :as f]
8+
[clj-time.coerce :as c])
69
(:use midje.sweet)
7-
(:import (com.mongodb MongoQueryException)
8-
(java.util Date)
9-
(java.time Instant)
10-
(java.time.temporal ChronoUnit)))
10+
(:import (com.mongodb MongoQueryException)))
1111

1212
;; Test run-command-transform()
1313
(against-background
@@ -259,18 +259,18 @@
259259
[(before :contents
260260
(let [conn (mg/connect)
261261
db (mg/get-db conn "testdb")
262-
now (Instant/now)]
262+
now (t/now)]
263263
(mc/remove db "timeDocs")
264-
(mc/insert-batch db "timeDocs" [{:time (Date/from (.plus now 2 ChronoUnit/HOURS)) :metric "queriesRan" :value 0}
265-
{:time (Date/from (.plus now 1 ChronoUnit/HOURS)) :metric "queriesRan" :value 1}
266-
{:time (Date/from now) :metric "queriesRan" :value 2}
267-
{:time (Date/from (.minus now 1 ChronoUnit/HOURS)) :metric "queriesRan" :value 3}
268-
{:time (Date/from (.minus now 2 ChronoUnit/HOURS)) :metric "queriesRan" :value 4}
269-
{:time (Date/from (.minus now 3 ChronoUnit/HOURS)) :metric "queriesRan" :value 5}
270-
{:time (Date/from (.minus now 4 ChronoUnit/HOURS)) :metric "queriesRan" :value 6}
271-
{:time (Date/from (.minus now 5 ChronoUnit/HOURS)) :metric "queriesRan" :value 7}
272-
{:time (Date/from (Instant/parse "2017-02-15T00:00:00.000Z")) :metric "queriesRan" :value 8}
273-
{:time (Date/from (Instant/parse "2017-02-14T20:45:01.000Z")) :metric "queriesRan" :value 8}])))
264+
(mc/insert-batch db "timeDocs" [{:time (c/to-date (t/plus now (t/hours 2))) :metric "queriesRan" :value 0}
265+
{:time (c/to-date (t/plus now (t/hours 1))) :metric "queriesRan" :value 1}
266+
{:time (c/to-date now) :metric "queriesRan" :value 2}
267+
{:time (c/to-date (t/minus now (t/hours 1))) :metric "queriesRan" :value 3}
268+
{:time (c/to-date (t/minus now (t/hours 2))) :metric "queriesRan" :value 4}
269+
{:time (c/to-date (t/minus now (t/hours 3))) :metric "queriesRan" :value 5}
270+
{:time (c/to-date (t/minus now (t/hours 4))) :metric "queriesRan" :value 6}
271+
{:time (c/to-date (t/minus now (t/hours 5))) :metric "queriesRan" :value 7}
272+
{:time (c/to-date (f/parse "2017-02-15T00:00:00.000Z")) :metric "queriesRan" :value 8}
273+
{:time (c/to-date (f/parse "2017-02-14T20:45:01.000Z")) :metric "queriesRan" :value 8}])))
274274
(after :contents
275275
(let [conn (mg/connect)]
276276
(mg/drop-db conn "testdb")))]
@@ -283,4 +283,5 @@
283283
(execute {} "testdb" "db.timeDocs.find({ time: { $gt: new ISODate() } }).count()") => 2
284284
(execute {} "testdb" "db.timeDocs.find({ time: { $lt: new ISODate() } }).count()") => 8
285285
(execute {} "testdb" "db.timeDocs.find({ time: { $lt: new ISODate('2017-02-15T00:00:00.000Z') } }).count()") => 1
286-
(execute {} "testdb" "db.timeDocs.find({ time: { $lte: new ISODate('2017-02-15T00:00:00.000Z') } }).count()") => 2))
286+
(execute {} "testdb" "db.timeDocs.find({ time: { $lte: new ISODate('2017-02-15T00:00:00.000Z') } }).count()") => 2
287+
(execute {} "testdb" "db.timeDocs.find({ time: { $lt: new ISODate('2017-02-17') } }).count()") => 2))

0 commit comments

Comments
 (0)