Skip to content

Commit c706351

Browse files
committed
EDN-serialize vars etc by modifying printing.
Use the letterpress library to provide tagged literal printed forms for vars, classes, and regular expressions. Use standard Clojure data readers to reconstitute these forms as the appropriate values. This removes the need for a custom EDN reader implementation.
1 parent addedf5 commit c706351

File tree

8 files changed

+96
-1011
lines changed

8 files changed

+96
-1011
lines changed

LICENSE

-233
Original file line numberDiff line numberDiff line change
@@ -200,236 +200,3 @@
200200
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201201
See the License for the specific language governing permissions and
202202
limitations under the License.
203-
204-
--------------------------------------------------------------------------------
205-
This program incorporates the Clojure EDN reader implementation, which is
206-
distributed under the Eclipse Public License v1.0:
207-
208-
Eclipse Public License - v 1.0
209-
210-
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE
211-
PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR
212-
DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS
213-
AGREEMENT.
214-
215-
1. DEFINITIONS
216-
217-
"Contribution" means:
218-
219-
a) in the case of the initial Contributor, the initial
220-
code and documentation distributed under this Agreement, and
221-
b) in the case of each subsequent Contributor:
222-
i) changes to the Program, and
223-
ii) additions to the Program;
224-
where such changes and/or additions to the Program
225-
originate from and are distributed by that particular Contributor. A
226-
Contribution 'originates' from a Contributor if it was added to the
227-
Program by such Contributor itself or anyone acting on such
228-
Contributor's behalf. Contributions do not include additions to the
229-
Program which: (i) are separate modules of software distributed in
230-
conjunction with the Program under their own license agreement, and (ii)
231-
are not derivative works of the Program.
232-
233-
"Contributor" means any person or entity that distributes
234-
the Program.
235-
236-
"Licensed Patents" mean patent claims licensable by a
237-
Contributor which are necessarily infringed by the use or sale of its
238-
Contribution alone or when combined with the Program.
239-
240-
"Program" means the Contributions distributed in accordance
241-
with this Agreement.
242-
243-
"Recipient" means anyone who receives the Program under
244-
this Agreement, including all Contributors.
245-
246-
2. GRANT OF RIGHTS
247-
248-
a) Subject to the terms of this Agreement, each
249-
Contributor hereby grants Recipient a non-exclusive, worldwide,
250-
royalty-free copyright license to reproduce, prepare derivative works
251-
of, publicly display, publicly perform, distribute and sublicense the
252-
Contribution of such Contributor, if any, and such derivative works, in
253-
source code and object code form.
254-
255-
b) Subject to the terms of this Agreement, each
256-
Contributor hereby grants Recipient a non-exclusive, worldwide,
257-
royalty-free patent license under Licensed Patents to make, use, sell,
258-
offer to sell, import and otherwise transfer the Contribution of such
259-
Contributor, if any, in source code and object code form. This patent
260-
license shall apply to the combination of the Contribution and the
261-
Program if, at the time the Contribution is added by the Contributor,
262-
such addition of the Contribution causes such combination to be covered
263-
by the Licensed Patents. The patent license shall not apply to any other
264-
combinations which include the Contribution. No hardware per se is
265-
licensed hereunder.
266-
267-
c) Recipient understands that although each Contributor
268-
grants the licenses to its Contributions set forth herein, no assurances
269-
are provided by any Contributor that the Program does not infringe the
270-
patent or other intellectual property rights of any other entity. Each
271-
Contributor disclaims any liability to Recipient for claims brought by
272-
any other entity based on infringement of intellectual property rights
273-
or otherwise. As a condition to exercising the rights and licenses
274-
granted hereunder, each Recipient hereby assumes sole responsibility to
275-
secure any other intellectual property rights needed, if any. For
276-
example, if a third party patent license is required to allow Recipient
277-
to distribute the Program, it is Recipient's responsibility to acquire
278-
that license before distributing the Program.
279-
280-
d) Each Contributor represents that to its knowledge it
281-
has sufficient copyright rights in its Contribution, if any, to grant
282-
the copyright license set forth in this Agreement.
283-
284-
3. REQUIREMENTS
285-
286-
A Contributor may choose to distribute the Program in object code
287-
form under its own license agreement, provided that:
288-
289-
a) it complies with the terms and conditions of this
290-
Agreement; and
291-
292-
b) its license agreement:
293-
294-
i) effectively disclaims on behalf of all Contributors
295-
all warranties and conditions, express and implied, including warranties
296-
or conditions of title and non-infringement, and implied warranties or
297-
conditions of merchantability and fitness for a particular purpose;
298-
299-
ii) effectively excludes on behalf of all Contributors
300-
all liability for damages, including direct, indirect, special,
301-
incidental and consequential damages, such as lost profits;
302-
303-
iii) states that any provisions which differ from this
304-
Agreement are offered by that Contributor alone and not by any other
305-
party; and
306-
307-
iv) states that source code for the Program is available
308-
from such Contributor, and informs licensees how to obtain it in a
309-
reasonable manner on or through a medium customarily used for software
310-
exchange.
311-
312-
When the Program is made available in source code form:
313-
314-
a) it must be made available under this Agreement; and
315-
316-
b) a copy of this Agreement must be included with each
317-
copy of the Program.
318-
319-
Contributors may not remove or alter any copyright notices contained
320-
within the Program.
321-
322-
Each Contributor must identify itself as the originator of its
323-
Contribution, if any, in a manner that reasonably allows subsequent
324-
Recipients to identify the originator of the Contribution.
325-
326-
4. COMMERCIAL DISTRIBUTION
327-
328-
Commercial distributors of software may accept certain
329-
responsibilities with respect to end users, business partners and the
330-
like. While this license is intended to facilitate the commercial use of
331-
the Program, the Contributor who includes the Program in a commercial
332-
product offering should do so in a manner which does not create
333-
potential liability for other Contributors. Therefore, if a Contributor
334-
includes the Program in a commercial product offering, such Contributor
335-
("Commercial Contributor") hereby agrees to defend and
336-
indemnify every other Contributor ("Indemnified Contributor")
337-
against any losses, damages and costs (collectively "Losses")
338-
arising from claims, lawsuits and other legal actions brought by a third
339-
party against the Indemnified Contributor to the extent caused by the
340-
acts or omissions of such Commercial Contributor in connection with its
341-
distribution of the Program in a commercial product offering. The
342-
obligations in this section do not apply to any claims or Losses
343-
relating to any actual or alleged intellectual property infringement. In
344-
order to qualify, an Indemnified Contributor must: a) promptly notify
345-
the Commercial Contributor in writing of such claim, and b) allow the
346-
Commercial Contributor to control, and cooperate with the Commercial
347-
Contributor in, the defense and any related settlement negotiations. The
348-
Indemnified Contributor may participate in any such claim at its own
349-
expense.
350-
351-
For example, a Contributor might include the Program in a commercial
352-
product offering, Product X. That Contributor is then a Commercial
353-
Contributor. If that Commercial Contributor then makes performance
354-
claims, or offers warranties related to Product X, those performance
355-
claims and warranties are such Commercial Contributor's responsibility
356-
alone. Under this section, the Commercial Contributor would have to
357-
defend claims against the other Contributors related to those
358-
performance claims and warranties, and if a court requires any other
359-
Contributor to pay any damages as a result, the Commercial Contributor
360-
must pay those damages.
361-
362-
5. NO WARRANTY
363-
364-
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS
365-
PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
366-
OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION,
367-
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY
368-
OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
369-
responsible for determining the appropriateness of using and
370-
distributing the Program and assumes all risks associated with its
371-
exercise of rights under this Agreement , including but not limited to
372-
the risks and costs of program errors, compliance with applicable laws,
373-
damage to or loss of data, programs or equipment, and unavailability or
374-
interruption of operations.
375-
376-
6. DISCLAIMER OF LIABILITY
377-
378-
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT
379-
NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT,
380-
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING
381-
WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
382-
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
383-
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR
384-
DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
385-
HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
386-
387-
7. GENERAL
388-
389-
If any provision of this Agreement is invalid or unenforceable under
390-
applicable law, it shall not affect the validity or enforceability of
391-
the remainder of the terms of this Agreement, and without further action
392-
by the parties hereto, such provision shall be reformed to the minimum
393-
extent necessary to make such provision valid and enforceable.
394-
395-
If Recipient institutes patent litigation against any entity
396-
(including a cross-claim or counterclaim in a lawsuit) alleging that the
397-
Program itself (excluding combinations of the Program with other
398-
software or hardware) infringes such Recipient's patent(s), then such
399-
Recipient's rights granted under Section 2(b) shall terminate as of the
400-
date such litigation is filed.
401-
402-
All Recipient's rights under this Agreement shall terminate if it
403-
fails to comply with any of the material terms or conditions of this
404-
Agreement and does not cure such failure in a reasonable period of time
405-
after becoming aware of such noncompliance. If all Recipient's rights
406-
under this Agreement terminate, Recipient agrees to cease use and
407-
distribution of the Program as soon as reasonably practicable. However,
408-
Recipient's obligations under this Agreement and any licenses granted by
409-
Recipient relating to the Program shall continue and survive.
410-
411-
Everyone is permitted to copy and distribute copies of this
412-
Agreement, but in order to avoid inconsistency the Agreement is
413-
copyrighted and may only be modified in the following manner. The
414-
Agreement Steward reserves the right to publish new versions (including
415-
revisions) of this Agreement from time to time. No one other than the
416-
Agreement Steward has the right to modify this Agreement. The Eclipse
417-
Foundation is the initial Agreement Steward. The Eclipse Foundation may
418-
assign the responsibility to serve as the Agreement Steward to a
419-
suitable separate entity. Each new version of the Agreement will be
420-
given a distinguishing version number. The Program (including
421-
Contributions) may always be distributed subject to the version of the
422-
Agreement under which it was received. In addition, after a new version
423-
of the Agreement is published, Contributor may elect to distribute the
424-
Program (including its Contributions) under the new version. Except as
425-
expressly stated in Sections 2(a) and 2(b) above, Recipient receives no
426-
rights or licenses to the intellectual property of any Contributor under
427-
this Agreement, whether expressly, by implication, estoppel or
428-
otherwise. All rights in the Program not expressly granted under this
429-
Agreement are reserved.
430-
431-
This Agreement is governed by the laws of the State of New York and
432-
the intellectual property laws of the United States of America. No party
433-
to this Agreement will bring a legal action under this Agreement more
434-
than one year after the cause of action arose. Each party waives its
435-
rights to a jury trial in any resulting litigation.

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ Copyright © 2013-2014 Marshall Bockrath-Vandegrift & Damballa, Inc.
8787

