]> asedeno.scripts.mit.edu Git - cl-protobufs.git/commitdiff
Simplify one interface a tiny bit
authorScott McKay <swm@google.com>
Wed, 13 Jun 2012 22:00:15 +0000 (22:00 +0000)
committerScott McKay <swm@google.com>
Wed, 13 Jun 2012 22:00:15 +0000 (22:00 +0000)
git-svn-id: http://svn.internal.itasoftware.com/svn/ita/trunk/qres/lisp/libs/cl-protobufs@548951 f8382938-511b-0410-9cdd-bb47b084005c

examples.lisp
pkgdcl.lisp
serialize.lisp
tests/cl-protobufs-tests.asd
tests/serialization-tests.lisp

index 8067bf9edeb2a266e07a3e97c05824d721c07ddc..f4809bd49c70cb376c66621839504e668c4cfe55 100644 (file)
     (eval (proto-impl:generate-serializer   message))
     (eval (proto-impl:generate-deserializer message))))
 
-(time (progn (setq gser (proto:serialize-object-to-stream geodata 'geodata :stream nil)) nil))
+(time (progn (setq gser (proto:serialize-object-to-bytes geodata 'geodata)) nil))
 (time (proto:deserialize-object 'geodata gser))
 
-(equalp gser (proto:serialize-object-to-stream
-              (proto:deserialize-object 'geodata gser)
-              'geodata :stream nil))
+(equalp gser (proto:serialize-object-to-bytes
+              (proto:deserialize-object 'geodata gser) 'geodata))
 
-(time (progn (setq gser-v (proto:serialize-object-to-stream geodata-v 'geodata-v :stream nil)) nil))
+(time (progn (setq gser-v (proto:serialize-object-to-bytes geodata-v 'geodata-v)) nil))
 (time (proto:deserialize-object 'geodata-v gser-v))
 
-(equalp gser-v (proto:serialize-object-to-stream
-                (proto:deserialize-object 'geodata-v gser-v)
-                'geodata-v :stream nil))
+(equalp gser-v (proto:serialize-object-to-bytes
+                (proto:deserialize-object 'geodata-v gser-v) 'geodata-v))
 
 (equalp gser gser-v)
 ||#
   (cdr x))
 
 (let ((list '("this" "is" "a" ("nested" "test"))))
-  (proto:serialize-object-to-stream list 'typed-list :stream nil)
+  (proto:serialize-object-to-bytes list 'typed-list)
   (proto:print-text-format list 'typed-list)
   (proto:print-text-format list 'typed-list :suppress-line-breaks t)
   (let ((text (with-output-to-string (s)
       (proto:parse-text-format 'typed-list :stream s))))
 
 (let ((list '((1 one) (2 two) (3 three))))
-  (proto:serialize-object-to-stream list 'typed-list :stream nil)
+  (proto:serialize-object-to-bytes list 'typed-list)
   (proto:print-text-format list 'typed-list)
   (proto:print-text-format list 'typed-list :suppress-line-breaks t)
   (let ((text (with-output-to-string (s)
   (setf (color-opacity color2) 50)
   (progn
     (format t "~2&Unextended (has-extension ~S)~%" (has-extension color1 'opacity))
-    (let ((ser1 (proto:serialize-object-to-stream rqst1 'add-color-request :stream nil)))
+    (let ((ser1 (proto:serialize-object-to-bytes rqst1 'add-color-request)))
       (print ser1)
       (proto:print-text-format rqst1)
       (proto:print-text-format (proto:deserialize-object 'add-color-request ser1))))
   (progn 
     (format t "~2&Extended (has-extension ~S)~%" (has-extension color2 'opacity))
-    (let ((ser2 (proto:serialize-object-to-stream rqst2 'add-color-request :stream nil)))
+    (let ((ser2 (proto:serialize-object-to-bytes rqst2 'add-color-request)))
       (print ser2)
       (proto:print-text-format rqst2)
       (proto:print-text-format (proto:deserialize-object 'add-color-request ser2)))))
        (rqst2  (make-instance 'add-color2 :wheel wheel2 :color color2)))
   (progn
     (format t "~2&Nested")
-    (let ((ser1 (proto:serialize-object-to-stream rqst1 'add-color1 :stream nil)))
+    (let ((ser1 (proto:serialize-object-to-bytes rqst1 'add-color1)))
       (print ser1)
       (proto:print-text-format rqst1)
       (proto:print-text-format (proto:deserialize-object 'add-color1 ser1))))
   (progn
     (format t "~2&Group")
-    (let ((ser2 (proto:serialize-object-to-stream rqst2 'add-color2 :stream nil)))
+    (let ((ser2 (proto:serialize-object-to-bytes rqst2 'add-color2)))
       (print ser2)
       (proto:print-text-format rqst2)
       (proto:print-text-format (proto:deserialize-object 'add-color2 ser2)))))
index 15fd019f3bc7abdbcb45475e436eca87904172ba..d52906152ddeb5212d52c3dff2c74f277415e75f 100644 (file)
    ;; Serialization and deserialization (wire format)
    "SERIALIZE-OBJECT-TO-FILE"
    "SERIALIZE-OBJECT-TO-STREAM"
+   "SERIALIZE-OBJECT-TO-BYTES"
    "SERIALIZE-OBJECT"
    "DESERIALIZE-OBJECT-FROM-FILE"
    "DESERIALIZE-OBJECT-FROM-STREAM"
+   "DESERIALIZE-OBJECT-FROM-BYTES"
    "DESERIALIZE-OBJECT"
    "OBJECT-SIZE"
 
index 97149f2a11dc96c92b0d540f54ab22c88d0c373c..bb36ad5470ae8e6f465440d4dec861b874aeba91 100644 (file)
 
 ;;; Serialization
 
-;; Serialize the object using the given protobuf type
+(defun serialize-object-to-file (filename object type &key visited)
+  "Serializes the object 'object' of type 'type' into the file 'filename'
+   using the wire format.
+   'object' and 'type' are the same as for 'serialize-object-to-bytes'."
+  (with-open-file (stream filename
+                   :direction :output
+                   :element-type '(unsigned-byte 8))
+    (serialize-object-to-stream object type :stream stream :visited visited)))
+
 (defun serialize-object-to-stream (object type &key (stream *standard-output*) visited)
   "Serializes the object 'object' of type 'type' onto the stream 'stream'
+   using the wire format.
+   'object' and 'type' are the same as for 'serialize-object-to-bytes'."
+  (let ((buffer (serialize-object-to-bytes object type :visited visited)))
+    (write-sequence buffer stream)
+    buffer))
+
+(defun serialize-object-to-bytes (object type &key visited)
+  "Serializes the object 'object' of type 'type' into a new byte vector
    using the wire format.
    'type' is the Lisp name of a Protobufs message (usually the name of a 
    Lisp class) or a 'protobuf-message'.
          (size    (object-size object type visited))
          (buffer  (make-byte-vector size)))
     (serialize-object object type buffer 0 visited)
-    (when stream
-      (write-sequence buffer stream))
     buffer))
 
-(defun serialize-object-to-file (filename object type &key visited)
-  (with-open-file (stream filename
-                   :direction :output
-                   :element-type '(unsigned-byte 8))
-    (serialize-object-to-stream object type :stream stream :visited visited)))
+;; Serialize the object using the given protobuf type
+
 
 ;; Allow clients to add their own methods
 ;; This is how we address the problem of cycles, e.g. -- if you have an object
 
 ;;; Deserialization
 
+(defun deserialize-object-from-file (type filename)
+  "Deserializes an object of the given type 'type' from the given file
+   as a Protobuf object."
+  (with-open-file (stream filename
+                   :direction :input
+                   :element-type '(unsigned-byte 8))
+    (deserialize-object-from-stream type :stream stream)))
+
 (defun deserialize-object-from-stream (type &key (stream *standard-input*))
-  "Deserializes an object of the given type 'type' as a Protobuf object.
-   'type' is the Lisp name of a Protobufs message (usually the name of a 
-   Lisp class) or a 'protobuf-message'.
-   The return value is the object."
+  "Deserializes an object of the given type 'type' from the given stream
+   as a Protobuf object."
   (let* ((size    (file-length stream))
          (buffer  (make-byte-vector size)))
     (read-sequence buffer stream)
     (deserialize-object type buffer 0 size)))
 
-(defun deserialize-object-from-file (type filename)
-  (with-open-file (stream filename
-                   :direction :input
-                   :element-type '(unsigned-byte 8))
-    (deserialize-object-from-stream type :stream stream)))
+(defun deserialize-object-from-bytes (type buffer)
+  "Deserializes an object of the given type 'type' from the given stream
+   as a Protobuf object.
+   'type' is the Lisp name of a Protobufs message (usually the name of a 
+   Lisp class) or a 'protobuf-message'.
+   The return value is the object."
+  (deserialize-object type buffer))
 
 ;; Allow clients to add their own methods
 ;; This is you might preserve object identity, e.g.
index f44b6085be281f9a6ec10b585d121958d634c0b3..be4394bb5e71860b3968861e335d1b043ab6b8a8 100644 (file)
@@ -19,7 +19,7 @@
   :maintainer '("Scott McKay")
   :description      "Test code for Protobufs for Common Lisp"
   :long-description "Test code for Protobufs for Common Lisp"
-  :depends-on (:protobufs :quux :test-tools
+  :depends-on (:cl-protobufs :quux :test-tools
               ;; Some of these tests use QRes business data
               #+qres :qres-core)
   :serial t
index cce1192b598c32d2c6a17699f89b0030b7a48373..bc98ce0022fcc47dcf11218502696f96222f3fcd 100644 (file)
                    :color :red :intvals '(2 3 5 7) :strvals '("two" "three" "five" "seven")))
          (test6  (make-instance 'basic-test6
                    :intvals '(2 3 5 7) :strvals '("two" "three" "five" "seven") :recvals (list test2 test2b))))
-    (let ((tser1  (serialize-object-to-stream test1 'basic-test1 :stream nil))
-          (tser1b (serialize-object-to-stream test1b 'basic-test1 :stream nil))
-          (tser2  (serialize-object-to-stream test2 'basic-test2 :stream nil))
-          (tser3  (serialize-object-to-stream test3 'basic-test3 :stream nil))
-          (tser4  (serialize-object-to-stream test4 'basic-test4 :stream nil))
-          (tser5  (serialize-object-to-stream test5 'basic-test5 :stream nil))
-          (tser6  (serialize-object-to-stream test6 'basic-test6 :stream nil)))
+    (let ((tser1  (serialize-object-to-bytes test1 'basic-test1))
+          (tser1b (serialize-object-to-bytes test1b 'basic-test1))
+          (tser2  (serialize-object-to-bytes test2 'basic-test2))
+          (tser3  (serialize-object-to-bytes test3 'basic-test3))
+          (tser4  (serialize-object-to-bytes test4 'basic-test4))
+          (tser5  (serialize-object-to-bytes test5 'basic-test5))
+          (tser6  (serialize-object-to-bytes test6 'basic-test6)))
       (qtest:assert-true (equalp tser1 #(#x08 #x96 #x01)))
       (qtest:assert-true (equalp tser1b #(#x08 #xEA #xFE #xFF #xFF #x0F)))
       (qtest:assert-true (equalp tser2 #(#x12 #x07 #x74 #x65 #x73 #x74 #x69 #x6E #x67)))
                    :color :red :intvals '(2 3 5 7) :strvals '("two" "three" "five" "seven")))
          (test6  (make-instance 'basic-test6
                    :intvals '(2 3 5 7) :strvals '("two" "three" "five" "seven") :recvals (list test2 test2b))))
-    (let ((tser1  (serialize-object-to-stream test1 'basic-test1 :stream nil))
-          (tser1b (serialize-object-to-stream test1b 'basic-test1 :stream nil))
-          (tser2  (serialize-object-to-stream test2 'basic-test2 :stream nil))
-          (tser3  (serialize-object-to-stream test3 'basic-test3 :stream nil))
-          (tser4  (serialize-object-to-stream test4 'basic-test4 :stream nil))
-          (tser5  (serialize-object-to-stream test5 'basic-test5 :stream nil))
-          (tser6  (serialize-object-to-stream test6 'basic-test6 :stream nil)))
+    (let ((tser1  (serialize-object-to-bytes test1 'basic-test1))
+          (tser1b (serialize-object-to-bytes test1b 'basic-test1))
+          (tser2  (serialize-object-to-bytes test2 'basic-test2))
+          (tser3  (serialize-object-to-bytes test3 'basic-test3))
+          (tser4  (serialize-object-to-bytes test4 'basic-test4))
+          (tser5  (serialize-object-to-bytes test5 'basic-test5))
+          (tser6  (serialize-object-to-bytes test6 'basic-test6)))
       (qtest:assert-true (equalp tser1 #(#x08 #x96 #x01)))
       (qtest:assert-true (equalp tser1b #(#x08 #xEA #xFE #xFF #xFF #x0F)))
       (qtest:assert-true (equalp tser2 #(#x12 #x07 #x74 #x65 #x73 #x74 #x69 #x6E #x67)))
                    :color :red :intvals '(2 3 5 7) :strvals '("two" "three" "five" "seven")))
          (test6  (make-instance 'basic-test6
                    :intvals '(2 3 5 7) :strvals '("two" "three" "five" "seven") :recvals (list test2 test2b))))
-    (let ((tser1  (serialize-object-to-stream test1 'basic-test1 :stream nil))
-          (tser1b (serialize-object-to-stream test1b 'basic-test1 :stream nil))
-          (tser2  (serialize-object-to-stream test2 'basic-test2 :stream nil))
-          (tser3  (serialize-object-to-stream test3 'basic-test3 :stream nil))
-          (tser4  (serialize-object-to-stream test4 'basic-test4 :stream nil))
-          (tser5  (serialize-object-to-stream test5 'basic-test5 :stream nil))
-          (tser6  (serialize-object-to-stream test6 'basic-test6 :stream nil)))
+    (let ((tser1  (serialize-object-to-bytes test1 'basic-test1))
+          (tser1b (serialize-object-to-bytes test1b 'basic-test1))
+          (tser2  (serialize-object-to-bytes test2 'basic-test2))
+          (tser3  (serialize-object-to-bytes test3 'basic-test3))
+          (tser4  (serialize-object-to-bytes test4 'basic-test4))
+          (tser5  (serialize-object-to-bytes test5 'basic-test5))
+          (tser6  (serialize-object-to-bytes test6 'basic-test6)))
       (macrolet ((slots-equalp (obj1 obj2 &rest slots)
                    (quux:with-gensyms (vobj1 vobj2)
                      (quux:with-collectors ((forms collect-form))
 (qtest:define-test serialization-integrity ()
   (flet ((do-test (message)
            (let* ((type (type-of message))
-                  (buf (proto:serialize-object-to-stream message type :stream nil))
+                  (buf (proto:serialize-object-to-bytes message type))
                   (new (proto:deserialize-object type buf))
-                  (newbuf (proto:serialize-object-to-stream new type :stream nil)))
+                  (newbuf (proto:serialize-object-to-bytes new type)))
              (qtest:assert-true (equalp (length buf) (length newbuf)))
              (qtest:assert-true (equalp buf newbuf))
              (qtest:assert-true (string= (with-output-to-string (s)
                     :regions regions
                     :cities cities
                     :airports airports)))
-    (let ((gser (proto:serialize-object-to-stream geodata 'geodata :stream nil)))
-      (qtest:assert-true (equalp gser (proto:serialize-object-to-stream
-                                       (proto:deserialize-object 'geodata gser)
-                                       'geodata :stream nil))))))
+    (let ((gser (proto:serialize-object-to-bytes geodata 'geodata)))
+      (qtest:assert-true (equalp gser (proto:serialize-object-to-bytes
+                                       (proto:deserialize-object 'geodata gser) 'geodata))))))
 
 (qtest:define-test geodata-optimized-serialization ()
   (dolist (class '(qres-core::country qres-core::region qres-core::region-key
                     :regions regions
                     :cities cities
                     :airports airports)))
-    (let ((gser (proto:serialize-object-to-stream geodata 'geodata :stream nil)))
-      (qtest:assert-true (equalp gser (proto:serialize-object-to-stream
-                                       (proto:deserialize-object 'geodata gser)
-                                       'geodata :stream nil))))))
+    (let ((gser (proto:serialize-object-to-bytes geodata 'geodata)))
+      (qtest:assert-true (equalp gser (proto:serialize-object-to-bytes
+                                       (proto:deserialize-object 'geodata gser) 'geodata))))))
 
 )       ;#+qres
 
          (car2   (make-instance 'automobile :model "Audi" :color color2))
          (rqst2  (make-instance 'buy-car-request :auto car2)))
     (setf (auto-color-paint-type color2) :metallic)
-    (let ((ser1 (proto:serialize-object-to-stream rqst1 'buy-car-request :stream nil)))
+    (let ((ser1 (proto:serialize-object-to-bytes rqst1 'buy-car-request)))
       (qtest:assert-true (string= (with-output-to-string (s)
                                     (proto:print-text-format rqst1 nil :stream s))
                                   (with-output-to-string (s)
                                     (proto:print-text-format
                                       (proto:deserialize-object 'buy-car-request ser1)  nil :stream s)))))
-    (let ((ser2 (proto:serialize-object-to-stream rqst2 'buy-car-request :stream nil)))
+    (let ((ser2 (proto:serialize-object-to-bytes rqst2 'buy-car-request)))
       (qtest:assert-true (string= (with-output-to-string (s)
                                     (proto:print-text-format rqst2 nil :stream s))
                                   (with-output-to-string (s)
          (wheel2 (make-instance 'color-wheel2 :name "Colors" :metadata meta2))
          (color2 (make-instance 'color2 :r-value 100 :g-value 0 :b-value 100))
          (rqst2  (make-instance 'add-color2 :wheel wheel2 :color color2)))
-    (let ((ser1 (proto:serialize-object-to-stream rqst1 'add-color1 :stream nil))
-          (ser2 (proto:serialize-object-to-stream rqst2 'add-color2 :stream nil)))
+    (let ((ser1 (proto:serialize-object-to-bytes rqst1 'add-color1))
+          (ser2 (proto:serialize-object-to-bytes rqst2 'add-color2)))
       (qtest:assert-true (string= (with-output-to-string (s)
                                     (proto:print-text-format rqst1 nil :stream s))
                                   (with-output-to-string (s)