Skip to content

Commit

Permalink
Added lookup refs support in values
Browse files Browse the repository at this point in the history
  • Loading branch information
Paula Gearon committed Mar 12, 2021
1 parent c24eff7 commit b13f92f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.clojars.quoll/zuko "0.4.5"
(defproject org.clojars.quoll/zuko "0.4.6"
:description "Threatgrid library for common graph database functionality"
:url "https://github.com/threatgrid/zuko"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down
22 changes: 19 additions & 3 deletions src/zuko/entity/writer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@
node)
:tg/empty-list))

(defn lookup-ref?
"Tests if i is a lookup ref"
[i]
(and (vector? i) (keyword? (first i)) (= 2 (count i))))

(defn resolve-ref
[[prop id]]
(or (and (= :db/id prop) (get @*id-map* id id))
(ffirst (node/find-triple *current-graph* ['?n prop id]))))

(defn value-triples
"Converts a value into a list of triples.
Return the entity ID of the data."
[v]
(cond
(lookup-ref? v) (or (resolve-ref v)
(value-triples-list v))
(sequential? v) (value-triples-list v)
(set? v) (value-triples-list (seq v))
(map? v) (map->triples v)
Expand Down Expand Up @@ -95,7 +107,10 @@
(if-let [r (@*id-map* id)] ;; an ID that is already mapped
[r false]
(cond ;; a negative ID is a request for a new saved ID
(and (number? id) (neg? id)) [(new-node id) false]
(and (number? id) (neg? id)) (let [new-id (new-node id)]
(when ident
(vswap! *id-map* assoc ident new-id))
[new-id false])
;; Use the provided ID
id (if (node/node-type? *current-graph* :db/id id)
[id false]
Expand All @@ -114,13 +129,14 @@


(s/defn map->triples
"Converts a single map to triples. Returns the entity reference or node id."
"Converts a single map to triples. Returns the entity reference or node id.
The triples are built up statefully in the volatile *triples*."
[data :- {s/Keyword s/Any}]
(let [[entity-ref ident?] (get-ref data)
data (dissoc data :db/id)
data (if ident? (dissoc data :db/ident) data)]
(doseq [d data]
(property-vals entity-ref d))
(property-vals entity-ref d)) ;; build up result in *triples*
entity-ref))


Expand Down

0 comments on commit b13f92f

Please sign in to comment.