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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 22 05:05:53 CEST 2013


Author: murray
Date: 2013-08-22 05:05:48 +0200 (Thu, 22 Aug 2013)
New Revision: 508

Modified:
   pkg/ChangeLog
   pkg/R/00classes.R
   pkg/inst/unitTests/runit.extensions.R
   pkg/man/P.Rd
   pkg/src/rprotobuf.h
   pkg/src/wrapper_Message.cpp
Log:
Improve extensions support by:

1) Allowing one to find the FieldDescriptor for any extension with
   P(), even if it is in some weird nested location and not automatically
   imported.

2) Add a function for showing the number of extensions that are set in
   a given message, and use this to improve the show() output for
   messages.

3) Update tests and documentation.



Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2013-07-15 03:02:53 UTC (rev 507)
+++ pkg/ChangeLog	2013-08-22 03:05:48 UTC (rev 508)
@@ -1,3 +1,14 @@
+2013-08-21  Murray Stokely  <murray at FreeBSD.org>
+
+	* man/P.Rd: Document behavior for extensions.
+	* inst/unitTests/runit.extensions.R (test.extension): Add another test.
+	* src/wrapper_Message.cpp (rprotobuf): Add function for returning
+	  the number of extensions set in this message, to improve show() output.
+	* src/rprotobuf.cpp (rprotobuf): Add support for looking up
+	  extensions by name and returning FieldDescriptors.
+	* R/00classes.R (P): Add support for returning extension
+	  descriptors.
+
 2013-07-14  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/unitTests/runit.extensions.R (test.extension): Comment-out

Modified: pkg/R/00classes.R
===================================================================
--- pkg/R/00classes.R	2013-07-15 03:02:53 UTC (rev 507)
+++ pkg/R/00classes.R	2013-08-22 03:05:48 UTC (rev 508)
@@ -119,7 +119,11 @@
 	desc <- .Call( "getProtobufDescriptor", type,
 		PACKAGE = "RProtoBuf" )
 	if( is.null( desc ) ){
-		stop( sprintf( "could not find descriptor for message type '%s' ", type ) )
+		# 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 ) )
+		}
 	}
 	desc
 }
@@ -127,8 +131,13 @@
 
 # {{{ show
 setMethod( "show", c( "Message" ), function(object){
-  show( sprintf( "message of type '%s' with %d field%s set", object at type,
-                length(object), if (length(object) == 1) "" else "s" ))
+  tmp <- sprintf( "message of type '%s' with %d field%s set", object at type,
+                 length(object), if (length(object) == 1) "" else "s" )
+  nexts <- .Call("Message__num_extensions", object at pointer, PACKAGE="RProtoBuf")
+  if (nexts > 0) {
+    tmp <- paste(tmp, sprintf("and %d extension%s", nexts, if (nexts == 1) "" else "s"))
+  }
+  show(tmp)
 } )
 setMethod( "show", c( "Descriptor" ), function(object){
 	show( sprintf( "descriptor for type '%s' ", object at type ) )

Modified: pkg/inst/unitTests/runit.extensions.R
===================================================================
--- pkg/inst/unitTests/runit.extensions.R	2013-07-15 03:02:53 UTC (rev 507)
+++ pkg/inst/unitTests/runit.extensions.R	2013-08-22 03:05:48 UTC (rev 508)
@@ -60,6 +60,13 @@
     # TODO(edd): Commented out now
     # test$setExtension(protobuf_unittest.optional_nested_enum_extension, 9)
 
+    ## Test nested extensions
+    checkEquals(test$getExtension(protobuf_unittest.TestNestedExtension.test),
+                NULL)
+    test$setExtension(protobuf_unittest.TestNestedExtension.test, "Hello World")
+    checkEquals(test$getExtension(protobuf_unittest.TestNestedExtension.test),
+                "Hello World")
+
     ## Test nested message extensions.
     tmp <- new( protobuf_unittest.TestAllTypes.NestedMessage )
     tmp$bb <- 3

Modified: pkg/man/P.Rd
===================================================================
--- pkg/man/P.Rd	2013-07-15 03:02:53 UTC (rev 507)
+++ pkg/man/P.Rd	2013-08-22 03:05:48 UTC (rev 508)
@@ -9,14 +9,14 @@
 P(type, file)
 }
 \arguments{
-  \item{type}{Fully qualified type name of the protocol buffer}
+  \item{type}{Fully qualified type name of the protocol buffer or extension}
   \item{file}{optional proto file. If given, the definition
   contained in the file is first registered with the 
   pool of message descriptors}
 }
 \value{
-An object of class \linkS4class{Descriptor}. 
-An error is generated otherwise.
+An object of class \linkS4class{Descriptor} for message types or
+\linkS4class{FieldDescriptor} for extensions.  An error is generated otherwise.
 }
 \author{
 Romain Francois <francoisromain at free.fr>

Modified: pkg/src/rprotobuf.h
===================================================================
--- pkg/src/rprotobuf.h	2013-07-15 03:02:53 UTC (rev 507)
+++ pkg/src/rprotobuf.h	2013-08-22 03:05:48 UTC (rev 508)
@@ -120,6 +120,7 @@
 RcppExport SEXP do_dollar_Descriptor( SEXP, SEXP ) ;
 RcppExport SEXP newProtoMessage( SEXP) ;
 RcppExport SEXP getProtobufDescriptor( SEXP ) ;
+RcppExport SEXP getExtensionDescriptor( SEXP ) ;
 RcppExport SEXP readProtoFiles( SEXP, SEXP ); 
 RcppExport Rboolean isMessage( SEXP, const char* ) ;
 RcppExport GPB::FieldDescriptor* getFieldDescriptor(GPB::Message*, SEXP) ;

Modified: pkg/src/wrapper_Message.cpp
===================================================================
--- pkg/src/wrapper_Message.cpp	2013-07-15 03:02:53 UTC (rev 507)
+++ pkg/src/wrapper_Message.cpp	2013-08-22 03:05:48 UTC (rev 508)
@@ -224,6 +224,23 @@
 	return res ;
 }
 
+/**
+ * The number of extensions the message has.
+ *
+ * @param xp external pointer to the Message
+ */
+RCPP_FUNCTION_1(int, METHOD(num_extensions), Rcpp::XPtr<GPB::Message> message){
+	const GPB::Reflection * ref = message->GetReflection() ;
+	int nexts = 0;
+	vector<const FieldDescriptor*> fields;
+	ref->ListFields(*message, &fields);
+	for (int i = 0; i < fields.size(); i++) {
+	  if (fields[i]->is_extension()) {
+	    nexts++;
+	  }
+	}
+	return nexts ;
+}
 
 /**
  * Get the message descriptor of a Message



More information about the Rprotobuf-commits mailing list