From 86bbea39e853e5d8b9fa45d33438759425573149 Mon Sep 17 00:00:00 2001 From: Shaun Carlson Date: Thu, 11 Aug 2022 15:42:57 -0600 Subject: [PATCH] set nullify to empty array to address protobuf 4 deprecation warning --- lib/protobuf/active_record/serialization.rb | 14 ++++++++++++++ spec/protobuf/active_record/serialization_spec.rb | 15 +++++++++++++++ spec/spec_helper.rb | 2 +- spec/support/db/setup.rb | 5 +++++ spec/support/definitions/messages.proto | 5 +++++ spec/support/models.rb | 1 + spec/support/models/zero.rb | 3 +++ spec/support/protobuf/messages.pb.rb | 6 ++++++ 8 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 spec/support/models/zero.rb diff --git a/lib/protobuf/active_record/serialization.rb b/lib/protobuf/active_record/serialization.rb index b330c11..cd4949a 100644 --- a/lib/protobuf/active_record/serialization.rb +++ b/lib/protobuf/active_record/serialization.rb @@ -225,6 +225,18 @@ def call(_selph) end end + class EmptyNullifyCaller + def initialize; end + + def call(_selph) + [] + end + end + + def _protobuf_empty_nullify_object(_field) + EmptyNullifyCaller.new + end + def _protobuf_nil_object(_field) NilMethodCaller.new end @@ -337,6 +349,8 @@ def _protobuf_field_objects(field) else self.class._protobuf_convert_to_fields_object(field) end + when field == :nullify + self.class._protobuf_empty_nullify_object(field) else self.class._protobuf_nil_object(field) end diff --git a/spec/protobuf/active_record/serialization_spec.rb b/spec/protobuf/active_record/serialization_spec.rb index d7e09fb..a83b6eb 100644 --- a/spec/protobuf/active_record/serialization_spec.rb +++ b/spec/protobuf/active_record/serialization_spec.rb @@ -182,6 +182,21 @@ class UnconfiguredUser expect(fields[:photos]).to be_an(Array) end end + + context "when nullify is NOT an attribute" do + it "initializes nullify to empty array" do + expect(user.fields_from_record[:nullify]).to eq [] + end + end + + context "when nullify IS an attribute" do + let(:zero) { Zero.create(:nullify => true) } + before { Zero.protobuf_message(ZeroMessage) } + + it "retains the original value" do + expect(zero.fields_from_record[:nullify]).to be true + end + end end describe "#to_proto" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 47628e0..a4be5d6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,7 +19,7 @@ # Turn deprecation warnings into errors with full backtrace. config.raise_errors_for_deprecations! - # Verifies the existance of any stubbed methods, replaces better_receive and better_stub + # Verifies the existence of any stubbed methods, replaces better_receive and better_stub # https://www.relishapp.com/rspec/rspec-mocks/v/3-1/docs/verifying-doubles/partial-doubles config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true diff --git a/spec/support/db/setup.rb b/spec/support/db/setup.rb index 90334c8..51a2a5e 100644 --- a/spec/support/db/setup.rb +++ b/spec/support/db/setup.rb @@ -26,4 +26,9 @@ t.timestamps :null => false end + + create_table :zeros do |t| + t.boolean :nullify + t.integer :z_saber_level + end end diff --git a/spec/support/definitions/messages.proto b/spec/support/definitions/messages.proto index e497f7a..8b7f8ec 100644 --- a/spec/support/definitions/messages.proto +++ b/spec/support/definitions/messages.proto @@ -21,3 +21,8 @@ message UserSearchMessage { repeated string guid = 1; repeated string email = 2; } + +message ZeroMessage { + bool nullify = 1; + int64 z_saber_level = 2; +} diff --git a/spec/support/models.rb b/spec/support/models.rb index 1e6cbe7..8dab97f 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -1,2 +1,3 @@ require "support/models/photo" require "support/models/user" +require "support/models/zero" diff --git a/spec/support/models/zero.rb b/spec/support/models/zero.rb new file mode 100644 index 0000000..67360c1 --- /dev/null +++ b/spec/support/models/zero.rb @@ -0,0 +1,3 @@ +class Zero < ::ActiveRecord::Base + include ::Protobuf::ActiveRecord::Model +end diff --git a/spec/support/protobuf/messages.pb.rb b/spec/support/protobuf/messages.pb.rb index 1d50a87..c34bcbc 100644 --- a/spec/support/protobuf/messages.pb.rb +++ b/spec/support/protobuf/messages.pb.rb @@ -12,6 +12,7 @@ class PhotoMessage < ::Protobuf::Message; end class UserMessage < ::Protobuf::Message; end class UserSearchMessage < ::Protobuf::Message; end +class ZeroMessage < ::Protobuf::Message; end ## @@ -39,3 +40,8 @@ class UserSearchMessage repeated :string, :email, 2 end +class ZeroMessage + optional :bool, :nullify, 1 + optional :int64, :z_saber_level, 2 +end +