8888
Distributed under the Apache License, Version 2.0.
8989

90-
The EDN reader implementation (`EdnReader.java`) is copyright © 2013 Rich
91-
Hickey, and is distributed under the Eclipse Public License v1.0.
92-
9390
[intro]: https://github.com/damballa/parkour/blob/master/doc/intro.md
9491
[motivation]: https://github.com/damballa/parkour/blob/master/doc/motivation.md
9592
[namespaces]: https://github.com/damballa/parkour/blob/master/doc/namespaces.md

project.clj

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
:dependencies [[org.clojure/tools.logging "0.3.1"]
1818
[com.damballa/abracad "0.4.11"]
1919
[org.apache.avro/avro "1.7.7"]
20+
[org.platypope/letterpress "0.1.0"]
2021
[pjstadig/scopes "0.3.0"]
2122
[transduce/transduce "0.1.1"]]
2223
:plugins [[codox/codox "0.6.6"]]

src/clojure/parkour/cser.clj

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
(ns parkour.cser
22
(:refer-clojure :exclude [assoc! get read-string pr-str])
33
(:require [clojure.core :as cc]
4-
[parkour.conf :as conf])
4+
[clojure.edn :as edn]
5+
[letterpress.core :as lp]
6+
[parkour.conf :as conf]
7+
[parkour.cser.readers :refer [data-readers]]
8+
[parkour.cser.printers :refer [data-printers]])
59
(:import [java.io Writer]
610
[clojure.lang RT]
7-
[org.apache.hadoop.conf Configuration]
8-
[parkour.edn EdnReader]))
11+
[org.apache.hadoop.conf Configuration]))
912

