(defun write-schema (protobuf &rest keys
&key (stream *standard-output*) (type :proto) &allow-other-keys)
"Writes the object 'protobuf' (schema, message, enum, etc) onto the
- stream 'stream'in the format given by 'type' (:proto, :text, etc)."
+ stream 'stream' in the format given by 'type' (:proto, :text, etc)."
(let ((*protobuf* protobuf))
(apply #'write-schema-as type protobuf stream keys)))
;;; Pretty print a schema as a .lisp file
-(defvar *show-lisp-enum-indexes* t)
+(defvar *show-lisp-enum-indexes* t)
(defvar *show-lisp-field-indexes* t)
+(defvar *use-common-lisp-package* nil)
(defmethod write-schema-as ((type (eql :lisp)) (schema protobuf-schema) stream
&key (indentation 0)
(show-field-indexes *show-lisp-field-indexes*)
- (show-enum-indexes *show-lisp-enum-indexes*))
+ (show-enum-indexes *show-lisp-enum-indexes*)
+ (use-common-lisp *use-common-lisp-package*))
(with-prefixed-accessors (name class documentation package lisp-package imports) (proto- schema)
(let* ((optimize (let ((opt (find-option schema "optimize_for")))
(and opt (cond ((string= opt "SPEED") :speed)
(proto-options schema)))
(pkg (and package (if (stringp package) package (string package))))
(lisp-pkg (and lisp-package (if (stringp lisp-package) lisp-package (string lisp-package))))
- (*show-lisp-enum-indexes* show-enum-indexes)
+ (*show-lisp-enum-indexes* show-enum-indexes)
(*show-lisp-field-indexes* show-field-indexes)
- (*protobuf-package* (or (find-proto-package lisp-pkg) *package*))
- (*package* *protobuf-package*))
+ (*use-common-lisp-package* use-common-lisp)
+ (*protobuf-package* (find-proto-package lisp-pkg))
+ ;; If *protobuf-package* has not been defined, print symbols
+ ;; from :common-lisp if *use-common-lisp-package* is true; or
+ ;; :keyword otherwise. This ensures that all symbols will be
+ ;; read back correctly.
+ ;; (The :keyword package does not use any other packages, so
+ ;; all symbols will be printed with package prefixes.
+ ;; Keywords are always printed as :keyword.)
+ (*package* (or *protobuf-package*
+ (when *use-common-lisp-package* (find-package :common-lisp))
+ (find-package :keyword))))
(when (or lisp-pkg pkg)
(let ((pkg (string-upcase (or lisp-pkg pkg))))
(format stream "~&(cl:eval-when (:execute :compile-toplevel :load-toplevel) ~
~% (unless (cl:find-package \"~A\") ~
- ~% (cl:defpackage ~A (:use :COMMON-LISP)))) ~
+ ~% (cl:defpackage ~A (:use~@[ ~(~S~)~])))) ~
~%(cl:in-package \"~A\") ~
~%(cl:export '(~{~A~^~% ~}))~%~%"
- pkg pkg pkg (collect-exports schema))))
+ pkg pkg (and *use-common-lisp-package* :common-lisp) pkg
+ (collect-exports schema))))
(when documentation
(write-schema-documentation type documentation stream :indentation indentation))
(format stream "~&(proto:define-schema ~(~A~)" (or class name))
;; Export just the slot accessor name
(defmethod collect-exports ((field protobuf-field))
- (list (proto-slot field)))
+ (list (or (proto-reader field)
+ (proto-slot field))))
;; Export the names of all the methods
(defmethod collect-exports ((service protobuf-service))