Skip to content

Commit 68ac297

Browse files
author
Daniel Neal
committed
Fix nil issue
1 parent 8f84bf9 commit 68ac297

File tree

5 files changed

+97
-103
lines changed

5 files changed

+97
-103
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Change Log
2+
## [2019.10.04]
3+
### Fixed
4+
- Don't terminate loops early if there's a nil in a sequence
5+
26
## [2019.09.14]
37

48
### New Major Version

README.md

+30-30
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,16 @@ Like a one-to-one index except that a nested hash-map of `path* -> item` is main
225225
- `id` - the id for the index in the compound
226226

227227
```clojure
228-
(-> (compound [{:index-type :one-to-one
229-
:id :delivery
230-
:kfn (juxt :customer :delivery-date :product)}
231-
{:index-type :nested-to-one
232-
:path [:customer :delivery-date :product]}])
233-
(add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
234-
{:customer 1 :delivery-date "2012-03-03" :product :apples}
235-
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
236-
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
237-
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
228+
(-> (c/compound [{:index-type :one-to-one
229+
:id :delivery
230+
:kfn (juxt :customer :delivery-date :product)}
231+
{:index-type :nested-to-one
232+
:path [:customer :delivery-date :product]}])
233+
(c/add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
234+
{:customer 1 :delivery-date "2012-03-03" :product :apples}
235+
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
236+
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
237+
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
238238
;; => {:delivery
239239
;; {[1 "2012-03-03" :bananas]
240240
;; {:customer 1, :delivery-date "2012-03-03", :product :bananas},
@@ -291,16 +291,16 @@ Like a one-to-one index except that a nested hash-map of `path* -> item` is main
291291
;; {:potatoes
292292
;; {:customer 2, :delivery-date "2012-03-11", :product :potatoes}}}}};; =>
293293

294-
(-> (compound [{:index-type :one-to-one
295-
:id :delivery
296-
:kfn (juxt :customer :delivery-date :product)}
297-
{:index-type :nested-to-many
298-
:path [:customer :delivery-date]}])
299-
(add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
300-
{:customer 1 :delivery-date "2012-03-03" :product :apples}
301-
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
302-
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
303-
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
294+
(-> (c/compound [{:index-type :one-to-one
295+
:id :delivery
296+
:kfn (juxt :customer :delivery-date :product)}
297+
{:index-type :nested-to-many
298+
:path [:customer :delivery-date]}])
299+
(c/add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
300+
{:customer 1 :delivery-date "2012-03-03" :product :apples}
301+
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
302+
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
303+
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
304304
```
305305

306306
### Index type: nested-to-many
@@ -314,16 +314,16 @@ Like a one-to-many index except that a nested hash-map is `path* -> set` is main
314314
- `id` - the id for the index in the compound
315315

