edn-format / edn

Extensible Data Notation
2.62k stars 96 forks source link

ns alias #82

Closed jerger closed 4 years ago

jerger commented 4 years ago

Hi, as we're using spec with namespaced keywords, our edn's get bloated with the namespace part like:

{:dda.provision/provisioner :dda.provision.dry-run/dry-run
 :dda.provision/user "user"
 :dda.provision/module "dda-provision"
 :dda.provision/sub-module "should-execute"
 :dda.provision/filename "aFile"}

namespace alias would be great in order to raise the semantic per boilerplate ratio. Comparable problem is solved e.g. in RDF (full example at https://www.w3schools.com/XML/xml_rdf.asp):

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  <rdf:Description rdf:about="https://www.w3schools.com">
</rdf:RDF>

Is there a comparable solution in edn ?

claj commented 4 years ago

It is possible to write such a map as

#:dda.provision{:provisioner :dda.provision.dry-run/dry-run
                :user "user"
                :module "dda-provision"
                :sub-module "should-execute"
                :filename "aFile"}

These namespaced maps where introduced in Clojure 1.9 (https://clojure.org/reference/reader#_maps). They are not mentioned in EDN format specification (awaiting BNF). Namespaced maps works when reading EDN with clojure.edn in versions >= 1.9:

(clojure.edn/read-string "#:a{:b 1 :c 1}")
#:a{:b 1, :c 1}

If you specify other qualified keys inside the map the namespace in front of the map vanishes:

(clojure.edn/read-string "#:a{:b 1 :c 1 :other/test 2}")
{:a/b 1, :a/c 1, :other/test 2}

As you can see above the default namespace a is overridden by giving a qualified keyword inside the map (like :other/test).

I do not know if this functionality is supported in other EDN-readers.

claj commented 4 years ago

also see #78