1013
(def ^:internal ^:dynamic *conf*
1114
"Configuration of job being executed/configured during job configuration
@@ -18,23 +21,34 @@ de/serialization."
1821

1922
(defn ^:private read-string*
2023
"Like core `edn/read-string`, but using cser/EDN reader implementation."
21-
([s] (read-string* {:eof nil, :readers *data-readers*} s))
22-
([opts s] (when s (EdnReader/readString s opts))))
24+
([s] (read-string* {} s))
25+
([opts s]
26+
(let [readers (merge *data-readers* data-readers (:readers opts))
27+
opts (assoc opts :readers readers)]
28+
(edn/read-string opts s))))
2329

2430
(defn read-string
2531
"Like core `edn/read-string`, but using cser/EDN reader implementation in the
2632
cser context of `conf`."
2733
([conf s] (with-conf conf (read-string* s)))
2834
([conf opts s] (with-conf conf (read-string* opts s))))
2935

36+
(defn ^:private pr-str*
37+
"Like core `pr-str`, but with cser-specific printers."
38+
([] "")
39+
([x] (lp/pr-str {:printers data-printers} x))
40+
([x & xs] (apply lp/pr-str {:printers data-printers} x xs)))
41+
3042
(defn pr-str
3143
"Like core `pr-str`, but in the cser context of `conf`."
32-
[conf & xs] (with-conf conf (apply cc/pr-str xs)))
44+
([conf] "")
45+
([conf x] (with-conf conf (pr-str* x)))
46+
([conf x & xs] (with-conf conf (apply pr-str* x xs))))
3347

