1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Confidential and proprietary information of ITA Software, Inc. ;;;
5 ;;; Copyright (c) 2012 ITA Software, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package "PROTO-IMPL")
14 ;;; Protocol buffer defining macros
16 ;; Define a schema named 'type', corresponding to a .proto file of that name
17 (defmacro define-proto (type (&key name syntax package lisp-package import optimize options documentation)
18 &body messages &environment env)
19 "Define a schema named 'type', corresponding to a .proto file of that name.
20 'name' can be used to override the defaultly generated Protobufs name.
21 'syntax' and 'package' are as they would be in a .proto file.
22 'lisp-package' can be used to specify a Lisp package if it is different from
23 the Protobufs package given by 'package'.
24 'import' is a list of pathname strings to be imported.
25 'optimize' can be either :space (the default) or :speed; if it is :speed, the
26 serialization code will be much faster, but much less compact.
27 'options' is a property list, i.e., (\"key1\" \"val1\" \"key2\" \"val2\" ...).
29 The body consists of 'define-enum', 'define-message' or 'define-service' forms."
30 (let* ((name (or name (class-name->proto type)))
31 (package (and package (if (stringp package) package (string-downcase (string package)))))
32 (lisp-pkg (and lisp-package (if (stringp lisp-package) lisp-package (string lisp-package))))
33 (options (loop for (key val) on options by #'cddr
34 collect (make-instance 'protobuf-option
37 (imports (if (listp import) import (list import)))
38 (protobuf (make-instance 'protobuf
41 :syntax (or syntax "proto2")
43 :lisp-package (or lisp-pkg package)
46 (append options (list (make-instance 'protobuf-option
48 :value (if (eq optimize :speed) "SPEED" "CODE_SIZE")
51 :documentation documentation))
53 (*protobuf-package* (or (find-package lisp-pkg)
54 (find-package (string-upcase lisp-pkg))
56 (apply #'process-imports imports)
57 (with-collectors ((forms collect-form))
58 (dolist (msg messages)
59 (assert (and (listp msg)
60 (member (car msg) '(define-enum define-message define-extend define-service))) ()
61 "The body of ~S must be one of ~{~S~^ or ~}"
62 'define-proto '(define-enum define-message define-extend define-service))
63 ;; The macro-expander will return a form that consists
64 ;; of 'progn' followed by a symbol naming what we've expanded
65 ;; (define-enum, define-message, define-extend, define-service),
66 ;; followed by the Lisp model object created by the defining form,
67 ;; followed by other defining forms (e.g., deftype, defclass)
68 (destructuring-bind (&optional progn type model definers)
69 (macroexpand-1 msg env)
70 (assert (eq progn 'progn) ()
71 "The macroexpansion for ~S failed" msg)
72 (map () #'collect-form definers)
75 (setf (proto-enums protobuf) (nconc (proto-enums protobuf) (list model))))
76 ((define-message define-extend)
77 (setf (proto-parent model) protobuf)
78 (setf (proto-messages protobuf) (nconc (proto-messages protobuf) (list model)))
79 (when (eq (proto-message-type model) :extends)
80 (setf (proto-extenders protobuf) (nconc (proto-extenders protobuf) (list model)))))
82 (setf (proto-services protobuf) (nconc (proto-services protobuf) (list model)))))))
83 (let ((var (intern (format nil "*~A*" type) *protobuf-package*)))
87 (let* ((old-proto ,var)
88 (new-proto ,protobuf))
90 (multiple-value-bind (upgradable warnings)
91 (protobuf-upgradable old-proto new-proto)
93 (protobufs-warn "The old schema for ~S (~A) can't be safely upgraded; proceeding anyway"
95 (map () #'protobufs-warn warnings))))
97 ,@(when (eq optimize :speed)
98 (with-collectors ((messages collect-message))
99 (labels ((collect-messages (message)
100 (collect-message message)
101 (map () #'collect-messages (proto-messages message))))
102 (map () #'collect-messages (proto-messages protobuf)))
103 (append (mapcar #'generate-object-size messages)
104 (mapcar #'generate-serializer messages)
105 (mapcar #'generate-deserializer messages))))
108 ;; Define an enum type named 'type' and a Lisp 'deftype'
109 (defmacro define-enum (type (&key name conc-name alias-for options documentation)
111 "Define a Protobufs enum type and a Lisp 'deftype' named 'type'.
112 'name' can be used to override the defaultly generated Protobufs enum name.
113 'conc-name' will be used as the prefix to the Lisp enum names, if it's supplied.
114 If 'alias-for' is given, no Lisp 'deftype' will be defined. Instead, the enum
115 will be used as an alias for an enum type that already exists in Lisp.
116 'options' is a set of keyword/value pairs, both of which are strings.
118 The body consists of the enum values in the form 'name' or (name index)."
119 (let* ((name (or name (class-name->proto type)))
120 (options (loop for (key val) on options by #'cddr
121 collect (make-instance 'protobuf-option
125 (enum (make-instance 'protobuf-enum
130 :documentation documentation)))
131 (with-collectors ((vals collect-val)
132 (forms collect-form))
134 (let* ((idx (if (listp val) (second val) (incf index)))
135 (name (if (listp val) (first val) val))
136 (val-name (kintern (if conc-name (format nil "~A~A" conc-name name) (symbol-name name))))
137 (enum-name (if conc-name (format nil "~A~A" conc-name name) (symbol-name name)))
138 (enum-val (make-instance 'protobuf-enum-value
139 :name (enum-name->proto enum-name)
142 (collect-val val-name)
143 (setf (proto-values enum) (nconc (proto-values enum) (list enum-val)))))
145 ;; If we've got an alias, define a a type that is the subtype of
146 ;; the Lisp enum so that typep and subtypep work
147 (unless (eq type alias-for)
148 (collect-form `(deftype ,type () ',alias-for)))
149 ;; If no alias, define the Lisp enum type now
150 (collect-form `(deftype ,type () '(member ,@vals))))
156 ;; Define a message named 'name' and a Lisp 'defclass'
157 (defmacro define-message (type (&key name conc-name alias-for options documentation)
158 &body fields &environment env)
159 "Define a message named 'type' and a Lisp 'defclass'.
160 'name' can be used to override the defaultly generated Protobufs message name.
161 The body consists of fields, or 'define-enum' or 'define-message' forms.
162 'conc-name' will be used as the prefix to the Lisp slot accessors, if it's supplied.
163 If 'alias-for' is given, no Lisp class is defined. Instead, the message will be
164 used as an alias for a class that already exists in Lisp. This feature is intended
165 to be used to define messages that will be serialized from existing Lisp classes;
166 unless you get the slot names or readers exactly right for each field, it will be
167 the case that trying to (de)serialize into a Lisp object won't work.
168 'options' is a set of keyword/value pairs, both of which are strings.
170 Fields take the form (slot &key type name default reader)
171 'slot' can be either a symbol giving the field name, or a list whose
172 first element is the slot name and whose second element is the index.
173 'type' is the type of the slot.
174 'name' can be used to override the defaultly generated Protobufs field name.
175 'default' is the default value for the slot.
176 'reader' is a Lisp slot reader function to use to get the value, instead of
177 using 'slot-value'; this is often used when aliasing an existing class.
178 'writer' is a Lisp slot writer function to use to set the value."
179 (let* ((name (or name (class-name->proto type)))
180 (options (loop for (key val) on options by #'cddr
181 collect (make-instance 'protobuf-option
184 (message (make-instance 'protobuf-message
188 :conc-name (and conc-name (string conc-name))
190 :documentation documentation))
192 (*protobuf* message))
193 (with-collectors ((slots collect-slot)
194 (forms collect-form))
195 (dolist (field fields)
197 ((define-enum define-message define-extend define-extension define-group)
198 (destructuring-bind (&optional progn type model definers extra-field extra-slot)
199 (macroexpand-1 field env)
200 (assert (eq progn 'progn) ()
201 "The macroexpansion for ~S failed" field)
202 (map () #'collect-form definers)
205 (setf (proto-enums message) (nconc (proto-enums message) (list model))))
206 ((define-message define-extend)
207 (setf (proto-parent model) message)
208 (setf (proto-messages message) (nconc (proto-messages message) (list model)))
209 (when (eq (proto-message-type model) :extends)
210 (setf (proto-extenders message) (nconc (proto-extenders message) (list model)))))
212 (setf (proto-parent model) message)
213 (setf (proto-messages message) (nconc (proto-messages message) (list model)))
215 (collect-slot extra-slot))
216 (setf (proto-fields message) (nconc (proto-fields message) (list extra-field))))
218 (setf (proto-extensions message) (nconc (proto-extensions message) (list model)))))))
220 (multiple-value-bind (field slot idx)
221 (process-field field index :conc-name conc-name :alias-for alias-for)
222 (assert (not (find (proto-index field) (proto-fields message) :key #'proto-index)) ()
223 "The field ~S overlaps with another field in ~S"
224 (proto-value field) (proto-class message))
228 (setf (proto-fields message) (nconc (proto-fields message) (list field)))))))
230 ;; If we've got an alias, define a a type that is the subtype of
231 ;; the Lisp class that typep and subtypep work
232 (unless (or (eq type alias-for) (find-class type nil))
233 (collect-form `(deftype ,type () ',alias-for)))
234 ;; If no alias, define the class now
235 (collect-form `(defclass ,type () (,@slots)
236 ,@(and documentation `((:documentation ,documentation))))))
242 (defmacro define-extension (from to)
243 "Define an extension range within a message.
244 The \"body\" is the start and end of the range, both inclusive."
247 ,(make-instance 'protobuf-extension
249 :to (if (eq to 'max) #.(1- (ash 1 29)) to))
252 (defmacro define-extend (type (&key name options documentation)
253 &body fields &environment env)
254 "Define an extension to the message named 'type'.
255 'name' can be used to override the defaultly generated Protobufs message name.
256 The body consists only of fields.
257 'options' is a set of keyword/value pairs, both of which are strings.
259 Fields take the form (slot &key type name default reader)
260 'slot' can be either a symbol giving the field name, or a list whose
261 first element is the slot name and whose second element is the index.
262 'type' is the type of the slot.
263 'name' can be used to override the defaultly generated Protobufs field name.
264 'default' is the default value for the slot.
265 'reader' is a Lisp slot reader function to use to get the value, instead of
266 using 'slot-value'; this is often used when aliasing an existing class.
267 'writer' is a Lisp slot writer function to use to set the value."
268 (declare (ignore env))
269 (let* ((name (or name (class-name->proto type)))
270 (options (loop for (key val) on options by #'cddr
271 collect (make-instance 'protobuf-option
274 (message (find-message *protobuf* name))
275 (conc-name (and message (proto-conc-name message)))
276 (alias-for (and message (proto-alias-for message)))
277 (extends (and message
278 (make-instance 'protobuf-message
281 :parent (proto-parent message)
284 :enums (copy-list (proto-enums message))
285 :messages (copy-list (proto-messages message))
286 :fields (copy-list (proto-fields message))
287 :options (or options (copy-list (proto-options message)))
288 :message-type :extends ;this message is an extension
289 :documentation documentation)))
292 "There is no message named ~A to extend" name)
293 (assert (eq type (proto-class message)) ()
294 "The type ~S doesn't match the type of the message being extended ~S"
296 (with-collectors ((forms collect-form))
297 (dolist (field fields)
298 (assert (not (member (car field)
299 '(define-enum define-message define-extend define-extension define-group))) ()
300 "The body of ~S can only contain field definitions" 'define-extend)
301 (multiple-value-bind (field slot idx)
302 (process-field field index :conc-name conc-name :alias-for alias-for)
303 (assert (not (find (proto-index field) (proto-fields extends) :key #'proto-index)) ()
304 "The field ~S overlaps with another field in ~S"
305 (proto-value field) (proto-class extends))
306 (assert (index-within-extensions-p idx message) ()
307 "The index ~D is not in range for extending ~S"
308 idx (proto-class message))
311 (let* ((inits (cdr slot))
313 (stable (fintern "~A-VALUES" sname))
314 (stype (getf inits :type))
315 (reader (or (getf inits :accessor)
317 (intern (if conc-name (format nil "~A~A" conc-name sname) (symbol-name sname))
318 (symbol-package sname))))
319 (writer (or (getf inits :writer)
320 (intern (format nil "~A-~A" reader 'setter)
321 (symbol-package sname))))
322 (default (getf inits :initform)))
323 ;; For the extended slots, each slot gets its own table
324 ;; keyed by the object, which lets us avoid having a slot in each
325 ;; instance that holds a table keyed by the slot name
326 ;; Multiple 'define-extends' on the same class in the same image
327 ;; will result in harmless redefinitions, so squelch the warnings
328 (collect-form `(without-redefinition-warnings ()
329 (let ((,stable (make-hash-table :test #'eq :weak t)))
330 (defmethod ,reader ((object ,type))
331 (gethash object ,stable ,default))
332 (defmethod ,writer ((object ,type) value)
333 (declare (type ,stype value))
334 (setf (gethash object ,stable) value))
335 ;; For Python compatibility
336 (defmethod get-extension ((object ,type) (slot (eql ',sname)))
337 (values (gethash object ,stable ,default)))
338 (defmethod set-extension ((object ,type) (slot (eql ',sname)) value)
339 (setf (gethash object ,stable) value))
340 (defmethod has-extension ((object ,type) (slot (eql ',sname)))
341 (multiple-value-bind (value foundp)
342 (gethash object ,stable ,default)
343 (declare (ignore value))
345 (defmethod clear-extension ((object ,type) (slot (eql ',sname)))
346 (remhash object ,stable))
347 (defsetf ,reader ,writer))))
348 ;; This so that (de)serialization works
349 (setf (proto-reader field) reader
350 (proto-writer field) writer)))
351 (setf (proto-message-type field) :extends) ;this field is an extension
352 (setf (proto-fields extends) (nconc (proto-fields extends) (list field)))))
358 (defun index-within-extensions-p (index message)
359 (let ((extensions (proto-extensions message)))
360 (some #'(lambda (ext)
361 (and (i>= index (proto-extension-from ext))
362 (i<= index (proto-extension-to ext))))
365 (defmacro define-group (type (&key index arity name conc-name alias-for options documentation)
366 &body fields &environment env)
367 "Define a message named 'type' and a Lisp 'defclass', *and* a field named type.
368 This is deprecated in Protobufs, but if you have to use it, you must give
369 'index' as the field index and 'arity' of :required, :optional or :repeated.
370 'name' can be used to override the defaultly generated Protobufs message name.
371 The body consists of fields, or 'define-enum' or 'define-message' forms.
372 'conc-name' will be used as the prefix to the Lisp slot accessors, if it's supplied.
373 If 'alias-for' is given, no Lisp class is defined. Instead, the message will be
374 used as an alias for a class that already exists in Lisp. This feature is intended
375 to be used to define messages that will be serialized from existing Lisp classes;
376 unless you get the slot names or readers exactly right for each field, it will be
377 the case that trying to (de)serialize into a Lisp object won't work.
378 'options' is a set of keyword/value pairs, both of which are strings.
380 Fields take the form (slot &key type name default reader)
381 'slot' can be either a symbol giving the field name, or a list whose
382 first element is the slot name and whose second element is the index.
383 'type' is the type of the slot.
384 'name' can be used to override the defaultly generated Protobufs field name.
385 'default' is the default value for the slot.
386 'reader' is a Lisp slot reader function to use to get the value, instead of
387 using 'slot-value'; this is often used when aliasing an existing class.
388 'writer' is a Lisp slot writer function to use to set the value."
389 (check-type index integer)
390 (check-type arity (member :required :optional :repeated))
391 (let* ((slot (or (and name (proto->slot-name name)) type))
392 (name (or name (class-name->proto type)))
393 (options (loop for (key val) on options by #'cddr
394 collect (make-instance 'protobuf-option
397 (mslot (unless alias-for
398 `(,slot ,@(case arity
402 `(:type (or ,type null)
405 `(:type (list-of ,type)
407 :initarg ,(kintern (symbol-name slot)))))
408 (mfield (make-instance 'protobuf-field
409 :name (slot-name->proto slot)
413 ;; One of :required, :optional or :repeated
416 :message-type :group))
417 (message (make-instance 'protobuf-message
421 :conc-name (and conc-name (string conc-name))
423 :message-type :group ;this message is a group
424 :documentation documentation))
426 (*protobuf* message))
427 (with-collectors ((slots collect-slot)
428 (forms collect-form))
429 (dolist (field fields)
431 ((define-enum define-message define-extend define-extension define-group)
432 (destructuring-bind (&optional progn type model definers extra-field extra-slot)
433 (macroexpand-1 field env)
434 (assert (eq progn 'progn) ()
435 "The macroexpansion for ~S failed" field)
436 (map () #'collect-form definers)
439 (setf (proto-enums message) (nconc (proto-enums message) (list model))))
440 ((define-message define-extend)
441 (setf (proto-parent model) message)
442 (setf (proto-messages message) (nconc (proto-messages message) (list model)))
443 (when (eq (proto-message-type model) :extends)
444 (setf (proto-extenders message) (nconc (proto-extenders message) (list model)))))
446 (setf (proto-parent model) message)
447 (setf (proto-messages message) (nconc (proto-messages message) (list model)))
449 (collect-slot extra-slot))
450 (setf (proto-fields message) (nconc (proto-fields message) (list extra-field))))
452 (setf (proto-extensions message) (nconc (proto-extensions message) (list model)))))))
454 (multiple-value-bind (field slot idx)
455 (process-field field index :conc-name conc-name :alias-for alias-for)
456 (assert (not (find (proto-index field) (proto-fields message) :key #'proto-index)) ()
457 "The field ~S overlaps with another field in ~S"
458 (proto-value field) (proto-class message))
462 (setf (proto-fields message) (nconc (proto-fields message) (list field)))))))
464 ;; If we've got an alias, define a a type that is the subtype of
465 ;; the Lisp class that typep and subtypep work
466 (unless (or (eq type alias-for) (find-class type nil))
467 (collect-form `(deftype ,type () ',alias-for)))
468 ;; If no alias, define the class now
469 (collect-form `(defclass ,type () (,@slots)
470 ,@(and documentation `((:documentation ,documentation))))))
478 (defun process-field (field index &key conc-name alias-for)
479 "Process one field descriptor within 'define-message' or 'define-extend'.
480 Returns a 'proto-field' object, a CLOS slot form and the incremented field index."
481 (when (i= index 18999) ;skip over the restricted range
483 (destructuring-bind (slot &key type (default nil default-p) reader writer name documentation) field
484 (let* ((idx (if (listp slot) (second slot) (iincf index)))
485 (slot (if (listp slot) (first slot) slot))
486 (reqd (clos-type-to-protobuf-required type))
487 (reader (if (eq reader 't)
488 (intern (if conc-name (format nil "~A~A" conc-name slot) (symbol-name slot))
489 (symbol-package slot))
491 (multiple-value-bind (ptype pclass)
492 (clos-type-to-protobuf-type type)
493 (let ((slot (unless alias-for
498 `(:accessor ,reader)))
501 :initarg ,(kintern (symbol-name slot))
502 ,@(cond ((and (not default-p)
505 ((and (not default-p)
507 ;; Use unbound for booleans only
508 (not (eq pclass :bool)))
511 `(:initform ,default))))))
512 (field (make-instance 'protobuf-field
513 :name (or name (slot-name->proto slot))
521 :default (and default (format nil "~A" default))
522 :packed (and (eq reqd :repeated)
523 (packed-type-p pclass))
524 :documentation documentation)))
525 (values field slot idx))))))
527 ;; Define a service named 'type' with generic functions declared for
528 ;; each of the methods within the service
529 (defmacro define-service (type (&key name options documentation)
531 "Define a service named 'type' and Lisp 'defgeneric' for all its methods.
532 'name' can be used to override the defaultly generated Protobufs service name.
533 'options' is a set of keyword/value pairs, both of which are strings.
535 The body is a set of method specs of the form (name (input-type output-type) &key options).
536 'input-type' and 'output-type' may also be of the form (type &key name)."
537 (let* ((name (or name (class-name->proto type)))
538 (options (loop for (key val) on options by #'cddr
539 collect (make-instance 'protobuf-option
542 (service (make-instance 'protobuf-service
546 :documentation documentation)))
547 (with-collectors ((forms collect-form))
548 (dolist (method method-specs)
549 (destructuring-bind (function (input-type output-type) &key name options documentation) method
550 (let* ((input-name (and (listp input-type)
551 (getf (cdr input-type) :name)))
552 (input-type (if (listp input-type) (car input-type) input-type))
553 (output-name (and (listp output-type)
554 (getf (cdr output-type) :name)))
555 (output-type (if (listp output-type) (car output-type) output-type))
556 (options (loop for (key val) on options by #'cddr
557 collect (make-instance 'protobuf-option
560 (method (make-instance 'protobuf-method
562 :name (or name (class-name->proto function))
563 :input-type input-type
564 :input-name (or input-name (class-name->proto input-type))
565 :output-type output-type
566 :output-name (or output-name (class-name->proto output-type))
568 :documentation documentation)))
569 (setf (proto-methods service) (nconc (proto-methods service) (list method)))
570 ;; The following are the hooks to CL-Stubby
571 (let* ((package (symbol-package function))
573 (server-fn (intern (format nil "~A-~A" 'do function) package))
574 (vinput (intern (format nil "~A-~A" (symbol-name input-type) 'in) package))
575 (voutput (intern (format nil "~A-~A" (symbol-name output-type) 'out) package))
576 (vchannel (intern (symbol-name 'channel) package))
577 (vcallback (intern (symbol-name 'callback) package)))
578 ;; The client side stub, e.g., 'read-air-reservation'.
579 ;; The expectation is that CL-Stubby will provide macrology to make it
580 ;; easy to implement a method for this on each kind of channel (HTTP, TCP socket,
581 ;; IPC, etc). Unlike C++/Java/Python, we don't need a client-side subclass,
582 ;; because we can just use multi-methods.
583 ;; The CL-Stubby macros take care of serializing the input, transmitting the
584 ;; request over the wire, waiting for input (or not if it's asynchronous),
585 ;; filling in the output, and calling the callback (if it's synchronous).
586 ;; It's not very Lispy to side-effect an output object, but it makes
587 ;; asynchronous calls simpler.
588 (collect-form `(defgeneric ,client-fn (,vchannel ,vinput ,voutput &key ,vcallback)
589 ,@(and documentation `((:documentation ,documentation)))
590 (declare (values ,output-type))))
591 ;; The server side stub, e.g., 'do-read-air-reservation'.
592 ;; The expectation is that the server-side program will implement
593 ;; a method with the business logic for this on each kind of channel
594 ;; (HTTP, TCP socket, IPC, etc), possibly on a server-side subclass
595 ;; of the input class
596 ;; The business logic is expected to perform the correct operations on
597 ;; the input object, which arrived via Protobufs, and produce an output
598 ;; of the given type, which will be serialized as a result.
599 ;; The channel objects hold client identity information, deadline info,
600 ;; etc, and can be side-effected to indicate success or failure
601 ;; CL-Stubby provides the channel classes and does (de)serialization, etc
602 (collect-form `(defgeneric ,server-fn (,vchannel ,vinput ,voutput &key ,vcallback)
603 ,@(and documentation `((:documentation ,documentation)))
604 (declare (values ,output-type))))))))
611 ;;; Ensure everything in a Protobufs schema is defined
613 (defvar *undefined-messages*)
615 ;; A very useful tool during development...
616 (defun ensure-all-protobufs ()
619 (loop for p being the hash-values of *all-protobufs*
621 #'string< :key #'proto-name)))
622 (mapcan #'ensure-protobuf protos)))
624 (defmethod ensure-protobuf ((proto protobuf))
625 "Ensure that all of the types are defined in the Protobufs schema 'proto'.
626 This returns two values:
627 - A list whose elements are (<undefined-type> \"message:field\" ...)
628 - The accumulated warnings table that has the same information as objects."
629 (let ((*undefined-messages* (make-hash-table))
630 (trace (list proto)))
631 (map () (curry #'ensure-message trace) (proto-messages proto))
632 (map () (curry #'ensure-service trace) (proto-services proto))
633 (loop for type being the hash-keys of *undefined-messages*
634 using (hash-value things)
636 (mapcar #'(lambda (thing)
637 (format nil "~A:~A" (proto-name (car thing)) (proto-name (cdr thing))))
638 things)) into warnings
639 finally (return (values warnings *undefined-messages*)))))
641 (defmethod ensure-message (trace (message protobuf-message))
642 (let ((trace (cons message trace)))
643 (map () (curry #'ensure-message trace) (proto-messages message))
644 (map () (curry #'ensure-field trace message) (proto-fields message))))
646 (defmethod ensure-field (trace message (field protobuf-field))
647 (ensure-type trace message field (proto-class field)))
649 (defmethod ensure-service (trace (service protobuf-service))
650 (map () (curry #'ensure-method trace service) (proto-methods service)))
652 (defmethod ensure-method (trace service (method protobuf-method))
653 (ensure-type trace service method (proto-input-type method))
654 (ensure-type trace service method (proto-output-type method)))
656 ;; 'message' and 'field' can be a message and a field or a service and a method
657 (defun ensure-type (trace message field type)
658 (unless (keywordp type)
659 (let ((msg (loop for p in trace
660 thereis (or (find-message p type)
661 (find-enum p type)))))
663 (push (cons message field) (gethash type *undefined-messages*))))))