Protocol Buffers @potatochips#40

Protocol Buffers @potatochips#40

A374f41eab3f73c50d8bab0652cb207a?s=128

TakuSemba

May 24, 2017
Tweet

Transcript

  1. Taku Semba Protocol Buffers CyberAgent,Inc

  2. About Me Taku Semba TakuSemba @takusemba AbemaTV.Inc

  3. Protocol Buffers language-neutral, platform-neutral extensible mechanism for serializing structured data

    ɾ ɾ faster than Json ɾ binary based data transmission
  4. Protocol Buffers json Protocol buffers \ QSFGFDUVSFT< \ JE OBNF๺ւಓ

    SPNBKJIPLLBJEP ^ \ JE OBNF੨৿ݝ SPNBKJBPNPSJ ^ \ ʜ \ JE OBNFࣛࣇౡݝ SPNBKJLBHPTIJNB ^ \ JE OBNFԭೄݝ SPNBKJPLJOBXB ^ > ^   ʜ  
  5. Quick Start

  6. How to use define proto file ɾ ɾ generate compile

    / decompile file ɾ make an API call
  7. define proto file ɾ ɾ generate compile / decompile file

    ɾ make an API call How to use
  8. syntax = "proto3"; option java_package = "com.your.package.name"; option java_outer_classname =

    "Prefectures"; option go_package = "proto"; package prefectures; message Prefecture { int64 id = 1; // ID string name = 2; // name string romaji = 3; // romaji } message GetPrefecturesResponse { repeated Prefecture prefectures = 1; } define proto file
  9. syntax = "proto3"; option java_package = "com.your.package.name"; option java_outer_classname =

    "Prefectures"; option go_package = "proto"; package prefectures; message Prefecture { int64 id = 1; // ID string name = 2; // name string romaji = 3; // romaji } message GetPrefecturesResponse { repeated Prefecture prefectures = 1; } define proto file
  10. syntax = "proto3"; option java_package = "com.your.package.name"; option java_outer_classname =

    "Prefectures"; option go_package = "proto"; package prefectures; message Prefecture { int64 id = 1; // ID string name = 2; // name string romaji = 3; // romaji } message GetPrefecturesResponse { repeated Prefecture prefectures = 1; } define proto file
  11. How to use define proto file ɾ ɾ generate compile

    / decompile file ɾ make an API call
  12. wire: square੡ͷprotocol buffers༻ͷϥΠϒϥϦ java -jar wire-compiler-VERSION-jar-with-dependencies.jar \ --proto_path=your/proto/path \ --java_out=your/output/path

    ˈ generate compile / decompile file
  13. generate compile / decompile file

  14. public final class Prefecture extends Message<Prefecture, Prefecture.Builder> { public final

    class GetPrefecturesResponse extends Message<GetPrefecturesResponse, GetPrefecturesResponse.Builder> { ɾɾɾ } ɾɾɾ } generate compile / decompile file
  15. public final class Prefecture extends Message<Prefecture, Prefecture.Builder> { public final

    class GetPrefecturesResponse extends Message<GetPrefecturesResponse, GetPrefecturesResponse.Builder> { ɾɾɾ } ɾɾɾ } generate compile / decompile file
  16. How to use define proto file ɾ ɾ generate compile

    / decompile file ɾ make an API call
  17. dependencies { // wire for retrofit compile ‘com.squareup.retrofit2:converter-wire:x.x.x' // wire

    compile ‘com.squareup.wire:wire-runtime:x.x.x’ // protocol buffers compile 'com.google.protobuf:protobuf-java:x.x.x' } make an API call
  18. interface Service { @GET("prefectures") fun getProtoPrefectures(): Single<GetPrefecturesResponse> } private val

    retrofit: Retrofit = Retrofit .Builder() .baseUrl(endpoint) .client(okHttpClient) .addConverterFactory(WireConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() make an API call
  19. interface Service { @GET("prefectures") fun getProtoPrefectures(): Single<GetPrefecturesResponse> } private val

    retrofit: Retrofit = Retrofit .Builder() .baseUrl(endpoint) .client(okHttpClient) .addConverterFactory(WireConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() make an API call
  20. interface Service { @GET("prefectures") fun getProtoPrefectures(): Single<GetPrefecturesResponse> } private val

    retrofit: Retrofit = Retrofit .Builder() .baseUrl(endpoint) .client(okHttpClient) .addConverterFactory(WireConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() make an API call
  21. fun getPrefectures(): Single<List<Prefecture>> { return retrofit .create(Service::class.java) .getProtoPrefectures() .map(GetPrefecturesResponse::prefectures) .map

    { val prefectures: ArrayList<Prefecture> = ArrayList() it.mapTo(prefectures) { value -> Prefecture().apply { id = value.id name = value.name romaji = value.romaji } } prefectures.toList() } } make an API call
  22. fun getPrefectures(): Single<List<Prefecture>> { return retrofit .create(Service::class.java) .getProtoPrefectures() .map(GetPrefecturesResponse::prefectures) .map

    { val prefectures: ArrayList<Prefecture> = ArrayList() it.mapTo(prefectures) { value -> Prefecture().apply { id = value.id name = value.name romaji = value.romaji } } prefectures.toList() } } make an API call
  23. fun getPrefectures(): Single<List<Prefecture>> { return retrofit .create(Service::class.java) .getProtoPrefectures() .map(GetPrefecturesResponse::prefectures) .map

    { val prefectures: ArrayList<Prefecture> = ArrayList() it.mapTo(prefectures) { value -> Prefecture().apply { id = value.id name = value.name romaji = value.romaji } } prefectures.toList() } } make an API call
  24. fast because of small data proto file could be an

    API document Conclusion pros hard to debug take time to make an api change Cons
  25. Protocol Buffers https://github.com/takusemba https://twitter.com/takusemba