]> asedeno.scripts.mit.edu Git - cl-protobufs.git/commitdiff
Add a more readable syntax for enum and field indices
authorScott McKay <swm@google.com>
Mon, 10 Sep 2012 17:38:33 +0000 (17:38 +0000)
committerScott McKay <swm@google.com>
Mon, 10 Sep 2012 17:38:33 +0000 (17:38 +0000)
Testing : precheckin --full --strict-errors
Reviewer: Fare (please)

JTB impact: No
Ops impact: No

Change to config                        : No
Change to XML schema                    : No
Change to DB schema                     : No
Change to transport (timeouts, headers) : No
Any change (or new use) of OAQs         : No
Change to inter-component transactions  : No
Depends on any other checkin / bug      : No

Tests that will verify:

I extended the CL-Protobufs tests

Description:

Add a more readable syntax for enum and field indices.

For define-enum, it was (name value).
  Allow (name :index value).

For define-message, it was ((name index) ...).
  Allow (name :index index ...).
  Complain if both forms appear in the same field.

git-svn-id: http://svn.internal.itasoftware.com/svn/ita/trunk/qres/lisp/libs/cl-protobufs@561136 f8382938-511b-0410-9cdd-bb47b084005c

cl-protobufs.rst
define-proto.lisp

index ab22dddaad23c9d3263eef49bc4668adbb37deff..166b92dee5f850907f90335f1c79cb7d046fef49 100644 (file)
@@ -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,
index 2b5a5cb9d292acb3805215fcb09b16d7d97c61c2..d3e91f0f4a71c2abaccbab6c4e63f0494dfeb727 100644 (file)
     (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)))
     (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