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

Fail initialization if an unknown attribute is present #248

Open
brianstien opened this issue Feb 24, 2015 · 2 comments
Open

Fail initialization if an unknown attribute is present #248

brianstien opened this issue Feb 24, 2015 · 2 comments

Comments

@brianstien
Copy link
Contributor

When initializing a protobuf message object with a hash of attributes, is it possible to fail if an attribute is not a field on the underlying message?

class Foo < Protobuf::Message
  optional :string, :hello, 1
end

# This is normal use
Foo.new(:hello => "world")

# This could fail with an UnknownAttributeError, but does not
Foo.new(:some_random_attribute_that_doesnt_exist => "foobar")
@localshred
Copy link
Contributor

Unsure if you're asking a question or proposing a change...

No, we don't fail when you provide a bogus field on hash initialization. When decoding bytes we don't fail on unknown fields (for obvious reasons) and I believe that behavior has transferred over to initialization with a hash.

@abrandoned
Copy link
Contributor

Not sure if we should "fail"; I looked through the Java and C implementations and it looks like they store unknown attributes (fields) in a map (UnknownFieldSet in C, FieldSet in Java) and allow introspection on the fields that are unknown through methods like is_empty?(), field_count()

if those implementation don't blow-up when unknown fields are encountered then it seems we should follow that path

they do not however generate the assignment methods to assign the value, but during initialization it doesn't look like they stop execution

https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.unknown_field_set#UnknownFieldSet

will do some more reading and see if anything else stands 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

3 participants