:package package
:lisp-package lisp-pkg
:syntax "proto2"))
+ (*protobuf* schema)
+ (*protobuf-package* (or (find-proto-package lisp-pkg) *package*))
(messages (mapcar #'(lambda (c)
(class-to-protobuf-message c schema
:slot-filter slot-filter
(defun class-to-protobuf-message (class schema
&key slot-filter type-filter enum-filter value-filter)
"Given a CLOS class, return a Protobufs model object for it."
- (let* ((class (find-class class))
+ (let* ((class (let ((c (find-class class)))
+ (unless (class-finalized-p c)
+ (finalize-inheritance c)) ;so the rest of the MOP will work
+ c))
(slots (class-slots class)))
(with-collectors ((enums collect-enum)
(msgs collect-msg)
(when field
(incf index 1) ;don't worry about the 19000-19999 restriction
(collect-field field))))
- (make-instance 'protobuf-message
- :class (class-name class)
- :name (class-name->proto (class-name class))
- :parent schema
- :alias-for (and *alias-existing-classes* (class-name class))
- :enums (delete-duplicates enums :key #'proto-name :test #'string=)
- :messages (delete-duplicates msgs :key #'proto-name :test #'string=)
- :fields fields))))
+ (let* ((cname (class-name class))
+ (pname (class-name->proto cname))
+ (message
+ ;;--- Making the message this late means its children won't
+ ;;--- have the right qualified names
+ (make-instance 'protobuf-message
+ :class cname
+ :name pname
+ :qualified-name (make-qualified-name *protobuf* pname)
+ :parent schema
+ :alias-for (and *alias-existing-classes* cname)
+ :enums (delete-duplicates enums :key #'proto-name :test #'string=)
+ :messages (delete-duplicates msgs :key #'proto-name :test #'string=)
+ :fields fields))
+ (*protobuf* message))
+ ;; Give every child a proper parent
+ (dolist (enum (proto-enums message))
+ (setf (proto-parent enum) message))
+ (dolist (msg (proto-messages message))
+ (setf (proto-parent msg) message))
+ (dolist (field (proto-fields message))
+ (setf (proto-parent field) message))
+ message))))
;; Returns a field, (optionally) an inner message, and (optionally) an inner enum
(defun slot-to-protobuf-field (class slot index slots
#+ignore ;this happens constantly, the warning is not useful
(protobufs-warn "Use DEFTYPE to define a MEMBER type instead of directly using ~S"
expanded-type))
- (make-instance 'protobuf-enum
- :class etype
- :name (class-name->proto ename)
- :values (loop for name in names
- for val in enums
- for index upfrom 0
- collect (make-instance 'protobuf-enum-value
- :name name
- :index index
- :value val))))))
+ (let* ((pname (class-name->proto ename))
+ (enum
+ (make-instance 'protobuf-enum
+ :class etype
+ :name pname
+ :qualified-name (make-qualified-name *protobuf* pname)
+ :parent *protobuf*))
+ (values
+ (loop for name in names
+ for val in enums
+ for index upfrom 0
+ collect (make-instance 'protobuf-enum-value
+ :name name
+ :qualified-name (make-qualified-name enum name)
+ :index index
+ :value val
+ :parent enum))))
+ (setf (proto-values enum) values)
+ enum))))
(default (if (slot-definition-initfunction slot)
(clos-init-to-protobuf-default
(slot-definition-initform slot) expanded-type value-filter)
:packed packed)))
(values field nil enum)))))))
+(defun list-of-list-of ()
+ (let ((list-of-package (find-package 'list-of)))
+ (and list-of-package (find-symbol (string 'list-of) list-of-package))))
+
(defun find-slot-definition-type (class slotd)
"Given a class and a slot descriptor, find the \"best\" type definition for the slot."
(let* ((slot-name (slot-definition-name slotd))
(if direct-slotd
;; The direct slotd will have an unexpanded definition
;; Prefer it for 'list-of' so we can get the base type
- (let ((type (slot-definition-type direct-slotd))
- (quux-list-of (and (find-package :quux)
- (intern "LIST-OF" (find-package :quux)))))
- (values (if (and (listp type) (member (car type) `(list-of vector-of ,quux-list-of)))
+ (let ((type (slot-definition-type direct-slotd)))
+ (values (if (and (listp type)
+ (or (member (car type) '(list-of vector-of))
+ (let ((list-of-list-of (list-of-list-of)))
+ (and list-of-list-of (eq (car type) list-of-list-of)))))
type
(slot-definition-type slotd))
(if (symbolp type)
(class-precedence-list class))))
(and direct-slotd (first (slot-definition-readers direct-slotd)))))
+(defun satisfies-list-of-p (type)
+ (and (consp type)
+ (eq (car type) 'satisfies)
+ (consp (cdr type))
+ (null (cddr type))
+ (let ((function (cadr type)))
+ (and (symbolp function)
+ (string= "LIST-OF" (package-name (symbol-package function)))
+ (let ((name (symbol-name function)))
+ (and (<= #.(length "LIST-OF-_-P") (length name))
+ (starts-with name "LIST-OF-")
+ (ends-with name "-P")
+ (let* ((typestring (subseq name #.(length "LIST-OF-") (- (length name) 2)))
+ (type (ignore-errors
+ (with-standard-io-syntax
+ (let ((*package* (find-package :cl)))
+ (read-from-string typestring))))))
+ (and (typep type 'symbol) type))))))))
+
(defun clos-type-to-protobuf-type (type &optional type-filter enum-filter)
"Given a Lisp type, returns a Protobuf type, a class or primitive type,
whether or not to pack the field, and (optionally) a set of enum values."
- (let ((type (if type-filter (funcall type-filter type) type))
- ;; Hideous, but useful, kludge for those of us at ITA-by-Google
- (quux-list-of (and (find-package :quux)
- (intern "LIST-OF" (find-package :quux)))))
- (flet ((type->protobuf-type (type)
- (case type
- ((int32) (values "int32" :int32))
- ((int64) (values "int64" :int64))
- ((uint32) (values "uint32" :uint32))
- ((uint64) (values "uint64" :uint64))
- ((sint32) (values "sint32" :sint32))
- ((sint64) (values "sint64" :sint64))
- ((fixed32) (values "fixed32" :fixed32))
- ((fixed64) (values "fixed64" :fixed64))
- ((sfixed32) (values "sfixed32" :sfixed32))
- ((sfixed64) (values "sfixed64" :sfixed64))
- ((integer) (values "int64" :int64))
- ((single-float float)
- (values "float" :float))
- ((double-float)
- (values "double" :double))
- ((boolean)
- (values "bool" :bool))
- ((symbol keyword)
- (values "string" :symbol))
- (otherwise
- (cond ((ignore-errors
- (or (eql type 'symbol)
- (subtypep type '(or string character))))
- (values "string" :string))
- ((ignore-errors
- (subtypep type 'byte-vector))
- (values "bytes" :bytes))
- (t
- (values (class-name->proto type) type)))))))
- (if (listp type)
+ (let* ((type (if type-filter (funcall type-filter type) type))
+ (list-of-list-of (list-of-list-of))
+ (type-enum (when (and *protobuf* (symbolp type))
+ (find-enum *protobuf* type)))
+ (type-alias (when (and *protobuf* (symbolp type))
+ (find-type-alias *protobuf* type)))
+ (expanded-type (type-expand type)))
+ (cond
+ ((listp type)
(destructuring-bind (head &rest tail) type
(case head
((or)
(clos-type-to-protobuf-type (second tail))
(clos-type-to-protobuf-type (first tail))))
((and)
- (cond ((and quux-list-of
- (ignore-errors
- (subtypep type `(,quux-list-of t))))
- ;; Special knowledge of 'quux:list-of', which uses (and list (satisfies <t>))
- (let* ((satisfies (find 'satisfies tail :key #'car))
- (pred (second satisfies))
- (type (if (starts-with (string pred) "LIST-OF-")
- (intern (subseq (string pred) #.(length "LIST-OF-")) (symbol-package pred))
- pred)))
- (multiple-value-bind (type class)
- (type->protobuf-type type)
- (values type class (packed-type-p class)))))
- (t
- (let ((new-tail (remove-if #'(lambda (x) (and (listp x) (eq (car x) 'satisfies))) tail)))
- (when (> (length new-tail) 1)
- (protobufs-warn "The AND type ~S is too complicated, proceeding anyway" type))
- (type->protobuf-type (first tail))))))
+ ;; Special knowledge of 'list-of:list-of', which uses (and list (satisfies list-of::FOO-p))
+ (let ((satisfies-list-of
+ (and list-of-list-of (find-if #'satisfies-list-of-p tail))))
+ (if satisfies-list-of
+ (multiple-value-bind (type class)
+ (lisp-type-to-protobuf-type satisfies-list-of)
+ (values type class (packed-type-p class)))
+ (let ((new-tail
+ (remove-if #'(lambda (x) (and (listp x) (eq (car x) 'satisfies))) tail)))
+ (when (> (length new-tail) 1)
+ (protobufs-warn "The AND type ~S is too complicated, proceeding anyway" type))
+ (lisp-type-to-protobuf-type (first tail))))))
((member) ;maybe generate an enum type
(if (or (equal type '(member t nil))
(equal type '(member nil t)))
(error "The MEMBER type ~S is too complicated" type))))))
((list-of vector-of)
(multiple-value-bind (type class)
- (type->protobuf-type (first tail))
+ (lisp-type-to-protobuf-type (first tail))
(values type class (packed-type-p class))))
((integer)
(let ((lo (or (first tail) '*))
(values "uint32" :uint32)
(values "uint64" :uint64))))
((float single-float double-float)
- (type->protobuf-type head))
+ (lisp-type-to-protobuf-type head))
(otherwise
- (if (eq head quux-list-of)
+ (if (eq head list-of-list-of)
(multiple-value-bind (type class)
- (type->protobuf-type (first tail))
+ (lisp-type-to-protobuf-type (first tail))
(values type class (packed-type-p class)))
- (type->protobuf-type type)))))
- (type->protobuf-type type)))))
+ (lisp-type-to-protobuf-type type))))))
+ (type-alias
+ (values (proto-proto-type-str type-alias) type))
+ ((not (or type-enum (equal type expanded-type)))
+ (clos-type-to-protobuf-type expanded-type))
+ (t
+ (lisp-type-to-protobuf-type type)))))
+
+(defun lisp-type-to-protobuf-type (type)
+ (case type
+ ((int32) (values "int32" :int32))
+ ((int64) (values "int64" :int64))
+ ((uint32) (values "uint32" :uint32))
+ ((uint64) (values "uint64" :uint64))
+ ((sint32) (values "sint32" :sint32))
+ ((sint64) (values "sint64" :sint64))
+ ((fixed32) (values "fixed32" :fixed32))
+ ((fixed64) (values "fixed64" :fixed64))
+ ((sfixed32) (values "sfixed32" :sfixed32))
+ ((sfixed64) (values "sfixed64" :sfixed64))
+ ((integer) (values "int64" :int64))
+ ((single-float float)
+ (values "float" :float))
+ ((double-float)
+ (values "double" :double))
+ ((boolean)
+ (values "bool" :bool))
+ ((symbol keyword)
+ (values "string" :symbol))
+ (otherwise
+ (cond ((ignore-errors
+ (or (eql type 'symbol)
+ (subtypep type '(or string character))))
+ (values "string" :string))
+ ((ignore-errors
+ (subtypep type 'byte-vector))
+ (values "bytes" :bytes))
+ (t
+ (values (class-name->proto type) type))))))
(defun packed-type-p (type)
"Returns true if the given Protobufs type can use a packed field."
"Given a Lisp type, returns a \"cardinality\": :required, :optional or :repeated.
If the sceond returned value is true, it's a repeated field that should use a vector."
(let ((type (if type-filter (funcall type-filter type) type))
- (quux-list-of (and (find-package :quux)
- (intern "LIST-OF" (find-package :quux)))))
+ (list-of-list-of (list-of-list-of)))
(if (listp type)
(destructuring-bind (head &rest tail) type
(case head
(clos-type-to-protobuf-required repeated)
(values (if optional :optional :required) nil))))
((and)
- (cond ((or (subtypep type '(list-of t))
- (and quux-list-of (subtypep type `(,quux-list-of t))))
+ (cond ((and (subtypep type 'list)
+ (not (subtypep type 'null)))
(values :repeated nil))
((subtypep type '(vector-of t))
(values :repeated t))
((vector-of)
(values :repeated t))
(otherwise
- (if (eq head quux-list-of)
+ (if (eq head list-of-list-of)
(values :repeated nil)
(values :required nil)))))
(values :required nil))))