Convert java class to proto message. Converting list with abstract or interface types.

1,249 views
Skip to first unread message

Sergey K

unread,
Jan 22, 2016, 12:54:44 PM1/22/16
to Protocol Buffers
Hi
How can I convert a ChatMessage to a proto message?

//Need covert to proto message
public class ChatMessage {
private String message;
private List<Extension> extensions;
}

interface Extension {
getName();
}

public abstract class AbstractExtension implement Extension {
   
   private String name;   
public String getName() { return name;}
}
    
public class HelpExtension extends AbstractExtension  {
private String hint;
}

public class AlertExtension extends AbstractExtension {
private String alert;
}

... and more implemented Extension class.


Josh Haberman

unread,
Jan 22, 2016, 6:43:12 PM1/22/16
to Protocol Buffers
Hi Sergey,

Protocol Buffers have an extension capability which is similar to what you are describing. The way to describe this in a .proto file would be something like:

syntax = "proto2";

message ChatMessage {
  optional string message = 1;
  extensions 100 to 200;
}

// These definitions can be in the same .proto file or a different one.

extend ChatMessage {
  optional string hint = 100;
}

extend ChatMessage {
  optional string alert = 101;
}

You can read more about extensions here:



Josh
Reply all
Reply to author
Forward
0 new messages