Skip to content

Commit addedf5

Browse files
committed
Extend and test configuration-aware extended-EDN serialization.
1 parent 5957062 commit addedf5

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

src/clojure/parkour/cser.clj

+30-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
(ns parkour.cser
2-
(:refer-clojure :exclude [assoc! get])
3-
(:require [parkour.conf :as conf])
4-
(:import [org.apache.hadoop.conf Configuration]
2+
(:refer-clojure :exclude [assoc! get read-string pr-str])
3+
(:require [clojure.core :as cc]
4+
[parkour.conf :as conf])
5+
(:import [java.io Writer]
6+
[clojure.lang RT]
7+
[org.apache.hadoop.conf Configuration]
58
[parkour.edn EdnReader]))
69

710
(def ^:internal ^:dynamic *conf*
811
"Configuration of job being executed/configured during job configuration
912
de/serialization."
1013
nil)
1114

15+
(defmacro ^:private with-conf
16+
"Enter dynamic cser context of `conf` for `body` forms."
17+
[conf & body] `(binding [*conf* (conf/ig ~conf)] ~@body))
18+
19+
(defn ^:private read-string*
20+
"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))))
23+
24+
(defn read-string
25+
"Like core `edn/read-string`, but using cser/EDN reader implementation in the
26+
cser context of `conf`."
27+
([conf s] (with-conf conf (read-string* s)))
28+
([conf opts s] (with-conf conf (read-string* opts s))))
29+
30+
(defn pr-str
31+
"Like core `pr-str`, but in the cser context of `conf`."
32+
[conf & xs] (with-conf conf (apply cc/pr-str xs)))
33+
1234
(defn ^:private assoc!*
1335
"Internal implementation for `assoc!`."
1436
([conf key val]
15-
(conf/assoc! conf key (pr-str val)))
37+
(conf/assoc! conf key (cc/pr-str val)))
1638
([conf key val & kvs]
1739
(let [conf (assoc!* conf key val)]
1840
(if (empty? kvs)
@@ -23,24 +45,15 @@ de/serialization."
2345
"Set `key` in `conf` to cser/EDN representation of `val`."
2446
{:tag `Configuration}
2547
([conf] conf)
26-
([conf key val]
27-
(binding [*conf* conf]
28-
(assoc!* conf key val)))
29-
([conf key val & kvs]
30-
(binding [*conf* conf]
31-
(apply assoc!* conf key val kvs))))
32-
33-
(defn ^:private edn-read-string
34-
"Like core `edn/read-string`, but using cser/EDN reader implementation."
35-
([s] (edn-read-string {:eof nil, :readers *data-readers*} s))
36-
([opts s] (when s (EdnReader/readString s opts))))
48+
([conf key val] (with-conf conf (assoc!* conf key val)))
49+
([conf key val & kvs] (with-conf conf (apply assoc!* conf key val kvs))))
3750

3851
(defn get
3952
"Clojure data value for `key` in `conf`."
4053
([conf key] (get conf key nil))
4154
([conf key default]
42-
(binding [*conf* conf]
55+
(with-conf conf
4356
(let [val (conf/get conf key nil)]
4457
(if (nil? val)
4558
default
46-
(edn-read-string val))))))
59+
(read-string* val))))))

test/parkour/cser_test.clj

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(ns parkour.cser-test
2+
(:require [clojure.test :refer :all]
3+
[parkour (conf :as conf) (cser :as cser)]
4+
[parkour.test-helpers :as th]))
5+
6+
(def conf-key
7+
"parkour.cser-test")
8+
9+
(defn roundtrips-conf?
10+
[x]
11+
(let [conf (conf/ig)]
12+
(cser/assoc! conf conf-key x)
13+
(= x (cser/get conf conf-key))))
14+
15+
(defn roundtrips-raw?
16+
[x]
17+
(let [conf (conf/ig)]
18+
(= x (cser/read-string conf (cser/pr-str conf x)))))
19+
20+
(deftest test-cser
21+
(th/with-config
22+
(let [conf (conf/ig)]
23+
(are [x] (and (roundtrips-conf? x)
24+
(roundtrips-raw? x))
25+
(range 10)
26+
[:foo 'bar "baz"]
27+
{:key #'val})
28+
(are [x] (and (not (roundtrips-conf? x))
29+
(not (roundtrips-raw? x)))
30+
;; Any other failing cases?
31+
java.lang.String))))

0 commit comments

Comments
 (0)