dproto icon indicating copy to clipboard operation
dproto copied to clipboard

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

Open timotheecour opened this issue 8 years ago • 2 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;
    }
}

timotheecour avatar Mar 08 '16 05:03 timotheecour

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?

msoucy avatar Mar 08 '16 11:03 msoucy

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.

timotheecour avatar Apr 08 '16 07:04 timotheecour