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

Protobuf on Rails Tips

Protobuf on Rails Tips

Hazumi Ichijo

July 31, 2019
Tweet

More Decks by Hazumi Ichijo

Other Decks in Programming

Transcript

  1. ©2018 Wantedly, Inc. ࠷௿ݶཉ͍͠ޓ׵ੑɹ  ΧϥϜͷ௥Ճ  ໊લͷมߋ 1SPUPCVGͳΒɾɾɾ 

    σγϦΞϥΠζෆՄͳΧϥϜ͸ແࢹ  ໊લͰͳ͘൪߸Ͱࣝผ όΠφϦͳΒ
  2. ©2019 Wantedly, Inc. syntax = "proto3"; option ruby_package = "SamplePB";

    message User { string user_name = 1; string introduction = 2; int64 age = 3; repeated Education educations = 4; repeated Experience experiences = 5; // ֶྺ message Education { string school_name = 1; } // ৬ྺ message Experience { string company_name = 1; } }
  3. ©2019 Wantedly, Inc. user = SamplePB::User.new(user_name: "Ұᑍ୺੅") # <SamplePB::User: user_name:

    "Ұᑍ୺੅", introduction: "", age: 0, educations: [], experiences: []> encoded = SamplePB::User.encode(user) # "\n\f\xE4\xB8\x80\xE6\xA2\x9D\xE7\xAB\xAF\xE6\xBE\x84" SamplePB::User.decode(decoded) # <SamplePB::User: user_name: "Ұᑍ୺੅", introduction: "", age: 0, educations: [], experiences: []> user.to_json # "{\"userName\":\"Ұᑍ୺੅\"}"
  4. ©2019 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 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
  5. ©2018 Wantedly, Inc. ϝϦοτ w εΩʔϚʹؚ·Εͳ͍
 ϑΟʔϧυ͸ແࢹ w ໊લͷมߋ͕λάφϯόʔͰ
 ࣝผ

    όΠφϦͳΒ  w αΠζ͕খ͘͞ͳΔ
 ʢ31$ͳΒ͔ͳΓخ͍͠ʣ syntax = "proto3"; option ruby_package = "Protos::SamplePB"; message User { string user_name = 1; string introduction = 2; int64 age = 3; repeated Education educations = 4; repeated Experience experiences = 5; // ֶྺ message Education { string school_name = 1; } // ৬ྺ message Experience { string company_name = 1; } }
  6. ©2019 Wantedly, Inc. irb(main):001:0> SamplePB::User => SamplePB::User irb(main):002:0> SamplePB::User =>

    SamplePB::User irb(main):003:0> reload! Reloading... => true irb(main):004:0> SamplePB::User Traceback (most recent call last): 4: from (irb):4 3: from app/protos/sample_pb.rb:6:in `<top (required)>' 2: from app/protos/sample_pb.rb:6:in `build' 1: from app/protos/sample_pb.rb:6:in `finalize_to_pool' RuntimeError (Unable to add defs to DescriptorPool: Symtab already has a def named 'User') Reload!͢Δͱࣦഊ͢Δ໰୊
  7. ©2019 Wantedly, Inc. protocͷόʔδϣϯ؅ཧ Running via Spring preloader in process

    83352 Loading development environment (Rails 5.1.7) irb(main):001:0> SamplePB Traceback (most recent call last): 5: from (irb):1 4: from app/protos/sample_pb.rb:6:in `<top (required)>' 3: from app/protos/sample_pb.rb:6:in `build' 2: from app/protos/sample_pb.rb:6:in `instance_eval' 1: from app/protos/sample_pb.rb:7:in `block in <top (required)>' NoMethodError (undefined method `add_file' for #<Google::Protobuf::Internal::Builder:0x00007ff4648e94b8>) Did you mean? add_enum w 1SPUPDͷόʔδϣϯ w HPPHMFQSPUPCVGͷόʔδϣϯ  
  8. ©2019 Wantedly, Inc. ศརͳWell-Known Types syntax = "proto3"; option ruby_package

    = "SamplePB"; import "google/protobuf/timestamp.proto"; import "google/protobuf/wrappers.proto"; message User { ... google.protobuf.Timestamp created_at = 6; google.protobuf.BoolValue accepted = 7; ... }
  9. ©2019 Wantedly, Inc. irb(main):020:0> SamplePB::User.new(created_at: Time.now, accepted: Google::Protobuf::BoolValue.new(value: true)).to_json =>

    "{\"createdAt\":\"2019-07-30T17:44:28.722279000Z\",\"accepted\":true}" ศརͳWell-Known Types