Skip to content

Commit

Permalink
Participate in adjoin protocol instead of inventing append function
Browse files Browse the repository at this point in the history
  • Loading branch information
amalloy committed Apr 25, 2012
1 parent f9519e8 commit e4aa378
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
11 changes: 6 additions & 5 deletions src/protobuf/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:use [protobuf.schema :only [field-schema]]
[useful.fn :only [fix]]
[clojure.java.io :only [input-stream output-stream]])
(:require useful.utils)
(:import (protobuf.core PersistentProtocolBufferMap PersistentProtocolBufferMap$Def PersistentProtocolBufferMap$Def$NamingStrategy Extensions)
(com.google.protobuf GeneratedMessage CodedInputStream Descriptors$Descriptor)
(java.io InputStream OutputStream)
Expand Down Expand Up @@ -89,12 +90,12 @@
(.writeDelimitedTo p out))
(.flush out))))

;; TODO make these functions nil-safe
(defn append
"Merge the given map into the protobuf. Equivalent to appending the byte representations."
[^PersistentProtocolBufferMap p map]
(.append p map))
(extend-protocol useful.utils/Adjoin
PersistentProtocolBufferMap
(adjoin [^PersistentProtocolBufferMap this other]
(.append this other)))

;; TODO make this nil-safe? Or just delete it?
(defn get-raw
"Get value at key ignoring extension fields."
[^PersistentProtocolBufferMap p key]
Expand Down
29 changes: 15 additions & 14 deletions test/protobuf/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns protobuf.core-test
(:use protobuf.core clojure.test
[useful.utils :only [adjoin]]
ordered-map.core)
(:import (java.io PipedInputStream PipedOutputStream)))

Expand Down Expand Up @@ -34,31 +35,31 @@
(is (= ["little" "yellow" "different"] (:tags p)))
(is (= "very" (:label p))))))

(deftest test-append
(deftest test-adjoin
(let [p (protobuf Foo :id 5 :tags ["little" "yellow"] :doubles [1.2] :floats [0.01])]
(let [p (append p {:label "bar"})]
(let [p (adjoin p {:label "bar"})]
(is (= 5 (:id p)))
(is (= "bar" (:label p)))
(is (= false (:deleted p)))
(is (= ["little" "yellow"] (:tags p))))
(let [p (append (assoc p :deleted true) p)]
(let [p (adjoin (assoc p :deleted true) p)]
(is (= true (:deleted p))))
(let [p (append p {:tags ["different"]})]
(let [p (adjoin p {:tags ["different"]})]
(is (= ["little" "yellow" "different"] (:tags p))))
(let [p (append p {:tags ["different"] :label "very"})]
(let [p (adjoin p {:tags ["different"] :label "very"})]
(is (= ["little" "yellow" "different"] (:tags p)))
(is (= "very" (:label p))))
(let [p (append p {:doubles [3.4] :floats [0.02]})]
(let [p (adjoin p {:doubles [3.4] :floats [0.02]})]
(is (= [1.2 3.4] (:doubles p)))
(is (= [(float 0.01) (float 0.02)] (:floats p))))))

(deftest test-ordered-append
(deftest test-ordered-adjoin
(let [inputs (apply ordered-map (for [x (range 26)
entry [(str x) (str (char (+ (int \a) x)))]]
entry))]
(= (seq inputs)
(seq (reduce (fn [m [k v]]
(append m {:attr_map {k v}}))
(adjoin m {:attr_map {k v}}))
(protobuf Foo)
inputs)))))

Expand Down Expand Up @@ -181,11 +182,11 @@
(let [p (protobuf Foo :counts {"foo" {:i 5 :d 5.0}})]
(is (= 5 (get-in p [:counts "foo" :i])))
(is (= 5.0 (get-in p [:counts "foo" :d])))
(let [p (append p {:counts {"foo" {:i 2 :d -2.4} "bar" {:i 99}}})]
(let [p (adjoin p {:counts {"foo" {:i 2 :d -2.4} "bar" {:i 99}}})]
(is (= 7 (get-in p [:counts "foo" :i])))
(is (= 2.6 (get-in p [:counts "foo" :d])))
(is (= 99 (get-in p [:counts "bar" :i])))
(let [p (append p {:counts {"foo" {:i -8 :d 4.06} "bar" {:i -66}}})]
(let [p (adjoin p {:counts {"foo" {:i -8 :d 4.06} "bar" {:i -66}}})]
(is (= -1 (get-in p [:counts "foo" :i])))
(is (= 6.66 (get-in p [:counts "foo" :d])))
(is (= 33 (get-in p [:counts "bar" :i])))
Expand All @@ -201,7 +202,7 @@
(is (= 1978 (get-in p [:time :year])))
(is (= 11 (get-in p [:time :month])))
(is (= 24 (get-in p [:time :day])))
(let [p (append p {:time {:year 1974 :month 1}})]
(let [p (adjoin p {:time {:year 1974 :month 1}})]
(is (= 1974 (get-in p [:time :year])))
(is (= 1 (get-in p [:time :month])))
(is (= nil (get-in p [:time :day])))
Expand All @@ -218,7 +219,7 @@
(is (= "foo" (get p :str)))
(is (= :a (get p :enu)))
(is (= keyset (set (keys p))))
(let [p (append p {:int nil :long nil :flt nil :dbl nil :str nil :enu nil})]
(let [p (adjoin p {:int nil :long nil :flt nil :dbl nil :str nil :enu nil})]
(is (= nil (get p :int)))
(is (= nil (get p :long)))
(is (= nil (get p :flt)))
Expand All @@ -229,13 +230,13 @@
(testing "nullable successions"
(let [p (protobuf Bar :label "foo")]
(is (= "foo" (get p :label)))
(let [p (append p {:label nil})]
(let [p (adjoin p {:label nil})]
(is (= nil (get p :label)))
(is (= ["foo" ""] (get-raw p :label))))))
(testing "repeated nullable"
(let [p (protobuf Bar :labels ["foo" "bar"])]
(is (= ["foo" "bar"] (get p :labels)))
(let [p (append p {:labels [nil]})]
(let [p (adjoin p {:labels [nil]})]
(is (= ["foo" "bar" nil] (get p :labels)))
(is (= ["foo" "bar" ""] (get-raw p :labels))))))))

Expand Down

0 comments on commit e4aa378

Please sign in to comment.