patch for perlxs_generator.cc

8 views
Skip to first unread message

Seth Daniel

unread,
Dec 17, 2008, 3:05:54 PM12/17/08
to protobu...@googlegroups.com
All,

I created a very simple patch for perlxs_generator.cc (attached). It adds a
'fields' method. This method returns the names of all fields defined in the
message. I need it for a project I'm working on and the API doesn't seem to
support reflection.

My perl XS skills are pretty weak so criticisms are welcome.

--
seth /\ sethdaniel.org

diff.txt

Dave Bailey

unread,
Dec 18, 2008, 3:00:57 PM12/18/08
to protobu...@googlegroups.com
Hi Seth,

Looks perfect!  I'll merge your patch tonight and create a new release with this and a couple of other things I'm due to add.

-dave

Index: src/google/protobuf/compiler/perlxs/perlxs_generator.cc
===================================================================
--- src/google/protobuf/compiler/perlxs/perlxs_generator.cc (revision 11)
+++ src/google/protobuf/compiler/perlxs/perlxs_generator.cc (working copy)
@@ -342,6 +342,10 @@
   "=item B<$length = $*value*-E<gt>length()>\n"
   "\n"
   "Returns the serialized length of C<*value*>.\n"
+   "\n"
+   "=item B<@fields = $*value*-E<gt>fields()>\n"
+   "\n"
+   "Returns the defined fields of C<*value*>.\n"
   "\n");

  // Message field accessors
@@ -1057,6 +1061,24 @@
   "    RETVAL\n"
   "\n"
   "\n");
+
+  // Fields
+
+  printer.Print(vars,
+   "int\n"
+   "$classname$::fields()\n"
+    "  PPCODE:\n"
+  );
+  for ( int i = 0; i < descriptor->field_count(); i++ ) {
+    const FieldDescriptor* field = descriptor->field(i);
+    vars["field"] = field->name();
+    printer.Print(vars,
+      "    XPUSHs(sv_2mortal(newSVpv(\"$field$\\0\",0)));\n"
+    );
+  }
+  printer.Print(vars,
+    "\n"
+    "\n");
 }




Seth Daniel

unread,
Dec 18, 2008, 3:57:21 PM12/18/08
to protobu...@googlegroups.com
On Thu, Dec 18, 2008 at 12:00:57PM -0800, Dave Bailey wrote:
> Hi Seth,
>
> Looks perfect! I'll merge your patch tonight and create a new release with
> this and a couple of other things I'm due to add.

Thanks. :o)

Can you elaborate on what you are adding?

--
seth /\ sethdaniel.org

Dave Bailey

unread,
Dec 18, 2008, 5:36:46 PM12/18/08
to protobu...@googlegroups.com
I'd like to add a to_hashref() method that will export a protobuf message (and its submessages) to a hashref.

Similarly, I'm going to modify copy_from() and merge_from() methods such that they will also work with a hashref argument.  This will make it easier to do things like construct protobuf messages out of JSON strings (or vice versa).

-dave

jza...@gmail.com

unread,
Jan 8, 2009, 2:49:14 PM1/8/09
to Protocol Buffers for Perl/XS
Does this imply that a 0.4 release is coming soon?

I'm just asking because I'm looking at spinning up a project prototype
that'd make good use of it.

Thanks,

Jeremy

On Dec 18 2008, 2:36 pm, "Dave Bailey" <d...@daveb.net> wrote:
> I'd like to add a to_hashref() method that will export a protobuf message
> (and its submessages) to a hashref.
>
> Similarly, I'm going to modify copy_from() and merge_from() methods such
> that they will also work with a hashref argument.  This will make it easier
> to do things like construct protobuf messages out of JSON strings (or vice
> versa).
>
> -dave
>

Dave Bailey

unread,
Jan 8, 2009, 4:01:41 PM1/8/09
to protobu...@googlegroups.com
Hi Jeremy,

Yes, I'm going to take care of that by the end of the week. 

-dave
Reply all
Reply to author
Forward
0 new messages