]> asedeno.scripts.mit.edu Git - cl-protobufs.git/blob - upgradable.lisp
Get some stuff working better for Stubby purposes.
[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 schema-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 schema-upgradable ((old protobuf-schema) (new protobuf-schema)
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 (schema-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 (schema-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 (schema-upgradable old-svc new-svc old new))))
64     (values (null *upgrade-warnings*) (nreverse *upgrade-warnings*))))
65
66
67 (defmethod schema-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 (schema-upgradable old-val new-val old new))))
75
76 (defmethod schema-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 schema-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 (schema-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 (schema-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                   (schema-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 schema-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 schema-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 (schema-upgradable old-method new-method old new))))
169
170 (defmethod schema-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 protobuf schemas equal?
185
186 ;; This is useful for testing purposes, but not much else
187 (defgeneric schemas-equal (schema1 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 'schema-upgradable' methods above
192 (defmethod schemas-equal ((schema1 protobuf-schema) (schema2 protobuf-schema))
193   (and
194    ;; If the name(s) are null, don't worry about them
195    (or (null (proto-class schema1)) (null (proto-class schema2))
196        (eql (proto-class schema1) (proto-class schema2)))
197    (or (null (proto-name schema1)) (null (proto-name schema2))
198         (equalp (proto-name schema1) (proto-name schema2)))
199    (equalp (proto-syntax schema1) (proto-syntax schema2))
200    (equalp (proto-package schema1) (proto-package schema2))
201    (equalp (proto-lisp-package schema1) (proto-lisp-package schema2))
202    (equalp (proto-imports schema1) (proto-imports schema2))
203    (= (length (proto-options schema1)) (length (proto-options schema2)))
204    (loop for option1 in (proto-options schema1)
205          as option2 = (find (proto-name option1) (proto-options schema2)
206                             :key #'proto-name :test #'string=)
207          always (and option2 (schemas-equal option1 option2)))
208    (= (length (proto-enums schema1)) (length (proto-enums schema2)))
209    (loop for enum1 in (proto-enums schema1)
210          as enum2 = (find (proto-name enum1) (proto-enums schema2)
211                           :key #'proto-name :test #'string=)
212          always (and enum2 (schemas-equal enum1 enum2)))
213    (= (length (proto-messages schema1)) (length (proto-messages schema2)))
214    (loop for msg1 in (proto-messages schema1)
215          as msg2 = (find-if #'(lambda (msg2)
216                                 (and (string= (proto-name msg1) (proto-name msg2))
217                                      (eql (proto-message-type msg1) (proto-message-type msg2))))
218                             (proto-messages schema2))
219          always (and msg2 (schemas-equal msg1 msg2)))
220    (= (length (proto-services schema1)) (length (proto-services schema2)))
221    (loop for svc1 in (proto-services schema1)
222          as svc2 = (find (proto-name svc1) (proto-services schema2)
223                          :key #'proto-name :test #'string=)
224          always (and svc2 (schemas-equal svc1 svc2)))))
225
226 (defmethod schemas-equal ((option1 protobuf-option) (option2 protobuf-option))
227   (and
228    (string= (proto-name option1) (proto-name option2))
229    (equalp  (proto-value option1) (proto-value option2))
230    (equalp  (proto-type option1) (proto-type option2))))
231
232 (defmethod schemas-equal ((enum1 protobuf-enum) (enum2 protobuf-enum))
233   (and
234    (eql    (proto-class enum1) (proto-class enum2))
235    (equalp (proto-name enum1) (proto-name enum2))
236    (equalp (proto-alias-for enum1) (proto-alias-for enum2))
237    (= (length (proto-options enum1)) (length (proto-options enum2)))
238    (loop for option1 in (proto-options enum1)
239          as option2 = (find (proto-name option1) (proto-options enum2)
240                             :key #'proto-name :test #'string=)
241          always (and option2 (schemas-equal option1 option2)))
242    (= (length (proto-values enum1)) (length (proto-values enum2)))
243    (loop for val1 in (proto-values enum1)
244          as val2 = (find (proto-name val1) (proto-values enum2)
245                          :key #'proto-name :test #'string=)
246          always (and val2 (schemas-equal val1 val2)))))
247
248 (defmethod schemas-equal ((value1 protobuf-enum-value) (value2 protobuf-enum-value))
249   (and 
250    (eql    (proto-class value1) (proto-class value2))
251    (equalp (proto-name value1) (proto-name value2))
252    (eql    (proto-index value1) (proto-index value2))
253    (equalp (proto-value value1) (proto-value value2))))
254
255 (defmethod schemas-equal ((message1 protobuf-message) (message2 protobuf-message))
256   (and
257    (eql    (proto-class message1) (proto-class message2))
258    (equalp (proto-name message1) (proto-name message2))
259    (equalp (proto-alias-for message1) (proto-alias-for message2))
260    (eql    (proto-message-type message1) (proto-message-type message2))
261    (= (length (proto-options message1)) (length (proto-options message2)))
262    (loop for option1 in (proto-options message1)
263          as option2 = (find (proto-name option1) (proto-options message2)
264                             :key #'proto-name :test #'string=)
265          always (and option2 (schemas-equal option1 option2)))
266    (= (length (proto-enums message1)) (length (proto-enums message2)))
267    (loop for enum1 in (proto-enums message1)
268          as enum2 = (find (proto-name enum1) (proto-enums message2)
269                           :key #'proto-name :test #'string=)
270          always (and enum2 (schemas-equal enum1 enum2)))
271    (= (length (proto-messages message1)) (length (proto-messages message2)))
272    (loop for msg1 in (proto-messages message1)
273          as msg2 = (find-if #'(lambda (msg2)
274                                 (and (string= (proto-name msg1) (proto-name msg2))
275                                      (eql (proto-message-type msg1) (proto-message-type msg2))))
276                             (proto-messages message2))
277          always (and msg2 (schemas-equal msg1 msg2)))
278    (= (length (proto-fields message1)) (length (proto-fields message2)))
279    (loop for fld1 in (proto-fields message1)
280          as fld2 = (find (proto-name fld1) (proto-fields message2)
281                          :key #'proto-name :test #'string=)
282          always (and fld2 (schemas-equal fld1 fld2)))
283    (= (length (proto-extensions message1)) (length (proto-extensions message2)))
284    (loop for ext1 in (proto-extensions message1)
285          for ext2 in (proto-extensions message2)
286          always (schemas-equal ext1 ext2))))
287
288 (defmethod schemas-equal ((field1 protobuf-field) (field2 protobuf-field))
289   (and
290    (eql    (proto-class field1) (proto-class field2))
291    (equalp (proto-name field1) (proto-name field2))
292    (equalp (proto-type field1) (proto-type field2))
293    (eql    (proto-required field1) (proto-required field2))
294    (eql    (proto-value field1) (proto-value field2))
295    (eql    (proto-index field1) (proto-index field2))
296    (eql    (proto-reader field1) (proto-reader field2))
297    (eql    (proto-writer field1) (proto-writer field2))
298    (equalp (proto-default field1) (proto-default field2))
299    (eql    (proto-packed field1) (proto-packed field2))
300    (eql    (proto-message-type field1) (proto-message-type field2))
301    (= (length (proto-options field1)) (length (proto-options field2)))
302    (loop for option1 in (proto-options field1)
303          as option2 = (find (proto-name option1) (proto-options field2)
304                             :key #'proto-name :test #'string=)
305          always (and option2 (schemas-equal option1 option2)))))
306
307 (defmethod schemas-equal ((extension1 protobuf-extension) (extension2 protobuf-extension))
308   (and
309    (eql (proto-extension-from extension1) (proto-extension-from extension2))
310    (eql (proto-extension-to extension1) (proto-extension-to extension2))))
311
312 (defmethod schemas-equal ((service1 protobuf-service) (service2 protobuf-service))
313   (and
314    (eql    (proto-class service1) (proto-class service2))
315    (equalp (proto-name service1) (proto-name service2))
316    (= (length (proto-options service1)) (length (proto-options service2)))
317    (loop for option1 in (proto-options service1)
318          as option2 = (find (proto-name option1) (proto-options service2)
319                             :key #'proto-name :test #'string=)
320          always (and option2 (schemas-equal option1 option2)))
321    (= (length (proto-methods service1)) (length (proto-methods service2)))
322    (loop for method1 in (proto-methods service1)
323          as method2 = (find (proto-name method1) (proto-methods service2)
324                             :key #'proto-name :test #'string=)
325          always (and method2 (schemas-equal method1 method2)))))
326
327 (defmethod schemas-equal ((method1 protobuf-method) (method2 protobuf-method))
328   (and
329    (eql    (proto-class method1) (proto-class method2))
330    (equalp (proto-name method1) (proto-name method2))
331    (eql    (proto-input-type method1) (proto-input-type method2))
332    (eql    (proto-output-type method1) (proto-output-type method2))
333    (equalp (proto-input-name method1) (proto-input-name method2))
334    (equalp (proto-output-name method1) (proto-output-name method2))
335    (eql    (proto-index method1) (proto-index method2))
336    (= (length (proto-options method1)) (length (proto-options method2)))
337    (loop for option1 in (proto-options method1)
338          as option2 = (find (proto-name option1) (proto-options method2)
339                             :key #'proto-name :test #'string=)
340          always (and option2 (schemas-equal option1 option2)))))