1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE ;;;
5 ;;; Copyright (c) 2012 Google, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package "PROTO-IMPL")
14 ;;; Protocol buffer generation from ordinary CLOS classes
16 ;; Controls whether or not to use ':alias-for' for the Protobuf generated
17 ;; for an existing Lisp class
18 ;; The default is presently true because, at least initially, we'll be using
19 ;; the generated Protobufs code in the Lisp world that includes that classes
20 ;; from which the code was generated
21 (defvar *alias-existing-classes* t)
23 ;; Doing this can't really work perfectly, there's not enough information
24 ;; - How do we decide if there's an ownership hierarchy that should produce embedded messages?
25 ;; - How do we decide if there are volatile slots that should not be included in the message?
26 (defun write-schema-for-classes (classes
27 &key (stream *standard-output*) (type :proto)
28 name package lisp-package install
29 slot-filter type-filter enum-filter value-filter
30 (alias-existing-classes *alias-existing-classes*))
31 "Given a set of CLOS classes, generates a Protobufs schema for the classes
32 and pretty prints the schema to the stream.
33 The return value is the schema."
34 (let ((schema (generate-schema-for-classes classes
37 :lisp-package (or lisp-package package)
39 :slot-filter slot-filter
40 :type-filter type-filter
41 :enum-filter enum-filter
42 :value-filter value-filter
43 :alias-existing-classes alias-existing-classes)))
45 (write-schema schema :stream stream :type type)
49 (defun generate-schema-for-classes (classes
50 &key name package lisp-package install
51 slot-filter type-filter enum-filter value-filter
52 (alias-existing-classes *alias-existing-classes*))
53 "Given a set of CLOS classes, generates a Protobufs schema for the classes.
54 The return value is the schema."
55 (let* ((*alias-existing-classes* alias-existing-classes)
56 (package (and package (if (stringp package) package (string-downcase (string package)))))
57 (lisp-pkg (string (or lisp-package package)))
58 (schema (make-instance 'protobuf-schema
61 :lisp-package lisp-pkg
64 (*protobuf-package* (or (find-proto-package lisp-pkg) *package*))
65 (messages (mapcar #'(lambda (c)
66 (class-to-protobuf-message c schema
67 :slot-filter slot-filter
68 :type-filter type-filter
69 :enum-filter enum-filter
70 :value-filter value-filter))
72 (setf (proto-messages schema) messages)
74 (record-protobuf schema)
75 (with-collectors ((messages collect-message))
76 (labels ((collect-messages (message)
77 (collect-message message)
78 (map () #'collect-messages (proto-messages message))))
79 (map () #'collect-messages (proto-messages schema)))
80 (map () #'record-protobuf messages)))
84 (defun class-to-protobuf-message (class schema
85 &key slot-filter type-filter enum-filter value-filter)
86 "Given a CLOS class, return a Protobufs model object for it."
87 (let* ((class (let ((c (find-class class)))
88 (unless (class-finalized-p c)
89 (finalize-inheritance c)) ;so the rest of the MOP will work
91 (slots (class-slots class)))
92 (with-collectors ((enums collect-enum)
94 (fields collect-field))
97 (multiple-value-bind (field msg enum)
98 (slot-to-protobuf-field class s index slots
99 :slot-filter slot-filter
100 :type-filter type-filter
101 :enum-filter enum-filter
102 :value-filter value-filter)
108 (incf index 1) ;don't worry about the 19000-19999 restriction
109 (collect-field field))))
110 (let* ((cname (class-name class))
111 (pname (class-name->proto cname))
113 ;;--- Making the message this late means its children won't
114 ;;--- have the right qualified names
115 (make-instance 'protobuf-message
118 :qualified-name (make-qualified-name *protobuf* pname)
120 :alias-for (and *alias-existing-classes* cname)
121 :enums (delete-duplicates enums :key #'proto-name :test #'string=)
122 :messages (delete-duplicates msgs :key #'proto-name :test #'string=)
124 (*protobuf* message))
125 ;; Give every child a proper parent
126 (dolist (enum (proto-enums message))
127 (setf (proto-parent enum) message))
128 (dolist (msg (proto-messages message))
129 (setf (proto-parent msg) message))
130 (dolist (field (proto-fields message))
131 (setf (proto-parent field) message))
134 ;; Returns a field, (optionally) an inner message, and (optionally) an inner enum
135 (defun slot-to-protobuf-field (class slot index slots
136 &key slot-filter type-filter enum-filter value-filter)
137 "Given a CLOS slot, return a Protobufs model object for it."
138 (when (or (null slot-filter)
139 (funcall slot-filter slot slots))
140 (multiple-value-bind (expanded-type unexpanded-type)
141 (find-slot-definition-type class slot)
142 (multiple-value-bind (type pclass packed enums)
143 (clos-type-to-protobuf-type expanded-type type-filter enum-filter)
144 (multiple-value-bind (reqd vectorp)
145 (clos-type-to-protobuf-required (find-slot-definition-type class slot) type-filter)
146 (let* ((ename (and enums
147 (if (and unexpanded-type (symbolp unexpanded-type))
148 (symbol-name unexpanded-type)
149 (format nil "~A-~A" 'enum (slot-definition-name slot)))))
151 (if (and unexpanded-type (symbolp unexpanded-type))
153 (intern ename (symbol-package (slot-definition-name slot))))))
155 (let* ((names (mapcar #'enum-name->proto enums))
156 (prefix (and (> (length names) 1)
157 (subseq (first names)
158 0 (mismatch (first names) (car (last names)))))))
159 (when (and prefix (> (length prefix) 2)
160 (every #'(lambda (name) (starts-with name prefix)) names))
161 (setq names (mapcar #'(lambda (name) (subseq name (length prefix))) names)))
162 (unless (and unexpanded-type (symbolp unexpanded-type))
163 #+ignore ;this happens constantly, the warning is not useful
164 (protobufs-warn "Use DEFTYPE to define a MEMBER type instead of directly using ~S"
166 (let* ((pname (class-name->proto ename))
168 (make-instance 'protobuf-enum
171 :qualified-name (make-qualified-name *protobuf* pname)
174 (loop for name in names
177 collect (make-instance 'protobuf-enum-value
179 :qualified-name (make-qualified-name enum name)
183 (setf (proto-values enum) values)
185 (default (if (slot-definition-initfunction slot)
186 (clos-init-to-protobuf-default
187 (slot-definition-initform slot) expanded-type value-filter)
188 (if (eq reqd :repeated)
189 (if vectorp $empty-vector $empty-list)
191 (field (make-instance 'protobuf-field
192 :name (slot-name->proto (slot-definition-name slot))
193 :type (if enum (class-name->proto ename) type)
194 :class (if enum etype pclass)
197 :value (slot-definition-name slot)
198 :reader (let ((reader (find-slot-definition-reader class slot)))
199 ;; Only use the reader if it is "interesting"
200 (unless (string= (symbol-name reader)
202 (class-name class) (slot-definition-name slot)))
206 (values field nil enum)))))))
208 (defun list-of-list-of ()
209 (let ((list-of-package (find-package 'list-of)))
210 (and list-of-package (find-symbol (string 'list-of) list-of-package))))
212 (defun find-slot-definition-type (class slotd)
213 "Given a class and a slot descriptor, find the \"best\" type definition for the slot."
214 (let* ((slot-name (slot-definition-name slotd))
215 (direct-slotd (some #'(lambda (c)
216 (find slot-name (class-direct-slots c) :key #'slot-definition-name))
217 (class-precedence-list class))))
219 ;; The direct slotd will have an unexpanded definition
220 ;; Prefer it for 'list-of' so we can get the base type
221 (let ((type (slot-definition-type direct-slotd)))
222 (values (if (and (listp type)
223 (or (member (car type) '(list-of vector-of))
224 (let ((list-of-list-of (list-of-list-of)))
225 (and list-of-list-of (eq (car type) list-of-list-of)))))
227 (slot-definition-type slotd))
230 (when (and (listp type)
232 (member 'null (cdr type)))
233 (find-if-not #'(lambda (s) (eq s 'null)) (cdr type))))))
234 (values (slot-definition-type slotd) nil))))
236 (defun find-slot-definition-reader (class slotd)
237 "Given a class and a slot descriptor, find the name of a reader method for the slot."
238 (let* ((slot-name (slot-definition-name slotd))
239 (direct-slotd (some #'(lambda (c)
240 (find slot-name (class-direct-slots c) :key #'slot-definition-name))
241 (class-precedence-list class))))
242 (and direct-slotd (first (slot-definition-readers direct-slotd)))))
244 (defun satisfies-list-of-p (type)
246 (eq (car type) 'satisfies)
249 (let ((function (cadr type)))
250 (and (symbolp function)
251 (string= "LIST-OF" (package-name (symbol-package function)))
252 (let ((name (symbol-name function)))
253 (and (<= #.(length "LIST-OF-_-P") (length name))
254 (starts-with name "LIST-OF-")
255 (ends-with name "-P")
256 (let* ((typestring (subseq name #.(length "LIST-OF-") (- (length name) 2)))
258 (with-standard-io-syntax
259 (let ((*package* (find-package :cl)))
260 (read-from-string typestring))))))
261 (and (typep type 'symbol) type))))))))
263 (defun clos-type-to-protobuf-type (type &optional type-filter enum-filter)
264 "Given a Lisp type, returns a Protobuf type, a class or primitive type,
265 whether or not to pack the field, and (optionally) a set of enum values."
266 (let ((type (if type-filter (funcall type-filter type) type))
267 (list-of-list-of (list-of-list-of)))
270 (destructuring-bind (head &rest tail) type
273 (when (or (> (length tail) 2)
274 (not (member 'null tail)))
275 (protobufs-warn "The OR type ~S is too complicated, proceeding anyway" type))
276 (if (eq (first tail) 'null)
277 (clos-type-to-protobuf-type (second tail))
278 (clos-type-to-protobuf-type (first tail))))
280 ;; Special knowledge of 'list-of:list-of', which uses (and list (satisfies list-of::FOO-p))
281 (let ((satisfies-list-of
282 (and list-of-list-of (find-if #'satisfies-list-of-p tail))))
283 (if satisfies-list-of
284 (multiple-value-bind (type class)
285 (lisp-type-to-protobuf-type satisfies-list-of)
286 (values type class (packed-type-p class)))
288 (remove-if #'(lambda (x) (and (listp x) (eq (car x) 'satisfies))) tail)))
289 (when (> (length new-tail) 1)
290 (protobufs-warn "The AND type ~S is too complicated, proceeding anyway" type))
291 (lisp-type-to-protobuf-type (first tail))))))
292 ((member) ;maybe generate an enum type
293 (if (or (equal type '(member t nil))
294 (equal type '(member nil t)))
295 (values "bool" :bool)
296 (let ((values (if enum-filter (funcall enum-filter tail) tail)))
297 (cond ((every #'(lambda (x)
298 (or (null x) (characterp x) (stringp x))) values)
299 (values "string" :string))
300 ((every #'(lambda (x)
301 (or (null x) (and (integerp x) (>= x 0)))) values)
302 (values "uint32" :uint32))
303 ((every #'(lambda (x)
304 (or (null x) (integerp x))) values)
305 (values "int32" :int32))
306 ((every #'(lambda (x) (symbolp x)) values)
307 (let ((values (remove-if #'null values)))
308 (values (class-name->proto (format nil "~A" type))
310 nil ;don't pack enums
311 (if enum-filter (funcall enum-filter values) values))))
313 (error "The MEMBER type ~S is too complicated" type))))))
315 (multiple-value-bind (type class)
316 (lisp-type-to-protobuf-type (first tail))
317 (values type class (packed-type-p class))))
319 (let ((lo (or (first tail) '*))
320 (hi (or (second tail) '*)))
321 (if (or (eq lo '*) (< lo 0))
323 (values "int64" :int64)
324 (if (<= (integer-length hi) 32)
325 (values "int32" :int32)
326 (values "int64" :int64)))
328 (values "uint64" :uint64)
329 (if (<= (integer-length hi) 32)
330 (values "uint32" :uint32)
331 (values "uint64" :uint64))))))
333 (let ((len (first tail)))
335 (values "int32" :int32)
336 (values "int64" :int64))))
338 (let ((len (first tail)))
340 (values "uint32" :uint32)
341 (values "uint64" :uint64))))
342 ((float single-float double-float)
343 (lisp-type-to-protobuf-type head))
345 (if (eq head list-of-list-of)
346 (multiple-value-bind (type class)
347 (lisp-type-to-protobuf-type (first tail))
348 (values type class (packed-type-p class)))
349 (lisp-type-to-protobuf-type type)))))
350 (lisp-type-to-protobuf-type type)))))
352 (defun lisp-type-to-protobuf-type (type)
354 ((int32) (values "int32" :int32))
355 ((int64) (values "int64" :int64))
356 ((uint32) (values "uint32" :uint32))
357 ((uint64) (values "uint64" :uint64))
358 ((sint32) (values "sint32" :sint32))
359 ((sint64) (values "sint64" :sint64))
360 ((fixed32) (values "fixed32" :fixed32))
361 ((fixed64) (values "fixed64" :fixed64))
362 ((sfixed32) (values "sfixed32" :sfixed32))
363 ((sfixed64) (values "sfixed64" :sfixed64))
364 ((integer) (values "int64" :int64))
365 ((single-float float)
366 (values "float" :float))
368 (values "double" :double))
370 (values "bool" :bool))
372 (values "string" :symbol))
374 (cond ((ignore-errors
375 (or (eql type 'symbol)
376 (subtypep type '(or string character))))
377 (values "string" :string))
379 (subtypep type 'byte-vector))
380 (values "bytes" :bytes))
382 (values (class-name->proto type) type))))))
384 (defun packed-type-p (type)
385 "Returns true if the given Protobufs type can use a packed field."
386 (not (null (member type '(:int32 :int64 :uint32 :uint64 :sint32 :sint64
387 :fixed32 :fixed64 :sfixed32 :sfixed64
388 :bool :float :double)))))
390 (defun clos-type-to-protobuf-required (type &optional type-filter)
391 "Given a Lisp type, returns a \"cardinality\": :required, :optional or :repeated.
392 If the sceond returned value is true, it's a repeated field that should use a vector."
393 (let ((type (if type-filter (funcall type-filter type) type))
394 (list-of-list-of (list-of-list-of)))
396 (destructuring-bind (head &rest tail) type
399 (let ((optional (member 'null tail))
400 (repeated (find-if #'(lambda (r)
401 (eq (clos-type-to-protobuf-required r) :repeated)) tail)))
403 (clos-type-to-protobuf-required repeated)
404 (values (if optional :optional :required) nil))))
406 (cond ((and (subtypep type 'list)
407 (not (subtypep type 'null)))
408 (values :repeated nil))
409 ((subtypep type '(vector-of t))
410 (values :repeated t))
412 (values :required nil))))
414 (if (or (equal type '(member t nil))
415 (equal type '(member nil t)))
416 (values :required nil)
417 (values (if (member nil tail) :optional :required) nil)))
419 (values :repeated nil))
421 (values :repeated t))
423 (if (eq head list-of-list-of)
424 (values :repeated nil)
425 (values :required nil)))))
426 (values :required nil))))
428 (defun clos-init-to-protobuf-default (value type &optional value-filter)
429 "Given an initform and a Lisp type, returns a plausible default value.
430 Don't call this if the default is empty, because that will confuse 'nil' with 'unbound'."
431 (let ((value (if value-filter (funcall value-filter value) value)))
432 (and (constantp value)
433 (ignore-errors (typep value type))
436 (defun protobuf-default-to-clos-init (default type)
437 "Given a Protobufs type and default, return a CLOS initform value.
438 Don't call this if the default is empty, because that will confuse 'nil' with 'unbound'."
439 (cond ((ignore-errors (typep default type))
442 (cond ((eq type :bool)
443 (boolean-true-p default))
444 ;; If we've got a symbol, it must be to initialize an enum type
445 ;; whose values are represented by keywords in Lisp
446 (t (kintern (symbol-name default)))))
448 (cond ((eq type :bool)
449 (boolean-true-p default))
450 ((member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
451 :fixed32 :sfixed32 :fixed64 :sfixed64))
452 (let ((default (read-from-string default)))
453 (and (integerp default) default)))
454 ((member type '(:float :double))
455 (let ((default (read-from-string default)))
456 (and (floatp default) default)))
459 (defun boolean-true-p (x)
460 "Returns t or nil given a value that might be a boolean."
463 (integer (not (eql x 0)))
464 (character (char-equal x #\t))
465 (string (or (string-equal x "true")
466 (string-equal x "yes")
468 (string-equal x "1")))
469 (symbol (string-equal (string x) "true"))))