]> asedeno.scripts.mit.edu Git - cl-protobufs.git/blob - upgradable.lisp
Add some support for testing: 'protobufs-equal'
[cl-protobufs.git] / upgradable.lisp
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;                                                                  ;;;
3 ;;; Confidential and proprietary information of ITA Software, Inc.   ;;;
4 ;;;                                                                  ;;;
5 ;;; Copyright (c) 2012 ITA Software, Inc.  All rights reserved.      ;;;
6 ;;;                                                                  ;;;
7 ;;; Original author: Scott McKay                                     ;;;
8 ;;;                                                                  ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
11 (in-package "PROTO-IMPL")
12
13
14 ;;; Can a version of a Protobufs schema be upgraded to a new version
15
16 (defgeneric protobuf-upgradable (old new &optional old-parent new-parent)
17   (:documentation
18    "Returns true if and only if the old Protobufs schema can be upgraded to
19     the new schema.
20     'old' is the old object (schema, enum, message, etc), 'new' is the new one.
21     'old-parent' is the \"parent\" of 'old', 'new-parent' is the parent of 'new'.
22     If the schema is not upgradable, the second value is a list of warnings."))
23
24 (defvar *upgrade-warnings*)
25 (defmacro upgrade-warn ((predicate old new) format-string &optional name)
26   "Collect an upgrade warning into *upgrade-warnings*."
27   (with-gensyms (vold vnew)
28     `(let* ((,vold ,old)
29             (,vnew ,new))
30        (cond ((,predicate ,vold ,vnew)
31               t)
32              (t
33               ;; Note that this returns the non-NIL value of *upgrade-warnings*,
34               ;; so the upgradable check will continue to collect warnings
35               (push (format nil ,format-string
36                             ,@(if name (list name vold vnew) (list vold vnew)))
37                     *upgrade-warnings*))))))
38
39 (defmethod protobuf-upgradable ((old protobuf) (new protobuf)
40                                 &optional old-parent new-parent)
41   (declare (ignore old-parent new-parent))
42   (let ((*upgrade-warnings* ()))
43     (and
44      ;; Are they named the same?
45      (upgrade-warn (string= (proto-name old) (proto-name new))
46                    "Protobuf schema name changed from '~A' to '~A'")
47      (upgrade-warn (string= (proto-package old) (proto-package new))
48                    "Protobuf schema package changed from '~A' to '~A'")
49      ;; Is every enum in 'old' upgradable to an enum in 'new'?
50      (loop for old-enum in (proto-enums old)
51            as new-enum = (find (proto-name old-enum) (proto-enums new)
52                                :key #'proto-name :test #'string=)
53            always (and new-enum (protobuf-upgradable old-enum new-enum old new)))
54      ;; Is every message in 'old' upgradable to a message in 'new'?
55      (loop for old-msg in (proto-messages old)
56            as new-msg = (find (proto-name old-msg) (proto-messages new)
57                               :key #'proto-name :test #'string=)
58            always (and new-msg (protobuf-upgradable old-msg new-msg old new)))
59      ;; Is every service in 'old' upgradable to a service in 'new'?
60      (loop for old-svc in (proto-services old)
61            as new-svc = (find (proto-name old-svc) (proto-services new)
62                               :key #'proto-name :test #'string=)
63            always (and new-svc (protobuf-upgradable old-svc new-svc old new))))
64     (values (null *upgrade-warnings*) (nreverse *upgrade-warnings*))))
65
66
67 (defmethod protobuf-upgradable ((old protobuf-enum) (new protobuf-enum)
68                                 &optional old-parent new-parent)
69   (declare (ignore old-parent new-parent))
70   ;; No need to check that the names are equal, our caller did that already
71   (loop for old-val in (proto-values old)
72         as new-val = (find (proto-name old-val) (proto-values new)
73                            :key #'proto-name :test #'string=)
74         always (and new-val (protobuf-upgradable old-val new-val old new))))
75
76 (defmethod protobuf-upgradable ((old protobuf-enum-value) (new protobuf-enum-value)
77                                 &optional old-enum new-enum)
78   (declare (ignore new-enum))
79   ;; No need to check that the names are equal, our caller did that already
80   ;; Do they have the same index?
81   (upgrade-warn (= (proto-index old) (proto-index new))
82                 "Enum index for '~A' changed from ~D to ~D"
83                 (format nil "~A.~A" (proto-name old-enum) (proto-name old))))
84
85
86 (defmethod protobuf-upgradable ((old protobuf-message) (new protobuf-message)
87                                 &optional old-parent new-parent)
88   (declare (ignore old-parent new-parent))
89   ;; No need to check that the names are equal, our caller did that already
90   (and
91    ;; Is every enum in 'old' upgradable to an enum in 'new'?
92    (loop for old-enum in (proto-enums old)
93          as new-enum = (find (proto-name old-enum) (proto-enums new)
94                              :key #'proto-name :test #'string=)
95          always (and new-enum (protobuf-upgradable old-enum new-enum old new)))
96    ;; Is every message in 'old' upgradable to a message in 'new'?
97    (loop for old-msg in (proto-messages old)
98          as new-msg = (find (proto-name old-msg) (proto-messages new)
99                             :key #'proto-name :test #'string=)
100          always (and new-msg (protobuf-upgradable old-msg new-msg old new)))
101    ;; Is every required field in 'old' upgradable to a field in 'new'?
102    ;; (Optional fields are safe to remove)
103    (loop for old-fld in (proto-fields old)
104          as new-fld = (find (proto-name old-fld) (proto-fields new)
105                             :key #'proto-name :test #'string=)
106          always (if new-fld
107                   (protobuf-upgradable old-fld new-fld old new)
108                   ;; If there's no new field, the old one must not be required
109                   (or (member (proto-required old-fld) '(:optional :repeated))
110                       (push (format nil "Old field '~A.~A' was required, and is now missing"
111                                     (proto-name old) (proto-name old-fld))
112                             *upgrade-warnings*))))))
113
114 (defmethod protobuf-upgradable ((old protobuf-field) (new protobuf-field)
115                                 &optional old-message new-message)
116   (flet ((arity-upgradable (old-arity new-arity)
117            (or (eq old-arity new-arity)
118                (not (eq new-arity :required))
119                ;; Optional fields and extensions are compatible
120                (and (eq old-arity :optional)
121                     (index-within-extensions-p (proto-index new) new-message))
122                (and (eq new-arity :optional)
123                     (index-within-extensions-p (proto-index old) old-message))))
124          (type-upgradable (old-type new-type)
125            ;;--- Handle conversions between embedded messages and bytes
126            (or
127             (string= old-type new-type)
128             ;; These varint types are all compatible
129             (and (member old-type '("int32" "uint32" "int64" "uint64" "bool") :test #'string=)
130                  (member new-type '("int32" "uint32" "int64" "uint64" "bool") :test #'string=))
131             ;; The two signed integer types are compatible
132             (and (member old-type '("sint32" "sint64") :test #'string=)
133                  (member new-type '("sint32" "sint64") :test #'string=))
134             ;; Fixed integers are compatible with each other
135             (and (member old-type '("fixed32" "sfixed32") :test #'string=)
136                  (member new-type '("fixed32" "sfixed32") :test #'string=))
137             (and (member old-type '("fixed64" "sfixed64") :test #'string=)
138                  (member new-type '("fixed64" "sfixed64") :test #'string=))
139             ;; Strings and bytes are compatible, assuming UTF-8 encoding
140             (and (member old-type '("string" "bytes") :test #'string=)
141                  (member new-type '("string" "bytes") :test #'string=))))
142          (default-upgradable (old-default new-default)
143            (declare (ignore old-default new-default))
144            t))
145     ;; No need to check that the names are equal, our caller did that already
146     (and
147      ;; Do they have the same index?
148      (upgrade-warn (= (proto-index old) (proto-index new))
149                    "Field index for '~A' changed from ~D to ~D"
150                    (format nil "~A.~A" (proto-name old-message) (proto-name old)))
151      ;; Are the arity and type upgradable?
152      (upgrade-warn (arity-upgradable (proto-required old) (proto-required new))
153                    "Arity of ~A, ~S, is not upgradable to ~S"
154                    (format nil  "~A.~A" (proto-name old-message) (proto-name old)))
155      (upgrade-warn (type-upgradable (proto-type old) (proto-type new))
156                    "Type of '~A', ~A, is not upgradable to ~A"
157                    (format nil  "~A.~A" (proto-name old-message) (proto-name old))))))
158
159
160 (defmethod protobuf-upgradable ((old protobuf-service) (new protobuf-service)
161                                 &optional old-parent new-parent)
162   (declare (ignore old-parent new-parent))
163   ;; No need to check that the names are equal, our caller did that already
164   ;; Is every method in 'old' upgradable to a method in 'new'?
165   (loop for old-method in (proto-methods old)
166         as new-method = (find (proto-name old-method) (proto-methods new)
167                               :key #'proto-name :test #'string=)
168         always (and new-method (protobuf-upgradable old-method new-method old new))))
169
170 (defmethod protobuf-upgradable ((old protobuf-method) (new protobuf-method)
171                                 &optional old-service new-service)
172   (declare (ignore new-service))
173   ;; No need to check that the names are equal, our caller did that already
174   (and
175    ;; Are their inputs and outputs the same?
176    (upgrade-warn (string= (proto-input-name old) (proto-input-name new))
177                  "Input type for ~A, ~A, is not upgradable to ~A"
178                  (format nil  "~A.~A" (proto-name old-service) (proto-name old)))
179    (upgrade-warn (string= (proto-output-name old) (proto-output-name new))
180                  "Output type for ~A, ~A, is not upgradable to ~A"
181                  (format nil  "~A.~A" (proto-name old-service) (proto-name old)))))
182
183 \f
184 ;;; Are two protobufs equal?
185
186 ;; This is useful for testing purposes, but not much else
187 (defgeneric protobufs-equal (proto1 proto2)
188   (:documentation
189    "Returns true if and only if the two Protobufs schemas are equal."))
190
191 ;; These methods are pretty similar to the 'protobuf-upgradable' methods above
192 (defmethod protobufs-equal ((proto1 protobuf) (proto2 protobuf))
193   (and
194    (eql    (proto-class proto1) (proto-class proto2))
195    (equalp (proto-name proto1) (proto-name proto2))
196    (equalp (proto-syntax proto1) (proto-syntax proto2))
197    (equalp (proto-package proto1) (proto-package proto2))
198    (equalp (proto-lisp-package proto1) (proto-lisp-package proto2))
199    (equalp (proto-imports proto1) (proto-imports proto2))
200    (= (length (proto-options proto1)) (length (proto-options proto2)))
201    (loop for option1 in (proto-options proto1)
202          as option2 = (find (proto-name option1) (proto-options proto2)
203                             :key #'proto-name :test #'string=)
204          always (and option2 (protobufs-equal option1 option2)))
205    (= (length (proto-enums proto1)) (length (proto-enums proto2)))
206    (loop for enum1 in (proto-enums proto1)
207          as enum2 = (find (proto-name enum1) (proto-enums proto2)
208                           :key #'proto-name :test #'string=)
209          always (and enum2 (protobufs-equal enum1 enum2)))
210    (= (length (proto-messages proto1)) (length (proto-messages proto2)))
211    (loop for msg1 in (proto-messages proto1)
212          as msg2 = (find-if #'(lambda (msg2)
213                                 (and (string= (proto-name msg1) (proto-name msg2))
214                                      (eql (proto-message-type msg1) (proto-message-type msg2))))
215                             (proto-messages proto2))
216          always (and msg2 (protobufs-equal msg1 msg2)))
217    (= (length (proto-services proto1)) (length (proto-services proto2)))
218    (loop for svc1 in (proto-services proto1)
219          as svc2 = (find (proto-name svc1) (proto-services proto2)
220                          :key #'proto-name :test #'string=)
221          always (and svc2 (protobufs-equal svc1 svc2)))))
222
223 (defmethod protobufs-equal ((option1 protobuf-option) (option2 protobuf-option))
224   (and
225    (string= (proto-name option1) (proto-name option2))
226    (equalp  (proto-value option1) (proto-value option2))
227    (equalp  (proto-type option1) (proto-type option2))))
228
229 (defmethod protobufs-equal ((enum1 protobuf-enum) (enum2 protobuf-enum))
230   (and
231    (eql    (proto-class enum1) (proto-class enum2))
232    (equalp (proto-name enum1) (proto-name enum2))
233    (equalp (proto-alias-for enum1) (proto-alias-for enum2))
234    (= (length (proto-options enum1)) (length (proto-options enum2)))
235    (loop for option1 in (proto-options enum1)
236          as option2 = (find (proto-name option1) (proto-options enum2)
237                             :key #'proto-name :test #'string=)
238          always (and option2 (protobufs-equal option1 option2)))
239    (= (length (proto-values enum1)) (length (proto-values enum2)))
240    (loop for val1 in (proto-values enum1)
241          as val2 = (find (proto-name val1) (proto-values enum2)
242                          :key #'proto-name :test #'string=)
243          always (and val2 (protobufs-equal val1 val2)))))
244
245 (defmethod protobufs-equal ((value1 protobuf-enum-value) (value2 protobuf-enum-value))
246   (and 
247    (eql    (proto-class value1) (proto-class value2))
248    (equalp (proto-name value1) (proto-name value2))
249    (eql    (proto-index value1) (proto-index value2))
250    (equalp (proto-value value1) (proto-value value2))))
251
252 (defmethod protobufs-equal ((message1 protobuf-message) (message2 protobuf-message))
253   (and
254    (eql    (proto-class message1) (proto-class message2))
255    (equalp (proto-name message1) (proto-name message2))
256    (equalp (proto-alias-for message1) (proto-alias-for message2))
257    (eql    (proto-message-type message1) (proto-message-type message2))
258    (= (length (proto-options message1)) (length (proto-options message2)))
259    (loop for option1 in (proto-options message1)
260          as option2 = (find (proto-name option1) (proto-options message2)
261                             :key #'proto-name :test #'string=)
262          always (and option2 (protobufs-equal option1 option2)))
263    (= (length (proto-enums message1)) (length (proto-enums message2)))
264    (loop for enum1 in (proto-enums message1)
265          as enum2 = (find (proto-name enum1) (proto-enums message2)
266                           :key #'proto-name :test #'string=)
267          always (and enum2 (protobufs-equal enum1 enum2)))
268    (= (length (proto-messages message1)) (length (proto-messages message2)))
269    (loop for msg1 in (proto-messages message1)
270          as msg2 = (find-if #'(lambda (msg2)
271                                 (and (string= (proto-name msg1) (proto-name msg2))
272                                      (eql (proto-message-type msg1) (proto-message-type msg2))))
273                             (proto-messages message2))
274          always (and msg2 (protobufs-equal msg1 msg2)))
275    (= (length (proto-fields message1)) (length (proto-fields message2)))
276    (loop for fld1 in (proto-fields message1)
277          as fld2 = (find (proto-name fld1) (proto-fields message2)
278                          :key #'proto-name :test #'string=)
279          always (and fld2 (protobufs-equal fld1 fld2)))
280    (= (length (proto-extensions message1)) (length (proto-extensions message2)))
281    (loop for ext1 in (proto-extensions message1)
282          for ext2 in (proto-extensions message2)
283          always (protobufs-equal ext1 ext2))))
284
285 (defmethod protobufs-equal ((field1 protobuf-field) (field2 protobuf-field))
286   (and
287    (eql    (proto-class field1) (proto-class field2))
288    (equalp (proto-name field1) (proto-name field2))
289    (equalp (proto-type field1) (proto-type field2))
290    (eql    (proto-required field1) (proto-required field2))
291    (eql    (proto-value field1) (proto-value field2))
292    (eql    (proto-index field1) (proto-index field2))
293    (eql    (proto-reader field1) (proto-reader field2))
294    (eql    (proto-writer field1) (proto-writer field2))
295    (equalp (proto-default field1) (proto-default field2))
296    (eql    (proto-packed field1) (proto-packed field2))
297    (eql    (proto-message-type field1) (proto-message-type field2))
298    (= (length (proto-options field1)) (length (proto-options field2)))
299    (loop for option1 in (proto-options field1)
300          as option2 = (find (proto-name option1) (proto-options field2)
301                             :key #'proto-name :test #'string=)
302          always (and option2 (protobufs-equal option1 option2)))))
303
304 (defmethod protobufs-equal ((extension1 protobuf-extension) (extension2 protobuf-extension))
305   (and
306    (eql (proto-extension-from extension1) (proto-extension-from extension2))
307    (eql (proto-extension-to extension1) (proto-extension-to extension2))))
308
309 (defmethod protobufs-equal ((service1 protobuf-service) (service2 protobuf-service))
310   (and
311    (eql    (proto-class service1) (proto-class service2))
312    (equalp (proto-name service1) (proto-name service2))
313    (= (length (proto-options service1)) (length (proto-options service2)))
314    (loop for option1 in (proto-options service1)
315          as option2 = (find (proto-name option1) (proto-options service2)
316                             :key #'proto-name :test #'string=)
317          always (and option2 (protobufs-equal option1 option2)))
318    (= (length (proto-methods service1)) (length (proto-methods service2)))
319    (loop for method1 in (proto-methods service1)
320          as method2 = (find (proto-name method1) (proto-methods service2)
321                             :key #'proto-name :test #'string=)
322          always (and method2 (protobufs-equal method1 method2)))))
323
324 (defmethod protobufs-equal ((method1 protobuf-method) (method2 protobuf-method))
325   (and
326    (eql    (proto-class method1) (proto-class method2))
327    (equalp (proto-name method1) (proto-name method2))
328    (eql    (proto-input-type method1) (proto-input-type method2))
329    (eql    (proto-output-type method1) (proto-output-type method2))
330    (equalp (proto-input-name method1) (proto-input-name method2))
331    (equalp (proto-output-name method1) (proto-output-name method2))
332    (= (length (proto-options method1)) (length (proto-options method2)))
333    (loop for option1 in (proto-options method1)
334          as option2 = (find (proto-name option1) (proto-options method2)
335                             :key #'proto-name :test #'string=)
336          always (and option2 (protobufs-equal option1 option2)))))