diff --git a/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb b/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb index 91cfe185..354aa845 100644 --- a/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +++ b/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb @@ -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 # diff --git a/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb b/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb index 56ebbed5..534d83a5 100644 --- a/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +++ b/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb @@ -295,7 +295,6 @@ class Location repeated ::Google::Protobuf::SourceCodeInfo::Location, :location, 1 end - end end diff --git a/lib/protobuf/field/bool_field.rb b/lib/protobuf/field/bool_field.rb index 97f81557..6eb8e5a2 100644 --- a/lib/protobuf/field/bool_field.rb +++ b/lib/protobuf/field/bool_field.rb @@ -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 ## @@ -19,16 +21,16 @@ 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) @@ -36,7 +38,7 @@ def decode(value) end def encode(value) - [value ? 1 : 0].pack('C') + value ? TRUE_ENCODE : FALSE_ENCODE end private diff --git a/lib/protobuf/field/varint_field.rb b/lib/protobuf/field/varint_field.rb index 59fabdcb..f1d7ffbd 100644 --- a/lib/protobuf/field/varint_field.rb +++ b/lib/protobuf/field/varint_field.rb @@ -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 diff --git a/spec/lib/protobuf/generators/base_spec.rb b/spec/lib/protobuf/generators/base_spec.rb index 43d25e84..d6128ac1 100644 --- a/spec/lib/protobuf/generators/base_spec.rb +++ b/spec/lib/protobuf/generators/base_spec.rb @@ -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( @@ -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