[Rprotobuf-commits] r872 - in pkg: . R inst inst/unitTests src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Mar 11 02:12:49 CET 2014


Author: murray
Date: 2014-03-11 02:12:48 +0100 (Tue, 11 Mar 2014)
New Revision: 872

Modified:
   pkg/ChangeLog
   pkg/R/00classes.R
   pkg/inst/NEWS.Rd
   pkg/inst/unitTests/runit.enums.R
   pkg/src/DescriptorPoolLookup.cpp
   pkg/src/rprotobuf.cpp
Log:
* Fix a bug in the show method for EnumDescriptor types.
* Import all top-level enums from imported .proto files add add a test.



Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2014-02-22 00:24:18 UTC (rev 871)
+++ pkg/ChangeLog	2014-03-11 01:12:48 UTC (rev 872)
@@ -1,3 +1,14 @@
+2014-03-10  Murray Stokely  <mstokely at google.com>
+
+	* src/DescriptorPoolLookup.cpp (rprotobuf): Import all top-level
+	  enums defined in an imported .proto file.
+	* src/rprotobuf.cpp (rprotobuf): Add a function to get the enum
+	  descriptor associated with a named enum.
+	* inst/unitTests/runit.enums.R (test.enums): Test that we import
+	  top-level enums from .proto files correctly.
+	* R/00classes.R (P): Correct a bug in the show() method for
+	  EnumDescriptor that prevented useful output in some contexts.
+
 2014-02-21  Murray Stokely  <mstokely at google.com>
 
 	* inst/unitTests/runit.messages.R (test.message): Add a test for

Modified: pkg/R/00classes.R
===================================================================
--- pkg/R/00classes.R	2014-02-22 00:24:18 UTC (rev 871)
+++ pkg/R/00classes.R	2014-03-11 01:12:48 UTC (rev 872)
@@ -34,7 +34,7 @@
 	pointer = "externalptr" ,  # pointer to a google::protobuf::EnumDescriptor c++ object
 	name    = "character",
 	full_name  = "character",
-	type    = "character"
+	type    = "character"   # TODO(mstokely): enums don't really have another type, remove?
 ), prototype = list( pointer = NULL, name = character(0),
 	full_name = character(0), type = character(0) ) )
 
@@ -122,7 +122,11 @@
 		# See if it is an extension
 		desc <- .Call("getExtensionDescriptor", type, PACKAGE="RProtoBuf")
 		if (is.null(desc)) {
-			stop( sprintf( "could not find descriptor for message type '%s' ", type ) )
+			# See if it is an enum
+			desc <- .Call("getEnumDescriptor", type, PACKAGE="RProtoBuf")
+			if (is.null(desc)) {
+				stop( sprintf( "could not find descriptor for message type '%s' ", type ) )
+			}
 		}
 	}
 	desc
