diff --git a/cl-protobufs.rst b/cl-protobufs.rst index ab22dddaad23c9d3263eef49bc4668adbb37deff..166b92dee5f850907f90335f1c79cb7d046fef49 100644 --- a/cl-protobufs.rst +++ b/cl-protobufs.rst @@ -459,10 +459,11 @@ alias for an enum type. in the .proto file. *body* consists of the enum values, each of which is either a symbol -or a list of the form ``(name index)``. By default, the indexes start -at 0 and are incremented by 1 for each new enum value. For schema -forward and backward compatibility, you should always use the -``(name index)`` form. +or a list either of the form ``(name index)`` or ``(name &key index)``. +By default, and if you have not explicitly given an index, the indexes +start at 0 and are incremented by 1 for each new enum value. For +schema forward and backward compatibility, you should always use the +explicit form, either ``(name index)`` or ``(name &key index)``. ``proto:define-enum`` can be used only within ``proto:define-schema`` or ``proto:define-message``. @@ -499,12 +500,12 @@ in the .proto file. The body *fields* consists of fields, ``proto:define-enum``, ``proto:define-message`` or ``proto:define-extension`` forms. -Fields take the form ``(slot &key type name default reader writer)``. +Fields take the form ``(slot &key index type name default reader writer)``. *slot* can be either a symbol giving the slot name or a list of the form ``(slot index)``. By default, the field indexes start at 1 and are incremented by 1 for each new field value. *type* is the type of the slot. For schema forward and backward compatibility, you should -always use the ``(slot index)`` form. +always use either the ``(slot index)`` form or supply ``:index``. *name* can be used to override the defaultly generated Protobufs field name (for example, a Lisp field called ``color-name``, by default, diff --git a/define-proto.lisp b/define-proto.lisp index 2b5a5cb9d292acb3805215fcb09b16d7d97c61c2..d3e91f0f4a71c2abaccbab6c4e63f0494dfeb727 100644 --- a/define-proto.lisp +++ b/define-proto.lisp @@ -175,7 +175,10 @@ (with-collectors ((vals collect-val) (forms collect-form)) (dolist (val values) - (let* ((idx (if (listp val) (second val) (incf index))) + ;; Allow old (name index) and new (name :index index) + (let* ((idx (if (listp val) + (if (eq (second val) :index) (third val) (second val)) + (incf index))) (name (if (listp val) (first val) val)) (val-name (kintern (if conc-name (format nil "~A~A" conc-name name) (symbol-name name)))) (enum-name (if conc-name (format nil "~A~A" conc-name name) (symbol-name name))) @@ -630,8 +633,12 @@ (setq index 19999)) (destructuring-bind (slot &rest other-options &key type reader writer name (default nil default-p) packed - options documentation &allow-other-keys) field - (let* ((idx (if (listp slot) (second slot) (iincf index))) + ((:index idx)) options documentation &allow-other-keys) field + ;; Allow old ((slot index) ...) or new (slot :index ...), + ;; but only allow one of those two to be used simultaneously + (assert (if idx (not (listp slot)) t) () + "Use either ((slot index) ...) or (slot :index index ...), but not both") + (let* ((idx (or idx (if (listp slot) (second slot) (iincf index)))) (slot (if (listp slot) (first slot) slot)) (reader (or reader (and conc-name