ruby-protobuf / protobuf

A pure ruby implementation of Google's Protocol Buffers
https://github.com/ruby-protobuf
MIT License
462 stars 101 forks source link

Newbie question #394

Closed kapso closed 5 years ago

kapso commented 5 years ago

My first attempt at protobuf and I am using this example - https://github.com/ruby-protobuf/protobuf/wiki/Compiling-Definitions

And I noticed that in my case the output is not /foo/user.proto but /foo/user_pb.rb, and as you can see the code is totally different as well.

Also, I am getting - LoadError (cannot load such file -- google/protobuf). Anything I am doing wrong, or just the wiki is really old?

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: foo/user.proto

require 'google/protobuf'

Google::Protobuf::DescriptorPool.generated_pool.build do
  add_file("foo/user.proto", :syntax => :proto2) do
    add_message "foo.User" do
      optional :first_name, :string, 1
      optional :last_name, :string, 2
    end
  end
end

module Foo
  User = Google::Protobuf::DescriptorPool.generated_pool.lookup("foo.User").msgclass
end

Versions -

protobuf (3.9.0)

$ protoc --version
libprotoc 3.7.0
film42 commented 5 years ago

Hey there! Google ships their own protobuf compiler for ruby in v3 (the code you pasted is from their gem). This gem will still work though. Check out the rake task helpers that ship with this gem. They should set the protoc calls up for you automatically using the correct output plugin. The underlying problem is that the ruby output plugin in protobuf v3 can not longer be overridden so we need to use a different output plugin name. Take a look at the code here: https://github.com/ruby-protobuf/protobuf/blob/master/lib/protobuf/tasks/compile.rake

If that works for you feel free to close this issue. Thanks!

kapso commented 5 years ago

@film42 that worked, thanks for the quick response.