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

Adds support to specify integer and bool proto fields as string. #253

Merged
merged 1 commit into from
Apr 14, 2015
Merged

Adds support to specify integer and bool proto fields as string. #253

merged 1 commit into from
Apr 14, 2015

Conversation

hrsht
Copy link

@hrsht hrsht commented Mar 30, 2015

Also fixes the range check for integer types.

r: @zachmargolis

@@ -17,7 +17,15 @@ def self.default
# #

def acceptable?(val)
[true, false].include?(val)
[true, false].include?(val) || (val.is_a?(String) && %w(true false).include?(val))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the class checking is not necessary, you could remove that and everything else would work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Will remove.

@zachmargolis
Copy link
Contributor

This is super helpful, thanks!

LGTM

@zachmargolis
Copy link
Contributor

Also looks like this fixes #249

Also fixes the range check for integer types.
@hrsht
Copy link
Author

hrsht commented Mar 30, 2015

Addressed comments. PTAL?

cc: @localshred

@localshred
Copy link
Contributor

Can you provide a reason why this use case should exist? I'm not convinced we should support magically coercing strings into bools or ints.

@zachmargolis
Copy link
Contributor

@localshred well one reason would be to fix the inconsistent serialization issue (#249) 😁

Another would be that we often build protobuf messages out of query params, where everything is stringified, and moving this coercion here matches our expectations as customers of this gem.

rescue
false
end

def coerce!(val)
return val.to_i if val.is_a?(Numeric)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required? (the .to_i and is_a?(Numeric) check)

we have been looking at standardizing serialization with this branch
https://github.com/localshred/protobuf/compare/bdewitt/fix_coercion

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required to convert a float to an integer, unless that should be disallowed.

Also, Integer('0x10') produces 16. IMO, it should return an error and hence requires Integer(val, 10) which throws an error if val is of type Numeric.

@hrsht
Copy link
Author

hrsht commented Apr 6, 2015

Ping!

@hrsht
Copy link
Author

hrsht commented Apr 13, 2015

Pinging again!

Please let me know if there are any additional changes to address.

@localshred
Copy link
Contributor

Sorry @hrsht, was out of town all last week. Will look into this some more this week.

abrandoned added a commit that referenced this pull request Apr 14, 2015
Adds support to specify integer and bool proto fields as string.
@abrandoned abrandoned merged commit df97317 into ruby-protobuf:master Apr 14, 2015
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.

4 participants