Skip to content

Commit

Permalink
Fix associative spec to allow for nested dynamic size specs
Browse files Browse the repository at this point in the history
closes #16
  • Loading branch information
tomerweller committed Mar 22, 2019
1 parent b70ebdb commit 9c2313e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/octet/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@
(size* [_ data]
(reduce (fn [acc [field data]]
(let [type (field dict)]
(if (satisfies? ISpecSize type)
(+ acc (size type))
(+ acc (size* type data)))))
(if (satisfies? ISpecDynamicSize type)
(+ acc (size* type data))
(+ acc (size type)))))
0
(into [] data)))

Expand Down
10 changes: 10 additions & 0 deletions test/octet/tests/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,16 @@
(t/is (= readed 24))
(t/is (= data [1 2 3 4 5])))))

(t/deftest spec-associative-nested-dynamic
(let [spec (buf/spec :outer (buf/spec :inner (buf/vector* buf/int32)))
buffer (buf/into (buf/spec :outer (buf/spec :inner (buf/vector* buf/int32))) {:outer {:inner [1]}})
written (impl/get-capacity buffer)]
(t/is (= written 8))

(let [[readed data] (buf/read* buffer spec)]
(t/is (= readed 8)
(t/is (= data {:outer {:inner [1]}}))))))

#?(:cljs
(do
(enable-console-print!)
Expand Down

0 comments on commit 9c2313e

Please sign in to comment.