Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that referenced classes contain full namespace. #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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