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. WIRE & Proto3
    @bquenaudon

    View full-size slide

  2. Identity if absent

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. 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

    View full-size slide

  9. syntax = "proto2";
    message MyMessage {
    required string a = 1;
    optional string b = 2;
    }
    class MyMessage(
    val a: String,
    val b: String? = null
    ) : Message() {}

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. 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;
    }

    View full-size slide

  13. 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;
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. val person: Person = anyMessage.unpack(Person.ADAPTER)
    val anyMessage: AnyMessage = AnyMessage.ADAPTER.pack(person)

    View full-size slide

  19. JSON
    • Proto2 : Wire & Protoc communication is dangerous.
    • Proto3 : All good.

    View full-size slide

  20. Proto2 㲗 Proto3
    Compatibility

    View full-size slide

  21. Proto2 㲗 Proto3 Compatibility
    • Cannot reference proto2 enums in proto3 message.
    • Anything else is good.

    View full-size slide

  22. Goodies
    package proto.package;
    import "wire/extensions.proto";
    option java_package = "java.package";
    option (wire.wire_package) = "wire.package";
    message MyMessage {}

    View full-size slide

  23. WIRE & Proto3
    FIN

    View full-size slide

  24. References
    • Protocol Buffers documentations
    • https://developers.google.com/protocol-buffers/
    • Wire documentations
    • https://square.github.io/wire/

    View full-size slide