-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.clj
88 lines (69 loc) · 2.69 KB
/
api.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
(ns org.purefn.lebowski.encoder.api
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]
[com.gfredericks.test.chuck.generators :refer [string-from-regex]]
[org.purefn.bridges.api :as api]
[org.purefn.lebowski.encoder.protocol :as proto])
(:import [com.couchbase.client.java.document AbstractDocument]))
;;------------------------------------------------------------------------------
;; API
;;------------------------------------------------------------------------------
(defn named
"Create an empty document with the given ID."
[encoder id]
(proto/named encoder id))
(defn versioned
"Create an empty document with the given ID and check-and-set token."
[encoder id cas]
(proto/versioned encoder id cas))
(defn encode
"Encode the value as a document with the given ID."
[encoder id value]
(proto/encode encoder id value))
(defn encode-versioned
"Encode the value as a document with the given ID and check-and-set token."
[encoder id value cas]
(proto/encode-versioned encoder id value cas))
(defn decode
"Decode the document into a value and check-and-set token."
[encoder document]
(proto/decode encoder document))
;;------------------------------------------------------------------------------
;; Data Specs
;;------------------------------------------------------------------------------
(def encoder? (partial satisfies? proto/CheckAndSetEncoder))
(def id-regex #"^[a-z]+(-[a-z]+)*(/[a-z]+(-[a-z]+)*)*\p{Graph}+$")
(def id?
(s/spec (s/and string?
(partial re-matches id-regex)
#(<= (count %) 250))
:gen #(string-from-regex
#"[a-z]+(-[a-z]+){0,2}(/[a-z]+(-[a-z]+){0,2}){0,2}[a-zA-Z0-9#$%&\*\+-\.:;<=>\?@_]+")))
(def document? (partial instance? AbstractDocument))
;;------------------------------------------------------------------------------
;; Function Specs
;;------------------------------------------------------------------------------
(s/fdef named
:args (s/cat :encoder encoder?
:id id?)
:ret document?)
(s/fdef versioned
:args (s/cat :encoder encoder?
:id id?
:cas api/cas?)
:ret document?)
(s/fdef encode
:args (s/cat :encoder encoder?
:id id?
:value some?)
:ret document?)
(s/fdef encode-versioned
:args (s/cat :encoder encoder?
:id id?
:value some?
:cas api/cas?)
:ret document?)
(s/fdef decode
:arge (s/cat :encoder encoder?
:document document?)
:ret api/doc?)