316316
``` clojure
317-
(-> (compound [{:index-type :one-to-one
318-
:id :delivery
319-
:kfn (juxt :customer :delivery-date :product)}
320-
{:index-type :nested-to-many
321-
:path [:customer :delivery-date]}])
322-
(add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
323-
{:customer 1 :delivery-date "2012-03-03" :product :apples}
324-
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
325-
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
326-
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
317+
(-> (c/compound [{:index-type :one-to-one
318+
:id :delivery
319+
:kfn (juxt :customer :delivery-date :product)}
320+
{:index-type :nested-to-many
321+
:path [:customer :delivery-date]}])
322+
(c/add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
323+
{:customer 1 :delivery-date "2012-03-03" :product :apples}
324+
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
325+
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
326+
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
327327
;; => {:delivery
328328
;; {[1 "2012-03-03" :bananas]
329329
;; {:customer 1, :delivery-date "2012-03-03", :product :bananas},

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject riverford/compound "2019.09.14"
1+
(defproject riverford/compound "2019.10.04"
22
:description "A micro structure for reagent data"
33
:url "https://github.com/riverford/compound"
44
:license {:name "Eclipse Public License"

src/compound2/core.cljc

+42-42
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,8 @@
177177
(assoc acc si (before si (get c (id si)))))
178178
{}
179179
sis)
180-
[x & xs] xs]
181-
(if (nil? x)
182-
(with-meta
183-
(reduce-kv (fn [acc si sx]
184-
(assoc acc (id si) (after si sx)))
185-
{(id pi) (after pi px)}
186-
sixs)
187-
(meta c))
180+
[x & more :as xs] xs]
181+
(if (seq xs)
188182
(let [k (extract-key pi x)
189183
ex (get-by-key pi px k)]
190184
(if ex
@@ -196,37 +190,43 @@
196190
(index si (unindex si sx k1 ex) k2 new))))
197191
{}
198192
sixs)
199-
xs))
193+
more))
200194
(recur (index pi px k x)
201195
(reduce-kv (fn [acc si sx]
202196
(assoc acc si (let [k (extract-key si x)]
203197
(index si sx k x))))
204198
{}
205199
sixs)
206-
xs))))))
200+
more)))
201+
(with-meta
202+
(reduce-kv (fn [acc si sx]
203+
(assoc acc (id si) (after si sx)))
204+
{(id pi) (after pi px)}
205+
sixs)
206+
(meta c)))))
207207
`remove-keys (fn [c ks]
208208
(loop [px (before pi (get c (id pi)))
209209
sixs (reduce (fn [acc si]
210210
(assoc acc si (before si (get c (id si)))))
211211
{}
212212
sis)
213-
[k & ks] ks]
214-
(if (nil? k)
215-
(with-meta
216-
(reduce-kv (fn [acc si sx]
217-
(assoc acc (id si) (after si sx)))
218-
{(id pi) (after pi px)}
219-
sixs)
220-
(meta c))
213+
[k & more :as ks] ks]
214+
(if (seq ks)
221215
(if-let [ex (get-by-key pi px k)]
222216
(recur (unindex pi px k ex)
223217
(reduce-kv (fn [acc si sx]
224218
(assoc acc si (let [k (extract-key si ex)]
225219
(unindex si sx k ex))))
226220
{}
227221
sixs)
228-
ks)
229-
(recur px sixs ks)))))})))
222+
more)
223+
(recur px sixs more))
224+
(with-meta
225+
(reduce-kv (fn [acc si sx]
226+
(assoc acc (id si) (after si sx)))
227+
{(id pi) (after pi px)}
228+
sixs)
229+
(meta c)))))})))
230230

231231
#?(:clj
232232
(defmacro compound [indexes]
@@ -257,15 +257,8 @@
257257
[sx `(before ~si (get ~m-sym (id ~si)))])
258258
si-syms
259259
sx-syms)
260-
[~x-sym & xs#] xs#]
261-
(if (nil? ~x-sym)
262-
(with-meta ~(into {`(id ~pi-sym) `(after ~pi-sym ~px-sym)}
263-
(map (fn [si sx]
264-
[`(id ~si)
265-
`(after ~si ~sx)])
266-
si-syms
267-
sx-syms))
268-
(meta ~m-sym))
260+
[~x-sym & more# :as xs#] xs#]
261+
(if (seq xs#)
269262
(let [k# (extract-key ~pi-sym ~x-sym)
270263
~ex-sym (get-by-key ~pi-sym ~px-sym k#)]
271264
(if ~ex-sym
@@ -278,30 +271,30 @@
278271
(index ~si (unindex ~si ~sx k1# ~ex-sym) k2# ~new-sym)))
279272
si-syms
280273
sx-syms)
281-
xs#))
274+
more#))
282275
(recur
283276
(index ~pi-sym ~px-sym k# ~x-sym)
284277
~@(map (fn [si sx]
285278
`(let [k# (extract-key ~si ~x-sym)]
286279
(index ~si ~sx k# ~x-sym)))
287280
si-syms
288281
sx-syms)
289-
xs#))))))
282+
more#)))
283+
(with-meta ~(into {`(id ~pi-sym) `(after ~pi-sym ~px-sym)}
284+
(map (fn [si sx]
285+
[`(id ~si)
286+
`(after ~si ~sx)])
287+
si-syms
288+
sx-syms))
289+
(meta ~m-sym)))))
290290
`remove-keys (fn [~m-sym ks#]
291291
(loop [~px-sym (before ~pi-sym (get ~m-sym (id ~pi-sym)))
292292
~@(mapcat (fn [si sx]
293293
[sx `(before ~si (get ~m-sym (id ~si)))])
294294
si-syms
295295
sx-syms)
296-
[~k-sym & ks#] ks#]
297-
(if (nil? ~k-sym)
298-
(with-meta ~(into {`(id ~pi-sym) `(after ~pi-sym ~px-sym)}
299-
(map (fn [si sx]
300-
[`(id ~si)
301-
`(after ~si ~sx)])
302-
si-syms
303-
sx-syms))
304-
(meta ~m-sym))
296+
[~k-sym & more# :as ks#] ks#]
297+
(if (seq ks#)
305298
(if-let [~ex-sym (get-by-key ~pi-sym ~px-sym ~k-sym)]
306299
(recur
307300
(unindex ~pi-sym ~px-sym ~k-sym ~ex-sym)
@@ -310,8 +303,15 @@
310303
(unindex ~si ~sx k# ~ex-sym)))
311304
si-syms
312305
sx-syms)
313-
ks#)
306+
more#)
314307
(recur
315308
~px-sym
316309
~@sx-syms
317-
ks#)))))})))))
310+
more#))
311+
(with-meta ~(into {`(id ~pi-sym) `(after ~pi-sym ~px-sym)}
312+
(map (fn [si sx]
313+
[`(id ~si)
314+
`(after ~si ~sx)])
315+
si-syms
316+
sx-syms))
317+
(meta ~m-sym)))))})))))

