Skip to content

pull in acceptable? optimization for varint and fix rubocop for previ… #356

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

Merged
merged 2 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class File < ::Protobuf::Message; end



##
# File Options
#
set_option :java_package, "com.google.protobuf.compiler"
set_option :java_outer_classname, "PluginProtos"


##
# Message Fields
#
Expand Down
1 change: 0 additions & 1 deletion lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ class Location
repeated ::Google::Protobuf::SourceCodeInfo::Location, :location, 1
end


end

end
Expand Down
18 changes: 10 additions & 8 deletions lib/protobuf/field/bool_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
module Protobuf
module Field
class BoolField < VarintField
FALSE_ENCODE = [0].pack('C')
FALSE_STRING = "false".freeze
TRUE_ENCODE = [1].pack('C')
TRUE_STRING = "true".freeze

##
Expand All @@ -19,24 +21,24 @@ def self.default
# #

def acceptable?(val)
[true, false].include?(val) || %w(true false).include?(val)
val == true || val == false || val == TRUE_STRING || val == FALSE_STRING
end

def coerce!(val)
case val
when String
val == TRUE_STRING
else
val
end
return true if val == true
return false if val == false
return true if val == TRUE_STRING
return false if val == FALSE_STRING

val
end

def decode(value)
value == 1
end

def encode(value)
[value ? 1 : 0].pack('C')
value ? TRUE_ENCODE : FALSE_ENCODE
end

private
Expand Down
9 changes: 7 additions & 2 deletions lib/protobuf/field/varint_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ def self.encode(value, use_cache = true)
##
# Public Instance Methods
#

def acceptable?(val)
int_val = coerce!(val)
int_val = if val.is_a?(Integer)
return true if val >= 0 && val < INT32_MAX # return quickly for smallest integer size, hot code path
val
else
coerce!(val)
end

int_val >= self.class.min && int_val <= self.class.max
rescue
false
Expand Down
24 changes: 19 additions & 5 deletions spec/lib/protobuf/generators/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ def compile
end

it 'serializes messages' do
output_string = <<-STRING
{ :foo => "space",
:bar => [{
:foo => "station",
:bar => { :foo => "orbit" },
:boom => 123,
:goat => ::MyEnum::FOO,
:bam => false,
:fire => 3.5 }],
:boom => 456,
:goat => ::MyEnum::BOO,
:bam => true, :fire => 1.2 }
STRING

output_string.lstrip!
output_string.rstrip!
output_string.delete!("\n")
output_string.squeeze!(" ")
expect(generator.serialize_value(MyMessage3.new(
:foo => 'space',
:bar => [MyMessage2.new(
Expand All @@ -125,11 +143,7 @@ def compile
:goat => MyEnum::BOO,
:bam => true,
:fire => 1.2,
))).to eq(
'{ :foo => "space", :bar => [{ '\
':foo => "station", :bar => { :foo => "orbit" }, :boom => 123, :goat => ::MyEnum::FOO, :bam => false, :fire => 3.5 '\
'}], :boom => 456, :goat => ::MyEnum::BOO, :bam => true, :fire => 1.2 }'
)
))).to eq(output_string)
end

it 'serializes enums' do
Expand Down