Skip to main content
Question Protected by gnat
edited tags
Link
Doug T.
  • 11.7k
  • 5
  • 47
  • 69
Tweeted twitter.com/#!/StackProgrammer/status/259987836533628928
Code details, code formatting
Source Link
Apoorv
  • 1.1k
  • 1
  • 6
  • 15
  1. Represent messages in .proto files, package them as a separate jar, and ship it to target consumers of the service --which is basically the default approach I guess.

  2. Do the same but also include hand crafted wrappers (not sub-classes!) around each message that implement a contract supporting at least these two methods (T is the wrapper class, V is the message class (using generics but simplified syntax for brevity):

    public V toProtobufMessage() { //Build V from T }

    public static T fromProtobufMessage(V message_) { //Build and return T from V }

     public V toProtobufMessage() {
         V.Builder builder = V.newBuilder();
         for (Item item : getItemList()) {
             builder.addItem(item);
         }
         return builder.setAmountPayable(getAmountPayable()).
                        setShippingAddress(getShippingAddress()).
                        build();
     }
    
     public static T fromProtobufMessage(V message_) { 
         return new T(message_.getShippingAddress(), 
                      message_.getItemList(),
                      message_.getAmountPayable());
     }
    
message ItemList {
    repeated item = 1;
}

message CustomerInvoice {
    required ShippingAddress address = 1;
    required ItemList = 2;
    required double amountamountPayable = 3;
}
message CustomerInvoice {
    required ShippingAddress address = 1;
    repeated Item item = 2;
    required double amountamountPayable = 3;
}
  1. Represent messages in .proto files, package them as a separate jar, and ship it to target consumers of the service --which is basically the default approach I guess.

  2. Do the same but also include hand crafted wrappers (not sub-classes!) around each message that implement a contract supporting at least these two methods (T is the wrapper class, V is the message class (using generics but simplified syntax for brevity):

    public V toProtobufMessage() { //Build V from T }

    public static T fromProtobufMessage(V message_) { //Build and return T from V }

message ItemList {
    repeated item = 1;
}

message CustomerInvoice {
    required ShippingAddress address = 1;
    required ItemList = 2;
    required double amount = 3;
}
message CustomerInvoice {
    required ShippingAddress address = 1;
    repeated Item item = 2;
    required double amount = 3;
}
  1. Represent messages in .proto files, package them as a separate jar, and ship it to target consumers of the service --which is basically the default approach I guess.

  2. Do the same but also include hand crafted wrappers (not sub-classes!) around each message that implement a contract supporting at least these two methods (T is the wrapper class, V is the message class (using generics but simplified syntax for brevity):

     public V toProtobufMessage() {
         V.Builder builder = V.newBuilder();
         for (Item item : getItemList()) {
             builder.addItem(item);
         }
         return builder.setAmountPayable(getAmountPayable()).
                        setShippingAddress(getShippingAddress()).
                        build();
     }
    
     public static T fromProtobufMessage(V message_) { 
         return new T(message_.getShippingAddress(), 
                      message_.getItemList(),
                      message_.getAmountPayable());
     }
    
message ItemList {
    repeated item = 1;
}

message CustomerInvoice {
    required ShippingAddress address = 1;
    required ItemList = 2;
    required double amountPayable = 3;
}
message CustomerInvoice {
    required ShippingAddress address = 1;
    repeated Item item = 2;
    required double amountPayable = 3;
}
Source Link
Apoorv
  • 1.1k
  • 1
  • 6
  • 15
Loading