Skip to content

Instantly share code, notes, and snippets.

@taylorSando
Created December 25, 2012 07:38
Show Gist options
  • Save taylorSando/4372079 to your computer and use it in GitHub Desktop.
Save taylorSando/4372079 to your computer and use it in GitHub Desktop.
You can't use map or vector binding in the params of a datomic function map. It will cause a 'clojure.lang.PersistentVector cannot be cast to java.lang.Stringclojure.lang.PersistentVector cannot be cast to java.lang.String
;; You can't use map or vector binding in the params of a datomic function map. It will cause a
;; 'clojure.lang.PersistentVector cannot be cast to java.lang.Stringclojure.lang.PersistentVector cannot be cast to java.lang.String
;; For example, I was attempting ot create a function so that I could pass the following to the transactor function:
;; (d/transact *conn* [ [:node/add {:node/description "Hello" :node/uri "http://www.example.com" :node/tags ["tag1", "tag2"]]])
;; This causes an error, because params isn't being treated like it can be bound to. You can only specify symbols to bind to
(def add-node {:db/doc "A function for adding a node to the database"
:db/ident :node/add
:db/fn (d/function '{:lang :clojure
:params [db [{:keys [uri description tags prerequisites]}]]
:code [((when-not (seq (d/q '[:find ?e
:in $ ?uri ?desc
:where
[?e :node/description ?desc]
[?e :node/uri ?uri]]
db uri description))
(let [node-map (hash-map :db/id (d/tempid :db.part/user)
:node/uri uri
:node/description description
:node/tag tags)]
(if prerequisites
(assoc node-map :node/prerequisite prerequisites)
node-map))))]})}
;; The solution is to change params and add a let within the code section to do the binding for you
(def add-node {:db/doc "A function for adding a node to the database"
:db/ident :node/add
:db/fn (d/function '{:lang :clojure
:params [db param-map]]
:code [(let [{:keys [uri description tags prerequisites]} param-map]
(when-not (seq (d/q '[:find ?e
:in $ ?uri ?desc
:where
[?e :node/description ?desc]
[?e :node/uri ?uri]]
db uri description))
(let [node-map (hash-map :db/id (d/tempid :db.part/user)
:node/uri uri
:node/description description
:node/tag tags)]
(if prerequisites
(assoc node-map :node/prerequisite prerequisites)
node-map))))]})})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment