Back
datafy (clj)
(source)protocol
(datafy o)
return a representation of o as data (default identity)
Examples
babashka/babashka
(ns babashka.impl.protocols
(:require [babashka.impl.protocols :as protocols]
[clojure.core.protocols :as p]
[clojure.datafy :as d]
;; ensure datafy is loaded, we're going to override its
;; clojure.lang.Namespace implementation for datafy
[clojure.reflect]
[sci.core :as sci :refer [copy-var]]
[sci.impl.types :as types]
[sci.impl.vars]))
;;;; datafy
(defmulti datafy types/type-impl)
(defmethod datafy :sci.impl.protocols/reified [x]
(let [methods (types/getMethods x)]
((get methods 'datafy) x)))
(defmethod datafy :default [x]
;; note: Clojure itself will handle checking metadata for impls
(d/datafy x))
(def protocols-namespace
{;; Datafiable
'Datafiable (sci/new-var 'clojure.core.protocols/Datafiable {:methods #{'datafy}
:protocol p/Datafiable
:ns protocols-ns}
{:ns protocols-ns})
'datafy (copy-var datafy protocols-ns)
thheller/shadow-cljs
(ns shadow.remote.runtime.cljs.js-builtins
(:require
[goog.object :as gobj]
[clojure.core.protocols :as p]))
(extend-protocol p/Datafiable
;; FIXME: this is kind of a bad idea
;; can't do this for all objects, since none of the CLJS types implement this
;; protocol either. the protocol dispatch will end up using object
;; FIXME: this could detect CLJS types to some extent
;; or should it just implement the protocols for the types?
object
(datafy [o]
(if-not (identical? (.-__proto__ o) js/Object.prototype)
o
(with-meta
(->> (gobj/getKeys o)
(reduce
(fn [m key]
(assoc! m key (gobj/get o key)))
(transient {}))
(persistent!))
array
(datafy [o]
(vec o))
js/Error
(datafy [e]
(let [data (ex-data e)
file (.-fileName e)
line (.-lineNumber e)
column (.-columnNumber e)]
(-> {:message (.-message e)
:name (.-name e)
:stack (.-stack e)}
(cond->
(some? data)
(assoc :data data)
wilkerlucio/pathom3
(ns com.wsscode.pathom3.interface.smart-map-test
(:require
[clojure.core.protocols :as d]
[clojure.test :refer [deftest is are run-tests testing]]
[com.wsscode.pathom3.connect.built-in.resolvers :as pbir]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.entity-tree :as p.ent]
[com.wsscode.pathom3.interface.smart-map :as psm]
[com.wsscode.pathom3.test.geometry-resolvers :as geo]
[com.wsscode.pathom3.test.helpers :as th]
[matcher-combinators.test])
#?(:clj
(:import
(clojure.lang
ExceptionInfo))))
(deftest smart-map-datafy-test
(let [sm (-> (pci/register [(pbir/alias-resolver :id :name)
(pbir/alias-resolver :id :age)])
(psm/smart-map {:id 10}))
smd (d/datafy sm)]
(is (= smd
{:id 10
:name ::pco/unknown-value
:age ::pco/unknown-value}))
camsaul/methodical
(ns methodical.impl.combo.clojure
"Simple method combination strategy that mimics the way vanilla Clojure multimethods combine methods; that is, to say,
not at all. Like vanilla Clojure multimethods, this method combination only supports primary methods."
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.interface]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(methodical.interface MethodCombination)))
clojure.protocols/Datafiable
(datafy [this]
{:class (class this)})
camsaul/methodical
(ns methodical.impl.multifn.cached
(:require
[clojure.core.protocols :as clojure.protocols]
[clojure.datafy :as datafy]
[methodical.interface :as i]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(clojure.lang Named)
(methodical.interface Cache MultiFnImpl)))
clojure.protocols/Datafiable
(datafy [this]
(assoc (datafy/datafy impl)
:class (class this)
:cache (datafy/datafy cache)))