test/compound2/test.cljc

+20-30
Original file line numberDiff line numberDiff line change
@@ -167,33 +167,23 @@
167167
:index-type :one-to-one}])
168168
(c/add-items [{:id 1 :name "Bob"} {:id 2 :name "Terry"} {:id 3 :name "Squirrek"} {:id 3 :color :red}])))))
169169

170-
(-> (c/compound [{:id :by-name ;; defaults to :kfn if :id not provided
171-
:index-type :one-to-one ;; defaults to :one-to-one for primary index
172-
:kfn :name
173-
:on-conflict (fn [a b] (merge a b))}
174-
{:id :by-colour
175-
:index-type :one-to-many ;; defaults to :one-to-many for secondary index
176-
:kfn :colour}
177-
{:id :by-tastiness
178-
:index-type :one-to-many ;; defaults to :one-to-many for secondary index
179-
:kfn :tastiness}])
180-
181-
(c/add-items [{:name :strawberry
182-
:colour :red
183-
:tastiness 4}
184-
185-
{:name :strawberry
186-
:tastiness 5}
187-
188-
{:name :banana
189-
:tastiness 3
190-
:colour :yellow}]))
191-
;; => {:by-name
192-
;; {:strawberry {:name :strawberry, :colour :red, :tastiness 5},
193-
;; :banana {:name :banana, :tastiness 3, :colour :yellow}},
194-
;; :by-colour
195-
;; {:red #{{:name :strawberry, :colour :red, :tastiness 5}},
196-
;; :yellow #{{:name :banana, :tastiness 3, :colour :yellow}}},
197-
;; :by-tastiness
198-
;; {5 #{{:name :strawberry, :colour :red, :tastiness 5}},
199-
;; 3 #{{:name :banana, :tastiness 3, :colour :yellow}}}}
170+
(deftest dont-terminate-early-on-nil
171+
[]
172+
(is (= {:name
173+
{"Terry" {:id 2, :name "Terry"},
174+
"Squirrel" {:id 3, :name "Squirrel"},
175+
"Bob" {:id 1, :name "Bob"}},
176+
:id
177+
{1 {:id 1, :name "Bob"},
178+
2 {:id 2, :name "Terry"},
179+
3 {:id 3, :name "Squirrel"}}}
180+
(-> (c/compound [{:index-type :one-to-one
181+
:kfn :id}
182+
{:index-type :one-to-one
183+
:kfn :name}])
184+
(c/add-items [{:id 1 :name "Bob"} nil {:id 2 :name "Terry"} nil {:id 3 :name "Squirrel"}]))
185+
(-> (c/compound* [{:index-type :one-to-one
186+
:kfn :id}
187+
{:index-type :one-to-one
188+
:kfn :name}])
189+
(c/add-items [{:id 1 :name "Bob"} nil {:id 2 :name "Terry"} nil {:id 3 :name "Squirrel"}])))))

0 commit comments

Comments
 (0)