msoucy / dproto

D Protocol Buffer mixins to create structures at compile time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

option to convert from a D struct to/from a Proto when this makes sense

timotheecour opened this issue · comments

would be nice to support conversions, when possible:

eg:

Person1 proto;
Person2 s;

proto.deserialize("...");
s.readFrom(proto);

Person1 proto2;
proto2.readFrom(s);
assert(proto2==proto);
message Person1 {
      required string name = 1;
      required int32 id = 2;
      optional string email = 3;

      enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
      }

      message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];
      }

      repeated PhoneNumber phone = 4;
    }
struct Person2{
    string name;
    int d;
    string email;

      enum PhoneType {
        MOBILE = 0,
        HOME = 1,
        WORK = 2,
      }

      struct PhoneNumber {
        string number;
        PhoneType type = PhoneType.HOME;
      }
      PhoneNumber[] phone;
    }
}

I'm a little confused about the use case - part of the point of protocol buffers is that you don't have multiple copies of the data classes to maintain. Is this something you run into regularly?

it's very common: proto's are by design limited (no methods although that's a mooter point in D thanks to UFCS, no pointers / self referencing etc), so are often used mostly for serialization/deserialization to disk and RPC's. So in languages like C++ you have to write the conversion from native data structure to proto. But in D it can be done automatically thanks to CT reflection. That's what msgpack and orange and other serialization packages do.