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

Rails から Protobuf を使いたくなった話

Hazumi Ichijo
September 20, 2018

Rails から Protobuf を使いたくなった話

Hazumi Ichijo

September 20, 2018
Tweet

More Decks by Hazumi Ichijo

Other Decks in Programming

Transcript

  1. ©2018 Wantedly, Inc. 8BOUFEMZ1FPQMF  ೥ϦϦʔε  ϚΠΫϩαʔϏεʢ(P 3VCZ ʜʣ

    8BOUFEMZ7JTJU  ೥ϦϦʔε  ϞϊϦγοΫͳ3BJMTΞϓϦέʔγϣϯ
  2. ©2018 Wantedly, Inc. syntax = "proto3"; option ruby_package = "Protos::SamplePB";

    message User { // ֶྺ message Education { string school_name = 1; } // ৬ྺ message Experience { string company_name = 1; } string user_name = 1; string introduction = 2; int64 age = 3; repeated Education educations = 4; repeated Experience experiences = 5; } TBNQMFQSPUP
  3. ©2018 Wantedly, Inc. # Generated by the protocol buffer compiler.

    DO NOT EDIT! # source: protos/sample.proto require 'google/protobuf' Google::Protobuf::DescriptorPool.generated_pool.build do add_message "User" do optional :user_name, :string, 1 optional :introduction, :string, 2 optional :age, :int64, 3 repeated :educations, :message, 4, "User.Education" repeated :experiences, :message, 5, "User.Experience" end add_message "User.Education" do optional :school_name, :string, 1 end add_message "User.Experience" do optional :company_name, :string, 1 end end module Protos::SamplePB User = Google::Protobuf::DescriptorPool.generated_pool.lookup("User").msgclass User::Education = Google::Protobuf::DescriptorPool.generated_pool.lookup("User.Education").msgclass User::Experience = Google::Protobuf::DescriptorPool.generated_pool.lookup("User.Experience").msgclass end TBNQMFQCSC
  4. ©2018 Wantedly, Inc. def new_sample educations = [ Protos::SamplePB::User::Education.new( school_name:

    "۴࿏ߴઐ" ), Protos::SamplePB::User::Education.new( school_name: "ஜ೾େֶ" ), ] experiences = [ Protos::SamplePB::User::Experience.new( company_name: "΢ΥϯςουϦʔגࣜձࣾ" ), ] Protos::SamplePB::User.new( user_name: 'rerost', introduction: 'δϟεί͔Β110km', educations: educations, experiences: experiences, ) end sample = new_sample puts Protos::SamplePB::User.encode_json(sample) { "userName": "rerost", "introduction": "δϟεί͔Β110km", "educations": [ { "schoolName": "۴࿏ߴઐ" }, { "schoolName": "ஜ೾େֶ" } ], "experiences": [ { "companyName": "΢ΥϯςουϦʔגࣜձࣾ" } ] }
  5. ©2018 Wantedly, Inc. def new_sample Protos::SamplePB::User.new( user_name: 1, # Τϥʔ

    user_nameͷܕ͕ҧ͏ͷͰ age: 23, introduction: 'δϟεί͔Β110km', educations: [], experiences: [], ) end sample = new_sample ҧ͏ܕΛೖΕͨ৔߹ʹ͍ͭͯ
  6. ©2018 Wantedly, Inc. def new_sample Protos::SamplePB::User.new( age: 23, ) end

    sample = new_sample encoded = Protos::SamplePB::User.encode(sample) puts encoded.class # String decoded = Protos::SamplePB::User.decode(encoded) puts decoded.age # 23 CJOBSZ΁ͷγϦΞϥΠζσγϦΞϥΠζ
  7. ©2018 Wantedly, Inc. def new_sample Protos::SamplePB::User.new( age: 23, ) end

    sample = new_sample encoded = Protos::SamplePB::User.encode_json(sample) puts encoded.class # String puts encoded # {"age":23,"educations":[],"experiences":[]} decoded = Protos::SamplePB::User.decode_json(encoded) puts decoded.age # 23 +40/΁ͷγϦΞϥΠζσγϦΞϥΠζ