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 Slide

  2. View Slide

  3. View Slide

  4. Identity if absent

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  13. New Types

    View 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 Slide

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

    View Slide

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

    View Slide

  17. 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 Slide

  18. 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 Slide

  19. 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 Slide

  20. 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 Slide

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

    View Slide

  22. JSON

    View Slide

  23. JSON?

    View Slide

  24. Why JSON?

    View Slide

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

    View Slide

  26. Proto2 㲗 Proto3
    Compatibility

    View Slide

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

    View Slide

  28. Goodies

    View Slide

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

    View Slide

  30. WIRE & Proto3
    FIN

    View Slide

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

    View Slide