-
Notifications
You must be signed in to change notification settings - Fork 13
/
uuid.clj
83 lines (70 loc) · 2.47 KB
/
uuid.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
(ns tupelo.uuid
(:use tupelo.core)
(:refer-clojure :exclude [rand])
(:require
[clj-uuid :as uuid]
[clojure.core :exclude [rand]]
[schema.core :as s]
[tupelo.schema :as tsk]
[tupelo.string :as str]
[tupelo.core.impl :as impl])
(:import
[java.util UUID]))
(def const-null-str
"A null UUID string '00000000-0000-0000-0000-000000000000' "
"00000000-0000-0000-0000-000000000000")
(def const-null-obj
"A null UUID object '00000000-0000-0000-0000-000000000000' "
(UUID/fromString const-null-str))
(def const-dummy-str
"A dummy UUID string '00000000-0000-0000-0000-000000000000' "
"cafebabe-1953-0510-0970-0123456789ff")
(def const-dummy-obj
"A dummy UUID object '00000000-0000-0000-0000-000000000000' "
(UUID/fromString const-dummy-str))
(def null-str
"Returns a null UUID string '00000000-0000-0000-0000-000000000000' "
(const->fn const-null-str))
(def null
"Returns a null UUID object '00000000-0000-0000-0000-000000000000' "
(const->fn const-null-obj))
(def dummy-str
"Returns a dummy UUID string 'cafebabe-1953-0510-0970-0123456789ff' "
(const->fn const-dummy-str))
(def dummy
"Returns a dummy UUID object 'cafebabe-1953-0510-0970-0123456789ff'"
(const->fn const-dummy-obj))
(def ^:no-doc uuid-regex-pattern impl/uuid-regex-pattern)
(def uuid-str?
"Returns true iff the string shows a valid UUID-like pattern of hex digits. Does not
distinguish between UUID subtypes."
impl/uuid-str?)
;-----------------------------------------------------------------------------
(s/defn rand :- UUID
"Returns a random uuid as a UUID object"
[] (uuid/v4))
(s/defn rand-str :- s/Str
"Returns a random uuid as a String"
[] (str (tupelo.uuid/rand)))
;-----------------------------------------------------------------------------
(def ^:no-doc uuid-counter (atom nil)) ; uninitialized
(defn counted-reset! [] (reset! uuid-counter 0))
(counted-reset!) ; initialize upon load
(defn counted-str []
(let [cnt (swap-out! uuid-counter inc)
uuid-str (format "%08x-aaaa-bbbb-cccc-ddddeeeeffff" cnt)]
uuid-str))
(defn counted []
(UUID/fromString (counted-str)))
;-----------------------------------------------------------------------------
(defmacro with-null
"For testing, replace all calls to uuid/rand with uuid/null"
[& forms]
`(with-redefs [rand null]
~@forms))
(defmacro with-counted
"For testing, replace all calls to uuid/rand with uuid/counted"
[& forms]
`(with-redefs [rand counted]
(counted-reset!)
~@forms))