Skip to content

Commit

Permalink
Merge pull request #140 from Taoensso/lazyseq
Browse files Browse the repository at this point in the history
Allow lazy sequences to be serialized as long as they have been realized
  • Loading branch information
kipz authored Nov 18, 2019
2 parents c9fdecd + 6e4c97e commit 7cac7fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/taoensso/faraday.clj
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,13 @@
(enc/revery? enc/stringy? s) (doto (AttributeValue.) (.setSS (mapv enc/as-qname s)))
(enc/revery? ddb-num? s) (doto (AttributeValue.) (.setNS (mapv str s)))
(enc/revery? freeze? s) (doto (AttributeValue.) (.setBS (mapv nt-freeze s)))
:else (throw (Exception. "Invalid DynamoDB value: set of invalid type or more than one type"))))))
:else (throw (Exception. "Invalid DynamoDB value: set of invalid type or more than one type")))))

clojure.lang.LazySeq
(serialize [s]
(if (.isRealized s)
(doto (AttributeValue.) (.setL (mapv serialize s)))
(throw (IllegalArgumentException. "Unrealized lazy sequences are not supported. Realize this sequence before calling Faraday (e.g. doall) or replace the sequence with a non-lazy alternative (e.g. 'mapv' instead of 'map', or use 'into []'). Faraday avoids attempting to realize values that might be infinite, since this could cause strange an unexpected problems that are hard to diagnose.")))))

(extend-type (Class/forName "[B")
ISerializable
Expand Down
13 changes: 13 additions & 0 deletions test/taoensso/faraday/tests/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1329,3 +1329,16 @@
(far/put-item *client-opts* range-table {:title "Three" :number 35}
{:cond-expr "attribute_not_exists(#t)"
:expr-attr-names {"#t" "title"}}))

(deftest lazy-seqs

(testing "Lazy seq that is not realized cannot be serialized"
(is (thrown? IllegalArgumentException
(far/put-item *client-opts* ttable {:id 10 :items (map str (range 5))}))))

(testing "Lazy seqs that are realized are okay"
(far/put-item *client-opts* ttable {:id 10 :items (doall (map str (range 5)))})
(is (= ["0" "1" "2" "3" "4"] (:items (far/get-item *client-opts* ttable {:id 10}))))

(far/put-item *client-opts* ttable {:id 10 :items (mapv str (range 5))})
(is (= ["0" "1" "2" "3" "4"] (:items (far/get-item *client-opts* ttable {:id 10}))))))

0 comments on commit 7cac7fe

Please sign in to comment.