Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Benoît Quenaudon

November 04, 2020
Tweet

More Decks by Benoît Quenaudon

Other Decks in Programming

Transcript

  1. 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
  2. syntax = "proto2"; message MyMessage { required string a =

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

    optional string b = 2; } class MyMessage( val a: String = "", val b: String? = null ) : Message<Message, Builder>() {} →
  4. 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.
  5. 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; }
  6. 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; } ↓
  7. 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.
  8. 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.
  9. 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 } }
  10. 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 } }