Skip to content

Conversation

caomania
Copy link

@caomania caomania commented Aug 15, 2016

This patch adds a complete namespace to classes referenced by the generated Protobuf ruby classes.

eg. Protobuf::Test::Beta vs Beta

Referencing the class with its full namespace is needed when using autoloading and the implementers code contains a pre-existing class with the same name as the class referenced within the protobuf.

Here's a very basic example.

# app/models/protobuf/test/alpha.rb
require "beefcake"

module Protobuf
  module Test
    class Alpha
      include Beefcake::Message
    end

    class Alpha
      required :id, :int32, 1
      optional :beta, Beta, 2
    end
  end
end
# app/models/protobuf/test/beta.rb
require "beefcake"

module Protobuf
  module Test

    class Beta
      include Beefcake::Message
    end

    class Beta
      required :id, :int32, 1
    end
  end
end
# app/models/beta.rb
class Beta
 # no op
end

If the Protobuf references another Protobuf class which has the same name as an existing class, load order takes precedence. Even when the existing class is in another namespace. This leads to the Protobuf referencing Beta vs Protobuf::Test::Beta

2.3.1 :001 > Beta
 => Beta
2.3.1 :002 > Protobuf::Test::Alpha.fields
 => {1=>#<struct Beefcake::Message::Field rule=:required, name=:id, type=:int32, fn=1, opts={}>, 2=>#<struct Beefcake::Message::Field rule=:optional, name=:beta, type=Beta, fn=2, opts={}>}

When other classes are not loaded first, the Protobuf references the correct class.

2.3.1 :001 > Protobuf::Test::Alpha.fields
 => {1=>#<struct Beefcake::Message::Field rule=:required, name=:id, type=:int32, fn=1, opts={}>, 2=>#<struct Beefcake::Message::Field rule=:optional, name=:beta, type=Protobuf::Test::Beta, fn=2, opts={}>}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant