项目作者: msoucy

项目描述 :
D Protocol Buffer mixins to create structures at compile time
高级语言: D
项目地址: git://github.com/msoucy/dproto.git
创建时间: 2013-03-19T21:18:29Z
项目社区:https://github.com/msoucy/dproto

开源协议:Boost Software License 1.0

下载


D Protocol Buffers


Build Status
Coverage Status
DUB
DUB license

Protocol buffers are a language-agnostic way of specifying message structures to allow communication and serialization.

dproto is designed to enable mixing protocol buffer files into your D code at compile time.

Inspiration and a good portion of the original parser is adapted from square/protoparser


Options

dproto supports altering behavior via protobuf options:

Option Meaning Example Default
dproto_reserved_fmt The format for renaming reserved D keywords as fields. "%s_" will convert version to version_ "%s_"

Examples

Further info

Examples can be found in import/dproto/dproto.d and in examples/.

Simple Example

  1. import std.stdio;
  2. import dproto.dproto;
  3. mixin ProtocolBufferFromString!"
  4. message Person {
  5. required string name = 1;
  6. required int32 id = 2;
  7. optional string email = 3;
  8. enum PhoneType {
  9. MOBILE = 0;
  10. HOME = 1;
  11. WORK = 2;
  12. }
  13. message PhoneNumber {
  14. required string number = 1;
  15. optional PhoneType type = 2 [default = HOME];
  16. }
  17. repeated PhoneNumber phone = 4;
  18. }
  19. ";
  20. int main()
  21. {
  22. Person person;
  23. person.name = "John Doe";
  24. person.id = 1234;
  25. person.email = "jdoe@example.com";
  26. ubyte[] serializedObject = person.serialize();
  27. Person person2 = Person(serializedObject);
  28. writeln("Name: ", person2.name);
  29. writeln("E-mail: ", person2.email);
  30. return 0;
  31. }

More Complex Example

  1. import dproto.dproto;
  2. mixin ProtocolBufferFromString!"
  3. enum PhoneType {
  4. MOBILE = 0;
  5. HOME = 0;
  6. WORK = 2;
  7. }
  8. message Person {
  9. required string name = 1;
  10. required int32 id = 2;
  11. optional string email = 3;
  12. message PhoneNumber {
  13. required string number = 1;
  14. optional PhoneType type = 2 [default = HOME];
  15. }
  16. repeated PhoneNumber phone = 4;
  17. }
  18. message AddressBook {
  19. repeated Person person = 1;
  20. }
  21. ";
  22. int main()
  23. {
  24. Person t;
  25. t.name = "Max Musterman";
  26. t.id = 3;
  27. t.email = "test@example.com";
  28. Person.PhoneNumber pn1;
  29. pn1.number = "0123456789";
  30. pn1.type = PhoneType.WORK;
  31. Person.PhoneNumber pn2;
  32. pn2.number = "0123456789";
  33. t.phone = [pn1, pn2];
  34. AddressBook addressbook;
  35. addressbook.person ~= t;
  36. addressbook.person ~= t;
  37. ubyte[] serializedObject = addressbook.serialize();
  38. AddressBook addressbook2 = AddressBook(serializedObject);
  39. assert(addressbook2.person.length == 2);
  40. foreach(t2; addressbook2.person)
  41. {
  42. assert(t2.name == "Max Musterman");
  43. assert(t2.id == 3);
  44. assert(t2.email == "test@example.com");
  45. assert(t2.email !is null);
  46. assert(t2.phone[0].number == "0123456789");
  47. assert(t2.phone[0].type == PhoneType.WORK);
  48. assert(t2.phone[1].number == "0123456789");
  49. assert(t2.phone[1].type == PhoneType.HOME);
  50. assert(t2.phone[1].type == PhoneType.MOBILE);
  51. assert(t2.phone.length == 2);
  52. }
  53. version(DigitalMars)
  54. {
  55. assert(addressbook2.person[0] == addressbook.person[1]);
  56. }
  57. return 0;
  58. }

Services

Generate interfaces for service definitions.

  1. import dproto.dproto;
  2. mixin ProtocolBufferInterface!"
  3. message ServiceRequest {
  4. string request = 1;
  5. }
  6. message ServiceResponse {
  7. string response = 1;
  8. }
  9. service TestService {
  10. rpc TestMethod (ServiceRequest) returns (ServiceResponse);
  11. }
  12. ";
  13. class ServiceImplementation : TestService {
  14. ServiceResponse TestMethod(ServiceRequest input) {
  15. ServiceResponse output;
  16. output.response = "received: " ~ input.request;
  17. return output;
  18. }
  19. }