Mapper for protocol buffer entities

726 views
Skip to first unread message

prateek sharma

unread,
Aug 8, 2013, 8:50:51 AM8/8/13
to prot...@googlegroups.com
Hi all,
I am new to buffers, using it with my java project.
I have one doubt about initializing proto buffer entities.

Initializing this proto buffer entity is done manually in all the examples given in tutorial, examples used setters to set the values directly to proto buffer entities but what when I have a base entity which I want to convert to proto buffer entity? Do I need to take values from the base entity and initialize proto buffer entity or there are any mapper available to do this task?

For example:
In my project if I have a base entity vehicle which has parameters as
id        int
name  String
type    int

I wrote a .proto file vehicle.proto as
message vehicle_proto{
     required int32 id = 1; // Unique ID number for this person.
     required string name = 2;
     optional string type = 3;
}

It created a file VehicleProto.java.

Now I have one entity vehicle and one entity VehicleProto and want to map vehicle to VehicleProto. How can I do it automatically?

Thanks,
Pratz

prateek sharma

unread,
Aug 9, 2013, 1:36:16 AM8/9/13
to prot...@googlegroups.com
Is it something that I did not get properly w.r.t. mapper or is my question invalid?

Thanks,
Pratz

Ilia Mirkin

unread,
Aug 9, 2013, 1:49:28 AM8/9/13
to prateek sharma, prot...@googlegroups.com
Is your basic question "is there an easy way to convert between a type
I created and a vastly similar type created by protoc"? AFAIK the
answer is no, definitely not in the core protobuf library. However it
shouldn't be too difficult to create something that handles most cases
with reflection. (And if you run into a reference loop, throw an
error, since that's fundamentally non-encodable using protobuf.)
However note that Java reflection is slow, and protobuf is often used
in applications where this stuff needs to be fast. Furthermore your
internal models invariably diverge from the protobuf stuff, and so you
end up with a bunch of fromProto/toProto functions anyways...
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+u...@googlegroups.com.
> To post to this group, send email to prot...@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

prateek sharma

unread,
Aug 9, 2013, 3:03:01 AM8/9/13
to Ilia Mirkin, prot...@googlegroups.com
Hi Ilia,
Yes you got my question correct. 
So do you mean that I should fill the proto buffer object manually(if not by reflection) using setters provided in proto buffer builder?

Thanks,
Pratz


moofish

unread,
Aug 9, 2013, 8:42:19 PM8/9/13
to prot...@googlegroups.com
You might look at protostuff. Protobuf does not due a mapping and they are pretty clear in the docs that PB is not a first class citizen for object orientated design. However there are many instances where you can implement some meta programming behavior to map objects because you can interrogate all the PB field and message data.
-m

prateek sharma

unread,
Aug 9, 2013, 10:48:19 PM8/9/13
to moofish, prot...@googlegroups.com

Protostuff looks promising. Let me give it a try.

Thanks,
Pratz

On Aug 10, 2013 6:12 AM, "moofish" <moof...@gmail.com> wrote:
You might look at protostuff. Protobuf does not due a mapping and they are pretty clear in the docs that PB is not a first class citizen for object orientated design. However there are many instances where you can implement some meta programming behavior to map objects because you can interrogate all the PB field and message data.
-m

--
You received this message because you are subscribed to a topic in the Google Groups "Protocol Buffers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/protobuf/-9QGZGVmUFE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to protobuf+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages