Skip to content

Commit 515db0a

Browse files
timothypratleyswannodette
authored andcommitted
CLJS-3036: Provide a clojure.edn namespace for Clojure compatibility
Adds the clojure.edn namespace, containing read and read-string. When writing CLJC libraries intended for use in Clojure and ClojureScript it is convenient to be able to use clojure.edn/read-string instead of using reader conditionals to use cljs.reader/read-string in ClojureScript.
1 parent 90c9a68 commit 515db0a

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

src/main/cljs/clojure/edn.cljs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
;; Copyright (c) Rich Hickey. All rights reserved.
2+
;; The use and distribution terms for this software are covered by the
3+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
;; which can be found in the file epl-v10.html at the root of this distribution.
5+
;; By using this software in any fashion, you are agreeing to be bound by
6+
;; the terms of this license.
7+
;; You must not remove this notice, or any other, from this software.
8+
9+
(ns clojure.edn
10+
"edn reading.
11+
12+
This namespace provides alias for cljs.reader/read and cljs.reader/read-string.
13+
Thus Clojure and ClojureScript source can reference these functions in the same way.
14+
In Clojure, read and read-string may cause evaluation,
15+
but clojure.edn/read and clojure.edn/read-string will not.
16+
In ClojureScript cljs.reader/read and cljs.reader/read-string will not cause evaluation,
17+
they only read edn."
18+
(:require [cljs.reader :as reader]))
19+
20+
(defn read
21+
"Reads the first object from an cljs.tools.reader.reader-types/IPushbackReader.
22+
Returns the object read. If EOF, throws if eof-error? is true otherwise returns eof.
23+
If no reader is provided, *in* will be used.
24+
25+
Reads data in the edn format (subset of Clojure data):
26+
http://edn-format.org
27+
28+
cljs.tools.reader.edn/read doesn't depend on dynamic Vars, all configuration
29+
is done by passing an opt map.
30+
31+
opts is a map that can include the following keys:
32+
:eof - value to return on end-of-file. When not supplied, eof throws an exception.
33+
:readers - a map of tag symbols to data-reader functions to be considered before default-data-readers.
34+
When not supplied, only the default-data-readers will be used.
35+
:default - A function of two args, that will, if present and no reader is found for a tag,
36+
be called with the tag and the value."
37+
([reader]
38+
(reader/read reader))
39+
([opts reader]
40+
(reader/read opts reader))
41+
([reader eof-error? eof opts]
42+
(reader/read reader eof-error? eof opts)))
43+
44+
(defn read-string
45+
"Reads one object from the string s.
46+
Returns nil when s is nil or empty.
47+
48+
Reads data in the edn format (subset of Clojure data):
49+
http://edn-format.org
50+
51+
opts is a map as per cljs.tools.reader.edn/read"
52+
([s]
53+
(reader/read-string s))
54+
([opts s]
55+
(reader/read-string opts s)))

src/test/cljs/clojure/edn_test.cljs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns clojure.edn-test
2+
(:require [cljs.test :refer-macros [deftest is testing]]
3+
[clojure.edn :as edn]
4+
[cljs.reader :as reader]
5+
[cljs.tools.reader.reader-types :as reader-types]))
6+
7+
(defn- test-reader []
8+
(reader-types/string-push-back-reader "[1 2 3]"))
9+
10+
(deftest test-read
11+
(testing "Mirrors cljs.reader/read"
12+
(is (= (edn/read (test-reader))
13+
(reader/read (test-reader))))
14+
(is (= (edn/read {} (test-reader))
15+
(reader/read {} (test-reader))))
16+
(is (= (edn/read (test-reader) false "EOF" {})
17+
(reader/read (test-reader) false "EOF" {})))))
18+
19+
(deftest test-read-string
20+
(testing "Mirrors cljs.reader/read-string"
21+
(is (= (edn/read-string "(+ 1 2)")
22+
(reader/read-string "(+ 1 2)")))
23+
(is (= (edn/read-string "{:a #{[1]}}")
24+
(reader/read-string "{:a #{[1]}}")))))

src/test/cljs/test_runner.cljs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
[clojure.string-test]
2424
[clojure.data-test]
2525
[clojure.datafy-test]
26+
[clojure.edn-test]
2627
[clojure.walk-test]
2728
[cljs.macro-test]
2829
[cljs.letfn-test]
@@ -69,6 +70,7 @@
6970
'clojure.string-test
7071
'clojure.data-test
7172
'clojure.datafy-test
73+
'clojure.edn-test
7274
'clojure.walk-test
7375
'cljs.letfn-test
7476
'cljs.reducers-test

src/test/self/self_parity/test.cljs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
#_[cljs.ns-test]
289289
[clojure.string-test]
290290
[clojure.data-test]
291+
[clojure.edn]
291292
[clojure.walk-test]
292293
[cljs.macro-test]
293294
[cljs.letfn-test]
@@ -331,6 +332,7 @@
331332
'cljs.reader-test
332333
'clojure.string-test
333334
'clojure.data-test
335+
'clojure.edn
334336
'clojure.walk-test
335337
'cljs.letfn-test
336338
'cljs.reducers-test

0 commit comments

Comments
 (0)