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

set nullify to empty array to address protobuf 4 deprecation warning #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
15 changes: 15 additions & 0 deletions spec/protobuf/active_record/serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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