Skip to content

Commit

Permalink
[v4] Settle on API for attribute map replies
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Sep 18, 2024
1 parent ef003c6 commit 37ea478
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
20 changes: 10 additions & 10 deletions src/taoensso/carmine_v4/resp/read.clj
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
(declare complete-reply)

(defrecord VerbatimString [format content])
(defrecord WithAttributes [content attributes])

(let [sentinel-end-of-aggregate-stream com/sentinel-end-of-aggregate-stream
sentinel-null-reply com/sentinel-null-reply]
Expand Down Expand Up @@ -426,20 +427,19 @@
(int \%) (read-aggregate-by-pairs read-opts read-reply in) ; Aggregate map ✓

(int \|) ; Attribute map ✓
(let [attrs (read-aggregate-by-pairs read-opts read-reply in)
target (read-reply read-opts in)]
(let [attrs (read-aggregate-by-pairs read-opts read-reply in)
content (read-reply read-opts in)]

(when-not skip?
;; TODO API okay?
(if (instance? clojure.lang.IObj target)
(with-meta target {:carmine/attributes attrs})
[:carmine/with-attributes target attrs]
(if (and (enc/can-meta? content) (map? attrs))
(with-meta content attrs)
(WithAttributes. content attrs)
#_
(throw
(ex-info "[Carmine] Attributes reply for unexpected (non-IObj) type"
{:eid :carmine.read/attributes-for-unexpected-type
:target (enc/typed-val target)
:attributes attrs})))))
(ex-info "[Carmine] Unexpected attributes reply"
{:eid :carmine.read/unexpected-attributes
:content (enc/typed-val content)
:attributes (enc/typed-val attrs)})))))

(int \>) ; Push ✓
(let [v (read-aggregate-by-ones [] com/read-opts-natural read-reply in)]
Expand Down
19 changes: 12 additions & 7 deletions test/taoensso/carmine_v4/tests/resp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(:import
[taoensso.carmine_v4.resp.common #_ReadOpts ReadThawed #_Parser]
[taoensso.carmine_v4.resp.write WriteFrozen]
[taoensso.carmine_v4.resp.read VerbatimString]))
[taoensso.carmine_v4.resp.read VerbatimString WithAttributes]))

(comment
(remove-ns 'taoensso.carmine-v4.tests.resp)
Expand Down Expand Up @@ -191,12 +191,17 @@
(is (= (binding [core/*raw-verbatim-strings?* false] (rr (xs->in+ "=11" "txt:hello\r\n"))) "hello\r\n"))])

(testing "Attribute map type"
[(is (enc/submap?
{:carmine/attributes {:key-popularity {:a 0.1923 :b 0.0012}}}
(meta
(rr (xs->in+ "|1" "+key-popularity"
"%2" "$1" "a" ",0.1923" "$1" "b" ",0.0012"
"*2" ":2039123" ":9543892")))))])
[(is (= (meta
(rr (xs->in+ "|1" "+key-popularity"
"%2" "$1" "a" ",0.1923" "$1" "b" ",0.0012"
"*2" ":2039123" ":9543892")))
{:key-popularity {:a 0.1923 :b 0.0012}}))

(is (=
(rr (xs->in+ "|1" "+key-popularity"
"%2" "$1" "a" ",0.1923" "$1" "b" ",0.0012"
"$7" "hello\r\n"))
(WithAttributes. "hello\r\n" {:key-popularity {:a 0.1923 :b 0.0012}})))])

(testing "Pushes"
;; Push replies can be received at any time, but only at the top level
Expand Down

0 comments on commit 37ea478

Please sign in to comment.