Share
Embed
Protobuf on Rails Tips
Hazumi Ichijo
July 31, 2019
Transcript
20 slides
-
©2019 Wantedly, Inc. Protobuf on Rails Tips Meguro.rb #29 Feb
22, 2019 - Hazumi Ichijo -
©2018 Wantedly, Inc. ࠷ݶཉ͍͠ޓੑɹ ΧϥϜͷՃ ໊લͷมߋ 1SPUPCVGͳΒɾɾɾ
σγϦΞϥΠζෆՄͳΧϥϜແࢹ ໊લͰͳ͘൪߸Ͱࣝผ όΠφϦͳΒ -
©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; } } -
©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\":\"Ұᑍ\"}" -
©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 -
©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; } } -
©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!͢Δͱࣦഊ͢Δ -
©2019 Wantedly, Inc. https://github.com/ruby-protobuf/protobuf/issues/381# grpc_root = File.join(config.root, "/lib/ruby_proto") $LOAD_PATH.unshift(grpc_root) Dir[File.join(grpc_root,
"**/*.rb")].each do |file| require file end Reload!͢Δͱࣦഊ͢Δ -
©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ͷόʔδϣϯ -
©2019 Wantedly, Inc. protocͷόʔδϣϯཧ protoc: version: 3.7.0 lint: group: uber2
generate: plugins: - name: ruby output: ./app -
©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; ... } -
©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