Skip to content

Commit

Permalink
set nullify to empty array to address protobuf 4 deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun Carlson committed Aug 11, 2022
1 parent fa49bd3 commit f1794d7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/protobuf/active_record/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/protobuf/active_record/serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class UnconfiguredUser
context "when a transformer is defined for the field" do
it "gets the field from the transformer" do
expect(user.fields_from_record[:email_domain]).to eq("test.co")
expect(user.fields_from_record[:email_domain]).to eq("test.co")
end
end

Expand All @@ -182,6 +183,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
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/support/db/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@

t.timestamps :null => false
end

create_table :zeros do |t|
t.boolean :nullify
t.integer :z_saber_level
end
end
5 changes: 5 additions & 0 deletions spec/support/definitions/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ message UserSearchMessage {
repeated string guid = 1;
repeated string email = 2;
}

message ZeroMessage {
bool nullify = 1;
int64 z_saber_level = 2;
}
1 change: 1 addition & 0 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require "support/models/photo"
require "support/models/user"
require "support/models/zero"
3 changes: 3 additions & 0 deletions spec/support/models/zero.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Zero < ::ActiveRecord::Base
include ::Protobuf::ActiveRecord::Model
end
6 changes: 6 additions & 0 deletions spec/support/protobuf/messages.pb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class PhotoMessage < ::Protobuf::Message; end
class UserMessage < ::Protobuf::Message; end
class UserSearchMessage < ::Protobuf::Message; end
class ZeroMessage < ::Protobuf::Message; end


##
Expand Down Expand Up @@ -39,3 +40,8 @@ class UserSearchMessage
repeated :string, :email, 2
end

class ZeroMessage
optional :bool, :nullify, 1
optional :int64, :z_saber_level, 2
end

0 comments on commit f1794d7

Please sign in to comment.