Last active
January 5, 2023 05:09
-
-
Save didibus/2a2a62d365f93d55db4fb27f46ecef89 to your computer and use it in GitHub Desktop.
Clojure datafy/nav example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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