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 ;;; Protobufs schema pretty printing
16 (defun write-schema (protobuf &rest keys
17 &key (stream *standard-output*) (type :proto) &allow-other-keys)
18 "Writes the object 'protobuf' (schema, message, enum, etc) onto the
19 stream 'stream'in the format given by 'type' (:proto, :text, etc)."
20 (let ((*protobuf* protobuf))
21 (apply #'write-schema-as type protobuf stream keys)))
23 (defgeneric write-schema-as (type protobuf stream &key indentation &allow-other-keys)
25 "Writes the protobuf object 'protobuf' (schema, message, enum, etc) onto
26 the given stream 'stream' in the format given by 'type' (:proto, :text, etc).
27 If 'more' is true, this means there are more enum values, fields, etc to
28 be written after the current one."))
30 (defgeneric write-schema-header (type schema stream)
32 "Writes a header for the schema onto the given stream 'stream'
33 in the format given by 'type' (:proto, :text, etc)."))
35 (defgeneric write-schema-documentation (type docstring stream &key indentation)
37 "Writes the docstring as a \"block comment\" onto the given stream 'stream'
38 in the format given by 'type' (:proto, :text, etc)."))
41 ;;; Pretty print a schema as a .proto file
43 (defmethod write-schema-as ((type (eql :proto)) (schema protobuf-schema) stream
45 (with-prefixed-accessors (documentation syntax package imports options) (proto- schema)
47 (write-schema-documentation type documentation stream :indentation indentation))
49 (format stream "~&syntax = \"~A\";~%~%" syntax))
51 (format stream "~&package ~A;~%~%" (substitute #\_ #\- package)))
53 (dolist (import imports)
54 (format stream "~&import \"~A\";~%" import))
56 (write-schema-header type schema stream)
58 (dolist (option options)
59 (format stream "~&option ~:/protobuf-option/;~%" option))
61 (loop for (enum . more) on (proto-enums schema) doing
62 (write-schema-as type enum stream :indentation indentation :more more)
64 (loop for (alias . more) on (proto-type-aliases schema) doing
65 (write-schema-as type alias stream :indentation indentation :more more)
67 (loop for (msg . more) on (proto-messages schema) doing
68 (write-schema-as type msg stream :indentation indentation :more more)
70 (loop for (svc . more) on (proto-services schema) doing
71 (write-schema-as type svc stream :indentation indentation :more more)
74 (defmethod write-schema-documentation ((type (eql :proto)) docstring stream
76 (let ((lines (split-string docstring :separators '(#\newline #\return))))
78 (format stream "~&~@[~VT~]// ~A~%"
79 (and (not (zerop indentation)) indentation) line))))
81 ;; Lisp was born in 1958 :-)
82 (defvar *lisp-options* '(("lisp_package" string 195801)
83 ("lisp_name" string 195802)
84 ("lisp_alias" string 195803)
85 ("lisp_type" string 195804)
86 ("lisp_class" string 195805)
87 ("lisp_slot" string 195806)))
89 (defvar *option-types* '(("ctype" symbol)
92 ("optimize_for" symbol)
95 ("stream_type" string)
96 ;; Keep the rest of these in alphabetical order
97 ("cc_api_version" integer)
98 ("cc_generic_services" symbol)
99 ("go_api_version" integer)
100 ("go_generic_services" symbol)
101 ("go_package" string)
102 ("java_api_version" integer)
103 ("java_generic_services" symbol)
104 ("java_java5_enums" boolean)
105 ("java_multiple_files" boolean)
106 ("java_outer_classname" string)
107 ("java_package" string)
108 ("java_use_javaproto2" boolean)
109 ("py_api_version" integer)
110 ("py_generic_services" symbol)))
112 (defmethod write-schema-header ((type (eql :proto)) (schema protobuf-schema) stream)
113 (when (any-lisp-option schema)
114 (format stream "~&import \"net/proto2/proto/descriptor.proto\";~%~%")
115 (format stream "~&extend proto2.MessageOptions {~%")
116 (loop for (option type index) in *lisp-options* doing
117 (format stream "~& optional ~(~A~) ~A = ~D;~%" type option index))
118 (format stream "~&}~%~%")))
120 (defgeneric any-lisp-option (schema)
122 "Returns true iff there is anything in the schema that would require that
123 the .proto file include and extend 'MessageOptions'.")
124 (:method ((schema protobuf-schema))
125 (labels ((find-one (protobuf)
126 (dolist (enum (proto-enums protobuf))
127 (with-prefixed-accessors (name class alias-for) (proto- enum)
129 (and class (not (string-equal name (class-name->proto class))) class))
130 (return-from any-lisp-option t))))
131 (dolist (msg (proto-messages protobuf))
132 (with-prefixed-accessors (name class alias-for) (proto- msg)
134 (and class (not (string-equal name (class-name->proto class))) class))
135 (return-from any-lisp-option t))))
136 (map () #'find-one (proto-messages protobuf))))
140 (defun cl-user::protobuf-option (stream option colon-p atsign-p)
141 (let* ((type (or (second (find (proto-name option) *option-types* :key #'first :test #'string=))
142 (proto-type option)))
143 (value (proto-value option)))
144 (cond (colon-p ;~:/protobuf-option/ -- .proto format
146 (cond ((find (proto-name option) *lisp-options* :key #'first :test #'string=)
148 ((symbol) "(~A)~@[ = ~A~]")
149 ((boolean) "(~A)~@[ = ~(~A~)~]")
151 (cond ((typep value 'standard-object)
152 ;; If the value is an instance of some class,
153 ;; then it must be some sort of complex option,
154 ;; so print the value using the text format
156 (with-output-to-string (s)
157 (print-text-format value nil
158 :stream s :print-name nil :suppress-line-breaks t)))
161 "(~A)~@[ = ~S~]")))))
164 ((symbol) "~A~@[ = ~A~]")
165 ((boolean) "~A~@[ = ~(~A~)~]")
167 (cond ((typep value 'standard-object)
169 (with-output-to-string (s)
170 (print-text-format value nil
171 :stream s :print-name nil :suppress-line-breaks t)))
173 (t "~A~@[ = ~S~]"))))))))
174 (format stream fmt-control (proto-name option) value)))
175 (atsign-p ;~@/protobuf-option/ -- string/value format
176 (let ((fmt-control (if (eq type 'symbol) "~(~S~) ~A" "~(~S~) ~S")))
177 (format stream fmt-control (proto-name option) value)))
178 (t ;~/protobuf-option/ -- keyword/value format
179 (let ((fmt-control (if (eq type 'symbol) "~(:~A~) ~A" "~(:~A~) ~S")))
180 (format stream fmt-control (proto-name option) value))))))
182 (defun cl-user::source-location (stream location colon-p atsign-p)
183 (declare (ignore colon-p atsign-p))
184 (format stream "(~S ~D ~D)"
185 (source-location-pathname location)
186 (source-location-start-pos location) (source-location-end-pos location)))
188 (defmethod write-schema-as ((type (eql :proto)) (enum protobuf-enum) stream
189 &key (indentation 0) more)
190 (declare (ignore more))
191 (with-prefixed-accessors (name class alias-for documentation options) (proto- enum)
193 (write-schema-documentation type documentation stream :indentation indentation))
194 (format stream "~&~@[~VT~]enum ~A {~%"
195 (and (not (zerop indentation)) indentation)
196 (maybe-qualified-name enum))
197 (let ((other (and class (not (string-equal name (class-name->proto class))) class)))
199 (format stream "~&~VToption (lisp_name) = \"~A:~A\";~%"
200 (+ indentation 2) (package-name (symbol-package class)) (symbol-name class))))
202 (format stream "~&~VToption (lisp_alias) = \"~A:~A\";~%"
203 (+ indentation 2) (package-name (symbol-package alias-for)) (symbol-name alias-for)))
204 (dolist (option options)
205 (format stream "~&option ~:/protobuf-option/;~%" option))
206 (loop for (value . more) on (proto-values enum) doing
207 (write-schema-as type value stream :indentation (+ indentation 2) :more more))
208 (format stream "~&~@[~VT~]}~%"
209 (and (not (zerop indentation)) indentation))))
211 (defparameter *protobuf-enum-comment-column* 56)
212 (defmethod write-schema-as ((type (eql :proto)) (val protobuf-enum-value) stream
213 &key (indentation 0) more)
214 (declare (ignore more))
215 (with-prefixed-accessors (name documentation index) (proto- val)
216 (format stream "~&~@[~VT~]~A = ~D;~:[~*~*~;~VT// ~A~]~%"
217 (and (not (zerop indentation)) indentation)
218 (maybe-qualified-name val) index
219 documentation *protobuf-enum-comment-column* documentation)))
221 (defmethod write-schema-as ((type (eql :proto)) (alias protobuf-type-alias) stream
222 &key (indentation 0) more)
223 (declare (ignore more))
224 (with-prefixed-accessors (name lisp-type proto-type) (proto- alias)
225 (let ((comment (format nil "Note: there is an alias ~A that maps Lisp ~(~S~) to Protobufs ~(~A~)"
226 name lisp-type proto-type)))
227 (write-schema-documentation type comment stream :indentation indentation))
228 (format stream "~&~@[~VT~]~%"
229 (and (not (zerop indentation)) indentation))))
231 (defmethod write-schema-as ((type (eql :proto)) (message protobuf-message) stream
232 &key (indentation 0) more index arity)
233 (declare (ignore more arity))
234 (let ((*protobuf* message))
235 (with-prefixed-accessors (name class alias-for message-type documentation options) (proto- message)
236 (cond ((eq message-type :group)
237 ;; If we've got a group, the printer for fields has already
238 ;; printed a partial line (nice modularity, huh?)
239 (format stream "group ~A = ~D {~%" name index)
240 (let ((other (and class (not (string-equal name (class-name->proto class))) class)))
242 (format stream "~&~VToption (lisp_name) = \"~A:~A\";~%"
243 (+ indentation 2) (package-name (symbol-package class)) (symbol-name class))))
245 (format stream "~&~VToption (lisp_alias) = \"~A:~A\";~%"
246 (+ indentation 2) (package-name (symbol-package alias-for)) (symbol-name alias-for)))
247 (dolist (option options)
248 (format stream "~&~VToption ~:/protobuf-option/;~%"
249 (+ indentation 2) option))
250 (loop for (enum . more) on (proto-enums message) doing
251 (write-schema-as type enum stream :indentation (+ indentation 2) :more more))
252 (loop for (field . more) on (proto-fields message) doing
253 (write-schema-as type field stream
254 :indentation (+ indentation 2) :more more :message message))
255 (format stream "~&~@[~VT~]}~%"
256 (and (not (zerop indentation)) indentation)))
259 (write-schema-documentation type documentation stream :indentation indentation))
260 (format stream "~&~@[~VT~]~A ~A {~%"
261 (and (not (zerop indentation)) indentation)
262 (if (eq message-type :message) "message" "extend")
263 (maybe-qualified-name message))
264 (let ((other (and class (not (string-equal name (class-name->proto class))) class)))
266 (format stream "~&~VToption (lisp_name) = \"~A:~A\";~%"
267 (+ indentation 2) (package-name (symbol-package class)) (symbol-name class))))
269 (format stream "~&~VToption (lisp_alias) = \"~A:~A\";~%"
270 (+ indentation 2) (package-name (symbol-package alias-for)) (symbol-name alias-for)))
271 (dolist (option options)
272 (format stream "~&~VToption ~:/protobuf-option/;~%"
273 (+ indentation 2) option))
274 (cond ((eq message-type :extends)
275 (loop for (field . more) on (proto-extended-fields message) doing
276 (write-schema-as type field stream
277 :indentation (+ indentation 2) :more more
280 (loop for (enum . more) on (proto-enums message) doing
281 (write-schema-as type enum stream :indentation (+ indentation 2) :more more))
282 (loop for (msg . more) on (proto-messages message) doing
283 (unless (eq (proto-message-type msg) :group)
284 (write-schema-as type msg stream :indentation (+ indentation 2) :more more)))
285 (loop for (field . more) on (proto-fields message) doing
286 (write-schema-as type field stream
287 :indentation (+ indentation 2) :more more
289 (loop for (extension . more) on (proto-extensions message) doing
290 (write-schema-as type extension stream :indentation (+ indentation 2) :more more))))
291 (format stream "~&~@[~VT~]}~%"
292 (and (not (zerop indentation)) indentation)))))))
294 (defun maybe-qualified-name (x &optional name)
295 "Given a message, return a fully qualified name if the short name
296 is not sufficient to name the message in the current scope."
298 ((or protobuf-message protobuf-enum protobuf-enum-value
300 (cond ((string= (make-qualified-name (proto-parent x) (proto-name x))
301 (proto-qualified-name x))
304 (proto-qualified-name x))))
307 (defparameter *protobuf-field-comment-column* 56)
308 (defmethod write-schema-as ((type (eql :proto)) (field protobuf-field) stream
309 &key (indentation 0) more message)
310 (declare (ignore more))
311 (with-prefixed-accessors (name documentation required type index packed options) (proto- field)
312 (let* ((class (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
313 (msg (and (not (keywordp class))
314 (or (find-message message class)
315 (find-enum message class)
316 (find-type-alias message class)))))
317 (cond ((and (typep msg 'protobuf-message)
318 (eq (proto-message-type msg) :group))
319 (format stream "~&~@[~VT~]~(~A~) "
320 (and (not (zerop indentation)) indentation) required)
321 (write-schema-as :proto msg stream :indentation indentation :index index :arity required))
323 (let* ((defaultp (if (proto-alias-for message)
324 ;; Special handling for imported CLOS classes
325 (if (eq (proto-required field) :optional)
327 (and (proto-default field)
328 (not (equalp (proto-default field) #()))
329 (not (empty-default-p field))))
330 (not (empty-default-p field))))
331 (default (proto-default field))
332 (default (and defaultp
333 (cond ((and (typep msg 'protobuf-enum)
334 (or (stringp default) (symbolp default)))
335 (let ((e (find default (proto-values msg)
336 :key #'proto-name :test #'string=)))
337 (and e (proto-name e))))
339 (if (boolean-true-p default) "true" "false"))
341 (default (and defaultp
342 (if (stringp default) (escape-string default) default))))
343 (if (typep msg 'protobuf-type-alias)
344 (format stream "~&~@[~VT~]~(~A~) ~(~A~) ~A = ~D~
345 ~:[~*~; [default = ~S]~]~@[ [packed = true]~*~]~{ [~:/protobuf-option/]~};~
346 ~:[~*~*~;~VT// ~A~]~%"
347 (and (not (zerop indentation)) indentation)
348 required (proto-proto-type msg) name index
349 defaultp default packed options
350 t *protobuf-field-comment-column*
351 (format nil "alias maps Lisp ~(~S~) to Protobufs ~(~A~)"
352 (proto-lisp-type msg) (proto-proto-type msg)))
353 (format stream (if (and (keywordp class) (not (eq class :bool)))
354 ;; Keyword class means a primitive type, print default with ~S
355 "~&~@[~VT~]~(~A~) ~A ~A = ~D~
356 ~:[~*~; [default = ~S]~]~@[ [packed = true]~*~]~{ [~:/protobuf-option/]~};~
357 ~:[~*~*~;~VT// ~A~]~%"
358 ;; Non-keyword class means an enum type, print default with ~A"
359 "~&~@[~VT~]~(~A~) ~A ~A = ~D~
360 ~:[~*~; [default = ~A]~]~@[ [packed = true]~*~]~{ [~:/protobuf-option/]~};~
361 ~:[~*~*~;~VT// ~A~]~%")
362 (and (not (zerop indentation)) indentation)
363 required (maybe-qualified-name msg type) name index
364 defaultp default packed options
365 documentation *protobuf-field-comment-column* documentation))))))))
367 (defun escape-string (string)
368 (if (every #'(lambda (ch) (and (standard-char-p ch) (graphic-char-p ch))) string)
370 (with-output-to-string (s)
371 (loop for ch across string
372 as esc = (escape-char ch)
373 do (format s "~A" esc)))))
375 (defmethod write-schema-as ((type (eql :proto)) (extension protobuf-extension) stream
376 &key (indentation 0) more)
377 (declare (ignore more))
378 (with-prefixed-accessors (from to) (proto-extension- extension)
379 (format stream "~&~@[~VT~]extensions ~D~:[~*~; to ~D~];~%"
380 (and (not (zerop indentation)) indentation)
381 from (not (eql from to)) (if (eql to #.(1- (ash 1 29))) "max" to))))
383 (defmethod write-schema-as ((type (eql :proto)) (service protobuf-service) stream
384 &key (indentation 0) more)
385 (declare (ignore more))
386 (with-prefixed-accessors (name documentation) (proto- service)
388 (write-schema-documentation type documentation stream :indentation indentation))
389 (format stream "~&~@[~VT~]service ~A {~%"
390 (and (not (zerop indentation)) indentation) name)
391 (loop for (method . more) on (proto-methods service) doing
392 (write-schema-as type method stream :indentation (+ indentation 2) :more more))
393 (format stream "~&~@[~VT~]}~%"
394 (and (not (zerop indentation)) indentation))))
396 (defmethod write-schema-as ((type (eql :proto)) (method protobuf-method) stream
397 &key (indentation 0) more)
398 (declare (ignore more))
399 (with-prefixed-accessors
400 (name documentation input-name output-name streams-name options) (proto- method)
401 (let* ((imsg (find-message *protobuf* input-name))
402 (omsg (find-message *protobuf* output-name))
403 (smsg (find-message *protobuf* streams-name))
404 (iname (maybe-qualified-name imsg))
405 (oname (maybe-qualified-name omsg))
406 (sname (maybe-qualified-name smsg)))
408 (write-schema-documentation type documentation stream :indentation indentation))
409 (format stream "~&~@[~VT~]rpc ~A (~@[~A~])~@[ streams (~A)~]~@[ returns (~A)~]"
410 (and (not (zerop indentation)) indentation)
411 name iname sname oname)
413 (format stream " {~%")
414 (dolist (option options)
415 (format stream "~&~@[~VT~]option ~:/protobuf-option/;~%"
416 (+ indentation 2) option))
417 (format stream "~@[~VT~]}"
418 (and (not (zerop indentation)) indentation)))
420 (format stream ";~%"))))))
423 ;;; Pretty print a schema as a .lisp file
425 (defvar *show-lisp-enum-indexes* t)
426 (defvar *show-lisp-field-indexes* t)
427 (defvar *use-common-lisp-package* nil)
429 (defmethod write-schema-as ((type (eql :lisp)) (schema protobuf-schema) stream
431 (show-field-indexes *show-lisp-field-indexes*)
432 (show-enum-indexes *show-lisp-enum-indexes*)
433 (use-common-lisp *use-common-lisp-package*))
434 (with-prefixed-accessors (name class documentation package lisp-package imports) (proto- schema)
435 (let* ((optimize (let ((opt (find-option schema "optimize_for")))
436 (and opt (cond ((string= opt "SPEED") :speed)
437 ((string= opt "CODE_SIZE") :space)
439 (options (remove-if #'(lambda (x) (string= (proto-name x) "optimize_for"))
440 (proto-options schema)))
441 (pkg (and package (if (stringp package) package (string package))))
442 (lisp-pkg (and lisp-package (if (stringp lisp-package) lisp-package (string lisp-package))))
443 (*show-lisp-enum-indexes* show-enum-indexes)
444 (*show-lisp-field-indexes* show-field-indexes)
445 (*use-common-lisp-package* use-common-lisp)
446 (*protobuf-package* (or (find-proto-package lisp-pkg) *package*))
447 (*package* *protobuf-package*))
448 (when (or lisp-pkg pkg)
449 (let ((pkg (string-upcase (or lisp-pkg pkg))))
450 (format stream "~&(cl:eval-when (:execute :compile-toplevel :load-toplevel) ~
451 ~% (unless (cl:find-package \"~A\") ~
452 ~% (cl:defpackage ~A (:use~@[ ~(~S~)~])))) ~
453 ~%(cl:in-package \"~A\") ~
454 ~%(cl:export '(~{~A~^~% ~}))~%~%"
455 pkg pkg (and *use-common-lisp-package* :common-lisp) pkg
456 (collect-exports schema))))
458 (write-schema-documentation type documentation stream :indentation indentation))
459 (format stream "~&(proto:define-schema ~(~A~)" (or class name))
460 (if (or pkg lisp-pkg imports optimize options documentation)
461 (format stream "~% (")
462 (format stream " ("))
465 (format stream "~A:package \"~A\"" spaces pkg)
466 (when (or lisp-pkg imports optimize options documentation)
470 (format stream "~A:lisp-package \"~A\"" spaces lisp-pkg)
471 (when (or imports optimize options documentation)
475 (cond ((= (length imports) 1)
476 (format stream "~A:import \"~A\"" spaces (car imports)))
478 (format stream "~A:import (~{\"~A\"~^ ~})" spaces imports)))
479 (when (or optimize options documentation)
483 (format stream "~A:optimize ~(~S~)" spaces optimize)
484 (when (or options documentation)
488 (format stream "~A:options (~{~/protobuf-option/~^ ~})" spaces options)
493 (format stream "~A:documentation ~S" spaces documentation)))
495 (loop for (enum . more) on (proto-enums schema) doing
496 (write-schema-as type enum stream :indentation 2 :more more))
497 (loop for (alias . more) on (proto-type-aliases schema) doing
498 (write-schema-as type alias stream :indentation 2 :more more))
499 (loop for (msg . more) on (proto-messages schema) doing
500 (write-schema-as type msg stream :indentation 2 :more more))
501 (loop for (svc . more) on (proto-services schema) doing
502 (write-schema-as type svc stream :indentation 2 :more more)))
503 (format stream ")~%")))
505 (defmethod write-schema-documentation ((type (eql :lisp)) docstring stream
506 &key (indentation 0))
507 (let ((lines (split-string docstring :separators '(#\newline #\return))))
509 (format stream "~&~@[~VT~];; ~A~%"
510 (and (not (zerop indentation)) indentation) line))))
512 (defmethod write-schema-header ((type (eql :lisp)) (schema protobuf-schema) stream)
513 (declare (ignorable type stream))
516 (defmethod write-schema-as ((type (eql :lisp)) (enum protobuf-enum) stream
517 &key (indentation 0) more)
518 (declare (ignore more))
520 (with-prefixed-accessors (name class alias-for
521 documentation source-location) (proto- enum)
523 (write-schema-documentation type documentation stream :indentation indentation))
524 (format stream "~@[~VT~](proto:define-enum ~(~S~)"
525 (and (not (zerop indentation)) indentation) class)
526 (let ((other (and name (not (string-equal name (class-name->proto class))) name)))
527 (cond ((or other alias-for documentation source-location)
528 (format stream "~%~@[~VT~](~:[~*~*~;:name ~(~S~)~@[~%~VT~]~]~
529 ~:[~*~*~;:alias-for ~(~S~)~@[~%~VT~]~]~
530 ~:[~*~;:documentation ~S~@[~%~VT~]~]~
531 ~:[~*~;:source-location ~/source-location/~])"
533 other other (and (or alias-for documentation) (+ indentation 5))
534 alias-for alias-for (and (or documentation source-location) (+ indentation 5))
535 documentation documentation (and source-location (+ indentation 5))
536 source-location source-location))
538 (format stream " ()"))))
539 (loop for (value . more) on (proto-values enum) doing
540 (write-schema-as type value stream :indentation (+ indentation 2) :more more)
543 (format stream ")")))
545 (defmethod write-schema-as ((type (eql :lisp)) (val protobuf-enum-value) stream
546 &key (indentation 0) more)
547 (declare (ignore more))
548 (with-prefixed-accessors (value index) (proto- val)
549 (if *show-lisp-enum-indexes*
550 (format stream "~&~@[~VT~](~(~A~) ~D)"
551 (and (not (zerop indentation)) indentation) value index)
552 (format stream "~&~@[~VT~]~(~A~)"
553 (and (not (zerop indentation)) indentation) value))))
555 (defmethod write-schema-as ((type (eql :lisp)) (alias protobuf-type-alias) stream
556 &key (indentation 0) more)
557 (declare (ignore more))
559 (with-prefixed-accessors (class lisp-type proto-type serializer deserializer) (proto- alias)
560 (format stream "~@[~VT~](proto:define-type-alias ~(~S~)"
561 (and (not (zerop indentation)) indentation) class)
562 (format stream " ()") ;no options yet
563 (format stream "~%~@[~VT~]:lisp-type ~(~S~)~
564 ~%~@[~VT~]:proto-type ~(~A~)~
565 ~%~@[~VT~]:serializer ~(~S~)~
566 ~%~@[~VT~]:deserializer ~(~S~))"
567 (+ indentation 2) lisp-type
568 (+ indentation 2) proto-type
569 (+ indentation 2) serializer
570 (+ indentation 2) deserializer)))
572 (defmethod write-schema-as ((type (eql :lisp)) (message protobuf-message) stream
573 &key (indentation 0) more index arity)
574 (declare (ignore more))
575 (let ((*protobuf* message))
576 (with-prefixed-accessors (name class alias-for conc-name message-type
577 documentation source-location) (proto- message)
578 (cond ((eq message-type :group)
580 (write-schema-documentation type documentation stream :indentation indentation))
581 (format stream "~&~@[~VT~](proto:define-group ~(~S~)"
582 (and (not (zerop indentation)) indentation) class)
583 (let ((other (and name (not (string-equal name (class-name->proto class))) name)))
584 (format stream "~%~@[~VT~](:index ~D~@[~%~VT~]~
585 :arity ~(~S~)~@[~%~VT~]~
586 ~:[~*~*~;:name ~(~S~)~@[~%~VT~]~]~
587 ~:[~*~*~;:alias-for ~(~S~)~@[~%~VT~]~]~
588 ~:[~*~*~;:conc-name ~(~S~)~@[~%~VT~]~]~
589 ~:[~*~;:documentation ~S~@[~%~VT~]~]~
590 ~:[~*~;:source-location ~/source-location/~])"
592 index (+ indentation 5)
593 arity (and (or other alias-for conc-name documentation source-location) (+ indentation 5))
594 other other (and (or alias-for conc-name documentation source-location) (+ indentation 5))
595 alias-for alias-for (and (or conc-name documentation source-location) (+ indentation 5))
596 conc-name conc-name (and (or documentation source-location) (+ indentation 5))
597 documentation documentation (and source-location (+ indentation 5))
598 source-location source-location))
599 (loop for (enum . more) on (proto-enums message) doing
600 (write-schema-as type enum stream :indentation (+ indentation 2) :more more)
603 (loop for (field . more) on (proto-fields message) doing
604 (write-schema-as type field stream
605 :indentation (+ indentation 2) :more more
611 (write-schema-documentation type documentation stream :indentation indentation))
612 (format stream "~&~@[~VT~](proto:define-~A ~(~S~)"
613 (and (not (zerop indentation)) indentation)
614 (if (eq message-type :message) "message" "extend") class)
615 (let ((other (and name (not (string-equal name (class-name->proto class))) name)))
616 (cond ((eq message-type :extends)
617 (format stream " ()"))
618 ((or other alias-for conc-name documentation source-location)
619 (format stream "~%~@[~VT~](~:[~*~*~;:name ~(~S~)~@[~%~VT~]~]~
620 ~:[~*~*~;:alias-for ~(~S~)~@[~%~VT~]~]~
621 ~:[~*~*~;:conc-name ~(~S~)~@[~%~VT~]~]~
622 ~:[~*~;:documentation ~S~@[~%~VT~]~]~
623 ~:[~*~;:source-location ~/source-location/~])"
625 other other (and (or alias-for conc-name documentation source-location) (+ indentation 5))
626 alias-for alias-for (and (or conc-name documentation source-location) (+ indentation 5))
627 conc-name conc-name (and (or documentation source-location) (+ indentation 5))
628 documentation documentation (and source-location (+ indentation 5))
629 source-location source-location))
631 (format stream " ()"))))
632 (cond ((eq message-type :extends)
633 (loop for (field . more) on (proto-extended-fields message) doing
634 (write-schema-as type field stream
635 :indentation (+ indentation 2) :more more
640 (loop for (enum . more) on (proto-enums message) doing
641 (write-schema-as type enum stream :indentation (+ indentation 2) :more more)
644 (loop for (msg . more) on (proto-messages message) doing
645 (unless (eq (proto-message-type msg) :group)
646 (write-schema-as type msg stream :indentation (+ indentation 2) :more more)
649 (loop for (field . more) on (proto-fields message) doing
650 (write-schema-as type field stream
651 :indentation (+ indentation 2) :more more
655 (loop for (extension . more) on (proto-extensions message) doing
656 (write-schema-as type extension stream :indentation (+ indentation 2) :more more)
658 (terpri stream)))))))
659 (format stream ")"))))
661 (defparameter *protobuf-slot-comment-column* 56)
662 (defmethod write-schema-as ((type (eql :lisp)) (field protobuf-field) stream
663 &key (indentation 0) more message)
664 (with-prefixed-accessors (value required index packed options documentation) (proto- field)
665 (let* ((class (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
666 (msg (and (not (keywordp class))
667 (or (find-message message class)
668 (find-enum message class)
669 (find-type-alias message class))))
670 (type (let ((cl (case class
677 ((:fixed32) 'fixed32)
678 ((:fixed64) 'fixed64)
679 ((:sfixed32) 'sfixed32)
680 ((:sfixed64) 'sfixed64)
682 ((:double) 'double-float)
685 ((:bytes) 'byte-vector)
688 (cond ((eq required :optional)
690 ((eq required :repeated)
691 (if (vector-field-p field)
695 (cond ((and (typep msg 'protobuf-message)
696 (eq (proto-message-type msg) :group))
697 (write-schema-as :lisp msg stream :indentation indentation :index index :arity required))
699 (let* ((defaultp (if (proto-alias-for message)
700 (if (eq (proto-required field) :optional)
702 (and (proto-default field)
703 (not (equalp (proto-default field) #()))
704 (not (empty-default-p field))))
705 (not (empty-default-p field))))
706 (default (proto-default field))
707 (default (and defaultp
708 (cond ((and (typep msg 'protobuf-enum)
709 (or (stringp default) (symbolp default)))
710 (let ((e (find default (proto-values msg)
711 :key #'proto-name :test #'string=)))
712 (and e (proto-value e))))
714 (boolean-true-p default))
716 (default (and defaultp
717 (if (stringp default) (escape-string default) default)))
718 (conc-name (proto-conc-name message))
719 (reader (when (and (not (eq (proto-reader field) value))
720 (not (string-equal (proto-reader field)
721 (format nil "~A~A" conc-name value))))
722 (proto-reader field)))
723 (writer (when (and (not (eq (proto-writer field) value))
724 (not (string-equal (proto-writer field)
725 (format nil "~A~A" conc-name value))))
726 (proto-writer field)))
727 (slot-name (if *show-lisp-field-indexes*
728 (format nil "(~(~S~) ~D)" value index)
729 (format nil "~(~S~)" value))))
730 (format stream (if (and (keywordp class) (not (eq class :bool)))
731 ;; Keyword class means a primitive type, print default with ~S
732 "~&~@[~VT~](~A :type ~(~S~)~:[~*~; :default ~S~]~
733 ~@[ :reader ~(~S~)~]~@[ :writer ~(~S~)~]~@[ :packed ~(~S~)~]~
734 ~@[ :options (~{~/protobuf-option/~^ ~})~])~
736 ;; Non-keyword class means an enum type, print default with ~(~S~)
737 "~&~@[~VT~](~A :type ~(~S~)~:[~*~; :default ~(~S~)~]~
738 ~@[ :reader ~(~S~)~]~@[ :writer ~(~S~)~]~@[ :packed ~(~S~)~]~
739 ~@[ :options (~{~/protobuf-option/~^ ~})~])~
741 (and (not (zerop indentation)) indentation)
742 slot-name type defaultp default reader writer packed options
743 ;; Don't write the comment if we'll insert a close paren after it
744 (and more documentation) *protobuf-slot-comment-column* documentation)))))))
746 (defmethod write-schema-as ((type (eql :lisp)) (extension protobuf-extension) stream
747 &key (indentation 0) more)
748 (declare (ignore more))
749 (with-prefixed-accessors (from to) (proto-extension- extension)
750 (format stream "~&~@[~VT~](proto:define-extension ~D ~D)"
751 (and (not (zerop indentation)) indentation)
752 from (if (eql to #.(1- (ash 1 29))) "max" to))))
754 (defmethod write-schema-as ((type (eql :lisp)) (service protobuf-service) stream
755 &key (indentation 0) more)
756 (declare (ignore more))
757 (with-prefixed-accessors (class documentation source-location) (proto- service)
759 (write-schema-documentation type documentation stream :indentation indentation))
760 (format stream "~&~@[~VT~](proto:define-service ~(~S~)"
761 (and (not (zerop indentation)) indentation) (proto-class service))
762 (cond ((or documentation source-location)
763 (format stream "~%~@[~VT~](~:[~*~;:documentation ~S~@[~%~VT~]~]~
764 ~:[~*~;:source-location ~/source-location/~])"
766 documentation documentation (and source-location (+ indentation 5))
767 source-location source-location))
769 (format stream " ()")))
770 (loop for (method . more) on (proto-methods service) doing
771 (write-schema-as type method stream :indentation (+ indentation 2) :more more)
774 (format stream ")")))
776 (defmethod write-schema-as ((type (eql :lisp)) (method protobuf-method) stream
777 &key (indentation 0) more)
778 (declare (ignore more))
779 (with-prefixed-accessors (class input-type output-type streams-type options
780 documentation source-location) (proto- method)
782 (write-schema-documentation type documentation stream :indentation indentation))
783 (format stream "~&~@[~VT~](~(~S~) (~(~S~) => ~(~S~)~@[ :streams ~(~S~)~])"
784 (and (not (zerop indentation)) indentation)
785 class input-type output-type streams-type)
787 (format stream "~%~VT:options (~{~/protobuf-option/~^ ~})"
788 (+ indentation 2) options))
789 (format stream ")")))
792 ;;; Collect symbols to be exported
794 (defgeneric collect-exports (schema)
796 "Collect all the symbols that should be exported from a Protobufs package"))
798 (defmethod collect-exports ((schema protobuf-schema))
801 (append (mapcan #'collect-exports (proto-enums schema))
802 (mapcan #'collect-exports (proto-messages schema))
803 (mapcan #'collect-exports (proto-services schema))))
806 ;; Export just the type name
807 (defmethod collect-exports ((enum protobuf-enum))
808 (list (proto-class enum)))
810 ;; Export the class name and all of the accessor names
811 (defmethod collect-exports ((message protobuf-message))
812 (append (list (proto-class message))
813 (mapcan #'collect-exports (proto-fields message))))
815 ;; Export just the slot accessor name
816 (defmethod collect-exports ((field protobuf-field))
817 (list (proto-slot field)))
819 ;; Export the names of all the methods
820 (defmethod collect-exports ((service protobuf-service))
821 (mapcan #'collect-exports (proto-methods service)))
823 ;; Export just the method name
824 (defmethod collect-exports ((method protobuf-method))
825 (list (proto-client-stub method) (proto-server-stub method)))