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