Wire & Proto3

Wire & Proto3

Starting in 3.3.0 Wire supports Proto3. What changes with Proto3? How does Wire make it comfortable to use? Let’s see.

05162bc961c3654218bf1839974a4f35?s=128

Benoît Quenaudon

November 04, 2020
Tweet

Transcript

  1. WIRE & Proto3 @bquenaudon

  2. None
  3. None
  4. Identity if absent

  5. syntax = "proto2"; message Message { required string a =

    1; optional string b = 2; }
  6. syntax = "proto2"; message Message { required string a =

    1; optional string b = 2; }
  7. syntax = "proto3"; message Message { string a = 1;

    }
  8. syntax = "proto3"; message Message { string a = 1;

    optional string b = 2; }
  9. syntax = "proto3"; message Message { string a = 1;

    optional string b = 2; }
  10. Identity if absent Strings → "" Bytes → empty bytes

    Bools → false Numerics → 0, 0L, 0f, 0.0 Enums → first defined value (which must be 0) Messages → Null
  11. syntax = "proto2"; message MyMessage { required string a =

    1; optional string b = 2; } class MyMessage( val a: String, val b: String? = null ) : Message<Message, Builder>() {} →
  12. syntax = "proto3"; message MyMessage { string a = 1;

    optional string b = 2; } class MyMessage( val a: String = "", val b: String? = null ) : Message<Message, Builder>() {} →
  13. New Types

  14. Any → com.squareup.wire.AnyMessage Duration → java.time.Duration Timestamp → java.time.Instant Struct

    → map<String, *> Wrappers → Boxed types for primitives (String?) Empty → kotlin.Unit New Types google.protobuf.
  15. message Request { // Only set when it is a

    Bitcoin request. optional BitcoinData bitcoin_data = 1; // Only set when it is a Stock request. optional StockData stock_data = 2; }
  16. message Request { // Only set when it is a

    Bitcoin request. optional BitcoinData bitcoin_data = 1; // Only set when it is a Stock request. optional StockData stock_data = 2; } message Request { // Will be either BitcoinData or StockData. optional google.protobuf.Any request_data = 1; } ↓
  17. Any → com.squareup.wire.AnyMessage Duration → java.time.Duration Timestamp → java.time.Instant Struct

    → map<String, *> Wrappers → Boxed types for primitives (String?) Empty → kotlin.Unit New Types google.protobuf.
  18. Any → com.squareup.wire.AnyMessage Duration → java.time.Duration Timestamp → java.time.Instant Struct

    → map<String, *> Wrappers → Boxed types for primitives (String?) Empty → kotlin.Unit New Types google.protobuf.
  19. class AnyMessage( val typeUrl: String, val value: ByteString = ByteString.EMPTY

    ) : Message<AnyMessage, Nothing>(ADAPTER, ByteString.EMPTY) { fun <T> unpack(adapter: ProtoAdapter<T>): T fun <T> unpackOrNull(adapter: ProtoAdapter<T>): T? companion object { fun pack(message: Message<*, *>): AnyMessage } }
  20. class AnyMessage( val typeUrl: String, val value: ByteString = ByteString.EMPTY

    ) : Message<AnyMessage, Nothing>(ADAPTER, ByteString.EMPTY) { fun <T> unpack(adapter: ProtoAdapter<T>): T fun <T> unpackOrNull(adapter: ProtoAdapter<T>): T? companion object { fun pack(message: Message<*, *>): AnyMessage } }
  21. val person: Person = anyMessage.unpack(Person.ADAPTER) val anyMessage: AnyMessage = AnyMessage.ADAPTER.pack(person)

  22. JSON

  23. JSON?

  24. Why JSON?

  25. JSON • Proto2 : Wire & Protoc communication is dangerous.

    • Proto3 : All good.
  26. Proto2 㲗 Proto3 Compatibility

  27. Proto2 㲗 Proto3 Compatibility • Cannot reference proto2 enums in

    proto3 message. • Anything else is good.
  28. Goodies

  29. Goodies package proto.package; import "wire/extensions.proto"; option java_package = "java.package"; option

    (wire.wire_package) = "wire.package"; message MyMessage {}
  30. WIRE & Proto3 FIN

  31. References • Protocol Buffers documentations • https://developers.google.com/protocol-buffers/ • Wire documentations

    • https://square.github.io/wire/