3448
(defn ^:private assoc!*
3549
"Internal implementation for `assoc!`."
3650
([conf key val]
37-
(conf/assoc! conf key (cc/pr-str val)))
51+
(conf/assoc! conf key (pr-str* val)))
3852
([conf key val & kvs]
3953
(let [conf (assoc!* conf key val)]
4054
(if (empty? kvs)

src/clojure/parkour/cser/printers.clj

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(ns parkour.cser.printers
2+
(:import [java.io Writer]
3+
[java.util.regex Pattern]
4+
[clojure.lang Var]))
5+
6+
(defn class-print
7+
"Tagged literal printer for Java classes."
8+
[^Class x ^Writer w]
9+
(doto w
10+
(.append "#parkour/class ")
11+
(.append (.getName x))))
12+
13+
(defn var-print
14+
"Tagged literal printer for Clojure vars."
15+
[^Var x ^Writer w]
16+
(doto w
17+
(.append "#parkour/var ")
18+
(.append (str (.-ns x)))
19+
(.append \/)
20+
(.append (str (.-sym x)))))
21+
22+
(defn pattern-print
23+
"Tagged literal printer for Java compiled regular expressions."
24+
[^Pattern x ^Writer w]
25+
(doto w
26+
(.append "#parkour/pattern \"")
27+
(.append (str x))
28+
(.append \")))
29+
30+
(def data-printers
31+
"Custom data printers for cser printing."
32+
{Class class-print,
33+
Var var-print,
34+
Pattern pattern-print,
35+
})

src/clojure/parkour/cser/readers.clj

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(ns parkour.cser.readers
2+
(:import [java.util.regex Pattern]
3+
[clojure.lang RT Var]))
4+
5+
(defn class-read
6+
"Tagged literal reader for Java classes."
7+
[x] (Class/forName (name x) false (RT/baseLoader)))
8+
9+
(defn var-read
10+
"Tagged literal reader for Clojure vars."
11+
[x]
12+
(let [ns (symbol (namespace x))
13+
n (symbol (name x))]
14+
(require ns)
15+
(intern ns n)))
16+
17+
(defn pattern-read
18+
"Tagged literal reader for Java compiled regular expressions."
19+
[x] (Pattern/compile x))
20+
21+
(def data-readers
22+
"Custom data readers for cser reading."
23+
{'parkour/class class-read,
24+
'parkour/var var-read,
25+
'parkour/pattern pattern-read,
26+
})

0 commit comments

Comments
 (0)