Andrea Amantini / Apr 01 2019

Clojure Datafy

(ns datafiches
  (:require [clojure.datafy :as d]
            [clojure.core.protocols :as p]))

(defrecord SomeData [name children]
  p/Datafiable
  (datafy [r] {:class "SomeData" :name name} ))
datafiches.SomeData
(SomeData. "hello" [1 2 3])
Map {name: "hello", children: Vector(3)}
(d/datafy (SomeData. "hello" [1 2 3]))
Map {class: "SomeData", name: "hello"}
(extend-type SomeData
  p/Navigable
  (nav [x k v] 
    (if (= k :children)
      (concat (k x) v)
      v)))
(d/nav (SomeData. "Hi" [1 2 3]) :children [4 5])
List(5) (1, 2, 3, 4, 5)

Clojure implements datafy for a few protocols

(d/datafy (ex-info "Crash!" {:foo :bar}))
(d/nav (ex-info "Crash" {:foo :bar}) :cause "foo")
"foo"