@@ -146,7 +150,8 @@
 	show( sprintf( "descriptor for field '%s' of type '%s' ", object at name, object at type ) )
 } )
 setMethod( "show", c( "EnumDescriptor" ), function(object){
-	show( sprintf( "descriptor for enum '%s' of type '%s' with %d values", object at name, object at type, value_count(object) ) )
+	show( sprintf( "descriptor for enum '%s' with %d values", object at name,
+                      value_count(object) ) )
 } )
 setMethod( "show", c( "ServiceDescriptor" ), function(object){
 	show( sprintf( "service descriptor <%s>", object at name ) )

Modified: pkg/inst/NEWS.Rd
===================================================================
--- pkg/inst/NEWS.Rd	2014-02-22 00:24:18 UTC (rev 871)
+++ pkg/inst/NEWS.Rd	2014-03-11 01:12:48 UTC (rev 872)
@@ -2,6 +2,18 @@
 \title{News for Package \pkg{RProtoBuf}}
 \newcommand{\cpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
 
+\section{Changes in RProtoBuf version 0.4.x (2014-XX-XX)}{
+  \itemize{
+    \item Document and add a test for the deprecated group
+    functionality.
+    \item Add a \code{CITATION} file pointing to the JSS preprint on
+    arXiv.org.
+    \item Fix a bug in the \code{show} method for \code{EnumDescriptor}
+    types.
+    \item Import all top-level enums from imported \code{.proto} files.
+  }
+}
+
 \section{Changes in RProtoBuf version 0.4.0 (2014-01-14)}{
   \itemize{
     \item Changes to support CRAN builds for MS Windows.

Modified: pkg/inst/unitTests/runit.enums.R
===================================================================
--- pkg/inst/unitTests/runit.enums.R	2014-02-22 00:24:18 UTC (rev 871)
+++ pkg/inst/unitTests/runit.enums.R	2014-03-11 01:12:48 UTC (rev 872)
@@ -1,15 +1,15 @@
 # Copyright 2011 Google Inc.
-# 
+#
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; either version 2
 # of the License, or (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
@@ -43,4 +43,14 @@
 
   # Verify that invalid indices are returned as NULL.
   checkTrue(is.null(value(ProtoFormat$PhoneType, index=900)))
+
+  # Verify that we import top-level enums from .proto files.
+  if (!exists("protobuf_unittest.TestAllTypes",
+              "RProtoBuf:DescriptorPool")) {
+    unittest.proto.file <- system.file("unitTests", "data",
+                                       "unittest.proto",
+                                       package="RProtoBuf")
+    readProtoFiles(file=unittest.proto.file)
+  }
+  checkTrue(inherits(P("protobuf_unittest.ForeignEnum"), "EnumDescriptor"))
 }

Modified: pkg/src/DescriptorPoolLookup.cpp
===================================================================
--- pkg/src/DescriptorPoolLookup.cpp	2014-02-22 00:24:18 UTC (rev 871)
+++ pkg/src/DescriptorPoolLookup.cpp	2014-03-11 01:12:48 UTC (rev 872)
@@ -40,6 +40,9 @@
 /**
  * Add descriptors from a proto file to the descriptor pool.
  *
+ * Specifically, message types, extensions, and enums are added.
+ * Services are not added because they are not really used in RProtoBuf.
+ *
  * @param files A character vector of .proto files to import.
  * @param dirs A character vector of directories to import from.
  * @throws Rcpp::exception if a file can't be loaded (uncaught).
@@ -54,19 +57,25 @@
                 "'\n";
             Rcpp_error(message.c_str());
         }
+        // add top level messages from the file.
         int ntypes = file_desc->message_type_count();
         for (int i = 0; i < ntypes; i++) {
             const GPB::Descriptor* desc = file_desc->message_type(i);
             add(desc->full_name());
             /* should we bother recursing ? */
-            /* TODO(mstokely): add top level enums and services? */
         }
-        // add top level extensions!
+        // add top level extensions
         int nexts = file_desc->extension_count();
         for (int i = 0; i < nexts; i++) {
             const GPB::FieldDescriptor* field_desc = file_desc->extension(i);
             add(field_desc->full_name());
         }
+        // add top level enums.
+        int nenums = file_desc->enum_type_count();
+        for (int i = 0; i < nenums; i++) {
+            const GPB::EnumDescriptor* enum_desc = file_desc->enum_type(i);
+            add(enum_desc->full_name());
+        }
     }
     // source_tree.removeDirectories( dirs ) ;
 }

Modified: pkg/src/rprotobuf.cpp
===================================================================
--- pkg/src/rprotobuf.cpp	2014-02-22 00:24:18 UTC (rev 871)
+++ pkg/src/rprotobuf.cpp	2014-03-11 01:12:48 UTC (rev 872)
@@ -116,6 +116,40 @@
 }
 
 /**
+ * get the descriptor associated with an enum
+ *
+ * @param type message type
+ *
+ * @return an S4 object of class EnumDescriptor, or NULL if the type
+ *  is unknown
+ */
+RcppExport SEXP getEnumDescriptor(SEXP type) {
+#ifdef RPB_DEBUG
+    Rprintf("<getEnumDescriptor>\n      type = ");
+    Rf_PrintValue(type);
+#endif
+
+    const char* typeName = CHAR(STRING_ELT(type, 0));
+
+    /* first try the generated pool */
+    const GPB::DescriptorPool* pool = GPB::DescriptorPool::generated_pool();
+    Rprintf("typeName = %s", typeName);
+    const GPB::EnumDescriptor* desc = pool->FindEnumTypeByName(typeName);
+    if (!desc) {
+        /* then try the "runtime" pool" */
+        pool = DescriptorPoolLookup::pool();
+        Rprintf("trying runtime pool typeName = %s", typeName);
+        desc = pool->FindEnumTypeByName(typeName);
+        if (!desc) {
+            /* unlucky */
+            return R_NilValue;
+        }
+    }
+
+    return (S4_EnumDescriptor(desc));
+}
+
+/**
  * make a new protobuf message
  *
  * @param descriptor a "Descriptor" R object



More information about the Rprotobuf-commits mailing list