Skip to content

Instantly share code, notes, and snippets.

@didibus
Last active January 5, 2023 05:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didibus/2a2a62d365f93d55db4fb27f46ecef89 to your computer and use it in GitHub Desktop.
Save didibus/2a2a62d365f93d55db4fb27f46ecef89 to your computer and use it in GitHub Desktop.
Clojure datafy/nav example
#datafy-nav-example
(ns datafy_test.core
(:require [clojure.datafy :as d]
[clojure.core.protocols :as p]))
(defprotocol PName
(getFirst [this])
(getLast [this]))
(deftype Name
[^String first
^String last]
PName
(getFirst [this] (.-first this))
(getLast [this] (.-last this)))
(defprotocol PPerson
(getName [this]))
(deftype Person
[^Name name]
PPerson
(getName [this] (.-name this)))
(def p (Person. (Name. "John" "Dow")))
(extend-protocol
p/Datafiable
Name
(datafy [o]
(vary-meta
(into {} (bean o))
merge
{`p/nav (fn [coll k _]
(get coll k))}))
Person
(datafy [o]
(vary-meta
(into {} (bean o))
merge
{`p/nav (fn [coll k _]
(get coll k))})))
(as-> (d/datafy p) <>
(d/nav <> :name (:name <>))
(d/datafy <>)
(d/nav <> :last (:last <>)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment