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

downcasting of values during field assignment #251

Open
skippy opened this issue Feb 27, 2015 · 0 comments
Open

downcasting of values during field assignment #251

skippy opened this issue Feb 27, 2015 · 0 comments

Comments

@skippy
Copy link
Contributor

skippy commented Feb 27, 2015

hello,

I'm noticing that if I assign an object that inherits from the ruby class which is created from the protocol definition, it is downcast, and I don't see any particular reason why. For example

Lets say I have the following classes. (The reason I'm doing this is I'm injecting a ton of validations into the new class; the other way to do this is to not have Corp::Location inherit from the proto-based ruby object but instead just serialize into it.... such as Corp::Location#to_proto calls Corp::WireDefinition::Location.new(self.to_hash).to_proto... thoughts on approach?)

Anyway, I digress a bit... check this out:

module Corp
  class Location < Corp::WireDefinition::Location
    #a ton of validations
  end
end

module Corp
  class Business < Corp::WireDefinition::Business
    # includes an array of Locations
  end
end

if I run the following, on assignment, the Corp::Location instance is downcast. This is problematic if I have methods within Corp::Location that I need to call later (such as running validations).

business = Corp::Business.new
business.locations = [Corp.Location.new, Corp.Location.new]
business.locations.first.is_a?(Corp.Location) # => false; it is a Corp::WireDefinition::Location

The culprit is this line: https://github.com/localshred/protobuf/blob/master/lib/protobuf/field/field_array.rb#L70

I don't understand why it is creating a new object here, downcasting it in the process, and git blame doesn't shed any light as to why this logic is here. Thoughts? Would it be ok if I submit a PR that rips this out?

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

No branches or pull requests

1 participant