This repository has been archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
grimoire.clj
167 lines (128 loc) · 4.79 KB
/
grimoire.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
157
158
159
160
161
162
163
164
165
166
167
(ns grimoire
"A demo of the shelving API, and applying it to a Grimoire v2-like store."
(:require [clojure.spec.alpha :as s]
[shelving.core :as shelving]))
;; Packages, containers of entities we wish to document.
;;------------------------------------------------------------------------------------------
(defmulti package->spec
:type)
(s/def ::package
(s/multi-spec package->spec :package))
(s/def :org.maven/type #{:org.maven/package})
(s/def :org.maven/group string?)
(s/def :org.maven/artifact string?)
(s/def :org.maven/version string?)
(s/def :org.maven/package
(s/keys :req-un [:org.maven/type
:org.maven/group
:org.maven/artifact
:org.maven/version]))
(defn ->mvn-pkg [group artifact version]
{:type :org.maven/package
:group group
:artifact artifact
:version version})
(defmethod package->spec :org.maven/package [_]
:org.maven/package)
;; Entities, things we want to store information about.
;;--------------------------------------------------------------------------------------------------
(defmulti entity->spec
:type)
(defmulti entity->package
:type)
(s/def ::entity
(s/multi-spec entity->spec :entity))
(s/def :org.clojure.namespace/name
string?)
(s/def :org.clojure.namespace/platform
#{"clj" "cljc" "cljr" "cljs"})
(s/def :org.clojure.namespace/type
#{:org.clojure/namespace})
(s/def :org.clojure/namespace
(s/keys :req-un [:org.clojure.namespace/type
:org.clojure.namespace/name
::package
:org.clojure.namespace/platform]))
(defn ->namespace [package platform name]
{:type :org.clojure/namespace
:package package
:platform platform
:name name})
(defmethod entity->spec :org.clojure/namespace [_]
:org.clojure/namespace)
(defmethod entity->package :org.clojure/namespace [{:keys [package]}]
package)
(s/def :org.clojure.def/type
#{:org.clojure/def})
(s/def :org.clojure.def/name
string?)
(s/def :org.clojure/def
(s/keys :req-un [:org.clojure.def/type
:org.clojure/namespace
:org.clojure.def/name]))
(defmethod entity->spec :org.clojure/def [_]
:org.clojure/def)
(defmethod entity->package :org.clojure/def [{:keys [namespace]}]
(entity->package namespace))
;; Annotations on entities.
;;--------------------------------------------------------------------------------------------------
(defmulti annotation->spec :type)
(defmulti annotation->entity :type)
(defmulti annotation->package :type)
(s/def ::annotation
(s/multi-spec annotation->spec :annotation))
;; Metadata
(s/def :org.clojure-grimoire.example/metadata
map?)
;; Examples
(s/def :org.clojure-grimoire.example/type
#{:org.clojure-grimoire/example})
(s/def :org.clojure-grimoire.example/name
string?)
(s/def :org.clojure-grimoire.example/text
string?)
(s/def :org.clojure-grimoire/example
(s/and (s/keys :opt-un [:org.clojure-grimoire.example/name
:org.clojure-grimoire.example/metadata]
:req-un [:org.clojure-grimoire.example/type
:org.clojure-grimoire.example/text])
(s/or :entity (s/keys :req-un [::entity])
:package (s/keys :req-un [::package]))))
(defmethod annotation->spec :org.clojure-grimoire/example [_]
:org.clojure-grimoire/example)
(defmethod annotation->entity :org.clojure-grimoire/example [{:keys [entity]}]
entity)
(defmethod annotation->package :org.clojure-grimoire/example [{:keys [package entity]}]
(or package
(entity->package entity)))
;; Articles
(s/def :org.clojure-grimoire.article/type
#{:org.clojure-grimoire/article})
(s/def :org.clojure-grimoire.article/name
string?)
(s/def :org.clojure-grimoire.article/text
string?)
(s/def :org.clojure-grimoire.article/metadata
map?)
(s/def :org.clojure-grimoire/article
(s/and (s/keys :req-un [:org.clojure-grimoire.article/type
:org.clojure-grimoire.article/metadata
:org.clojure-grimoire.article/title
:org.clojure-grimoire.article/text]
:opt-un [:org.clojure-grimoire.article/metadata])
(s/or :entity (s/keys :req-un [::entity])
:package (s/keys :req-un [::package]))))
(defmethod annotation->spec :org.clojure-grimoire/article [_]
:org.clojure-grimoire/article)
(defmethod annotation->entity :org.clojure-grimoire/article [{:keys [entity]}]
entity)
(defmethod annotation->package :org.clojure-grimoire/article [{:keys [package entity]}]
(or package
(entity->package entity)))
(def schema
(-> shelving/empty-schema
(shelving/value-spec ::package)
(shelving/value-spec ::entity)
(shelving/record-spec ::annotation)
(shelving/automatic-rels true)
(shelving/automatic-specs true)))