forked from damballa/abracad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edn.clj
240 lines (211 loc) · 7.15 KB
/
edn.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
(ns abracad.avro.edn
(:require [abracad.avro :as avro]
[abracad.avro.util :refer [coerce]])
(:import [clojure.lang BigInt Cons IMeta IPersistentList IPersistentMap
IPersistentSet IPersistentVector ISeq Keyword PersistentArrayMap
PersistentQueue Ratio Sorted Symbol]
[org.apache.avro Schema]))
(defprotocol EDNAvroSerializable
"Protocol for customizing EDN-in-Avro serialization."
(-schema-name [this]
"Full package-/namespace-qualified name for EDN-in-Avro purposes.")
(field-get [this field]
"Value of keyword `field` for EDN-in-Avro serialization of object.")
(field-list [this]
"List of keyword EDN-in-Avro fields this object provides."))
(defn schema-name
"EDN-in-Avro schema name of `obj`."
[obj]
(if (meta obj)
"abracad.avro.edn.Meta"
(-schema-name obj)))
(extend-protocol EDNAvroSerializable
nil (-schema-name [_] "null")
Character
(-schema-name [_] "abracad.avro.edn.Character")
(field-get [this _] (int this))
(field-list [_] #{:value})
Symbol
(-schema-name [_] "abracad.avro.edn.Symbol")
(field-get [this field]
(case field
:namespace (namespace this)
:name (name this)))
(field-list [_] #{:namespace :name})
Keyword
(-schema-name [_] "abracad.avro.edn.Keyword")
(field-get [this field]
(case field
:namespace (namespace this)
:name (name this)))
(field-list [_] #{:namespace :name})
Byte
(-schema-name [_] "abracad.avro.edn.Byte")
(field-get [this _] (int this))
(field-list [_] #{:value})
Short
(-schema-name [_] "abracad.avro.edn.Short")
(field-get [this _] (int this))
(field-list [_] #{:value})
BigInt
(-schema-name [_] "abracad.avro.edn.BigInt")
(field-get [this _] (.toByteArray (.toBigInteger this)))
(field-list [_] #{:value})
BigInteger
(-schema-name [_] "abracad.avro.edn.BigInteger")
(field-get [this _] (.toByteArray this))
(field-list [_] #{:value})
BigDecimal
(-schema-name [_] "abracad.avro.edn.BigDecimal")
(field-get [this field]
(case field
:value (.unscaledValue this)
:scale (.scale this)))
(field-list [this] #{:value :scale})
IPersistentList
(-schema-name [_] "abracad.avro.edn.List")
(field-get [this _] this)
(field-list [this] #{:values})
ISeq
(-schema-name [_] "abracad.avro.edn.List")
(field-get [this _] this)
(field-list [this] #{:values})
IPersistentVector
(-schema-name [_] "abracad.avro.edn.Vector")
(field-get [this _] this)
(field-list [this] #{:values})
IPersistentMap
(-schema-name [this]
(if (instance? Sorted this)
"abracad.avro.edn.SortedMap"
"abracad.avro.edn.Map"))
(field-get [this _] (apply concat this))
(field-list [_] #{:keyvals})
IPersistentSet
(-schema-name [this]
(if (instance? Sorted this)
"abracad.avro.edn.SortedSet"
"abracad.avro.edn.Set"))
(field-get [this _] this)
(field-list [_] #{:values})
PersistentQueue
(-schema-name [_] "abracad.avro.edn.Queue")
(field-get [this _] this)
(field-list [this] #{:values})
Ratio
(-schema-name [_] "abracad.avro.edn.Ratio")
(field-get [this field]
(case field
:numerator (.-numerator this)
:denominator (.-denominator this)))
(field-list [this] #{:numerator :denominator})
Object
(-schema-name [this] (avro/schema-name this))
(field-get [this field] (avro/field-get this field))
(field-list [this] (avro/field-list this)))
(defn ^:private edn-list*
"List-equivalent of `args`."
[args] (if (empty? args) () (seq args)))
(defn ^:private hash-map*
"Unsorted map from interleaved key-values in `args`."
[args] (apply hash-map args))
(defn ^:private sorted-map*
"Sorted map from interleaved key-values in `args`."
[args] (apply sorted-map args))
(defn ^:private hash-set*
"Unsorted set from values in `args`."
[args] (apply hash-set args))
(defn ^:private sorted-set*
"Sorted set from values in `args`."
[args] (apply sorted-set args))
(defn ^:private bigdecimal
"BigDecimal constructor function matching EDN-in-Avro serialization."
[^BigInteger value ^long scale] (BigDecimal. value (int scale)))
(defn ^:private queue*
"Queue from values in `args`."
[args] (into PersistentQueue/EMPTY args))
(defn ^:private ratio
"Ratio constructor function matching EDN-in-Avro serialization."
[n d] (Ratio. n d))
(def ^:private base-elements
"Schemas for base EDN element types."
[;; Core EDN element types, preserving extra run-time type
;; information where potentially desirable
"null"
"boolean"
"string"
{:type "record", :name "Character"
:fields [{:name "value", :type "int"}]}
{:type "record", :name "Symbol"
:fields [{:name "namespace", :type ["null", "string"]}
{:name "name", :type "string"}]}
{:type "record", :name "Keyword"
:fields [{:name "namespace", :type ["null", "string"]}
{:name "name", :type "string"}]}
{:type "record", :name "Byte"
:fields [{:name "value", :type "int"}]}
"int"
{:type "record", :name "Short"
:fields [{:name "value", :type "int"}]}
"long"
{:type "record", :name "BigInt"
:fields [{:name "value", :type "bytes"}]}
{:type "record", :name "BigInteger"
:fields [{:name "value", :type "bytes"}]}
"float"
"double"
{:type "record", :name "BigDecimal"
:fields [{:name "value", :type "BigInteger"}
{:name "scale", :type "int"}]}
{:type "record", :name "List"
:fields [{:name "values",
:type {:type "array",
:items "Element"}}]}
{:type "record", :name "Vector"
:fields [{:name "values",
:type {:type "array",
:items "Element"}}]}
{:type "record", :name "Map"
:fields [{:name "keyvals",
:type {:type "array",
:items "Element"}}]}
{:type "record", :name "SortedMap"
:fields [{:name "keyvals",
:type {:type "array",
:items "Element"}}]}
{:type "record", :name "Set"
:fields [{:name "values",
:type {:type "array",
:items "Element"}}]}
{:type "record", :name "SortedSet"
:fields [{:name "values",
:type {:type "array",
:items "Element"}}]}
;;
;; Built-in tagged literals
;; [TODO]
;;
;; Extensions
"bytes"
{:type "record", :name "Queue"
:fields [{:name "values",
:type {:type "array",
:items "Element"}}]}
{:type "record", :name "Ratio"
:fields [{:name "numerator", :type "BigInteger"}
{:name "denominator", :type "BigInteger"}]}
])
(defn new-schema
"Return new EDN-in-Avro schema. If provided, incorporate
`schemas` (which should be compatible with `avro/parse-schema`) as
additional allowed element types."
[& schemas]
(let [schemas (mapcat (partial coerce IPersistentVector vector) schemas)]
(avro/parse-schema
`{:type "record", :name "abracad.avro.edn.Element",
:fields [{:name "value"
:type [{:type "record", :name "Meta"
:fields [{:name "value", :type "Element"}
{:name "meta", :type "Element"}]}
~@schemas
~@base-elements]}]})))