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

Protocol Buffers @potatochips#40

Protocol Buffers @potatochips#40

TakuSemba

May 24, 2017
Tweet

More Decks by TakuSemba

Other Decks in Technology

Transcript

  1. Protocol Buffers json Protocol buffers \ QSFGFDUVSFT< \ JE OBNF๺ւಓ

    SPNBKJIPLLBJEP ^ \ JE OBNF੨৿ݝ SPNBKJBPNPSJ ^ \ ʜ \ JE OBNFࣛࣇౡݝ SPNBKJLBHPTIJNB ^ \ JE OBNFԭೄݝ SPNBKJPLJOBXB ^ > ^   ʜ  
  2. How to use define proto file ɾ ɾ generate compile

    / decompile file ɾ make an API call
  3. 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
  4. 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
  5. 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
  6. How to use define proto file ɾ ɾ generate compile

    / decompile file ɾ make an API call
  7. public final class Prefecture extends Message<Prefecture, Prefecture.Builder> { public final

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

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

    / decompile file ɾ make an API call
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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