Skip to content

Commit

Permalink
Allow to pass empty strings and sets to .import method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Mar 10, 2018
1 parent 46b1784 commit 77e1447
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/dynamoid/adapter_plugin/aws_sdk_v2.rb
Expand Up @@ -99,7 +99,7 @@ def batch_write_item table_name, objects, options = {}
requests = []

objects.each_slice(BATCH_WRITE_ITEM_REQUESTS_LIMIT) do |os|
requests << os.map { |o| { put_request: { item: o } } }
requests << os.map { |o| { put_request: { item: sanitize_item(o) } } }
end

begin
Expand Down Expand Up @@ -421,13 +421,8 @@ def list_tables
#
# See: http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#put_item-instance_method
def put_item(table_name, object, options = {})
item = {}
options ||= {}

object.each do |k, v|
next if v.nil? || ((v.is_a?(Set) || v.is_a?(String)) && v.empty?)
item[k.to_s] = v
end
item = sanitize_item(object)

begin
client.put_item(
Expand Down Expand Up @@ -1037,6 +1032,12 @@ def to_h
DELETE = 'DELETE'.freeze
PUT = 'PUT'.freeze
end

def sanitize_item(attributes)
attributes.reject do |k, v|
v.nil? || ((v.is_a?(Set) || v.is_a?(String)) && v.empty?)
end
end
end
end
end
49 changes: 49 additions & 0 deletions spec/dynamoid/persistence_spec.rb
Expand Up @@ -330,6 +330,11 @@ def reload_address
expect(User.find(user.id).name).to eq nil
end

it 'saves attributes with nil value' do
user = User.create(name: nil)
expect(User.find(user.id).name).to eq nil
end

it 'supports container types being nil' do
u = User.create(name: 'Philip')
u.todo_list = nil
Expand Down Expand Up @@ -933,6 +938,8 @@ def self.name; 'Doc'; end
describe '.import' do
before do
Address.create_table
User.create_table
Tweet.create_table
end

it 'creates multiple documents' do
Expand Down Expand Up @@ -979,5 +986,47 @@ def self.name; 'Address'; end
expect(Dynamoid.adapter).to receive(:batch_write_item).and_call_original
Address.import([{city: 'Chicago'}, {city: 'New York'}])
end

it 'supports empty containers in `serialized` fields' do
users = User.import([name: 'Philip', favorite_colors: Set.new])

user = User.find(users[0].id)
expect(user.favorite_colors).to eq Set.new
end

it 'supports array being empty' do
users = User.import([{todo_list: []}])

user = User.find(users[0].id)
expect(user.todo_list).to eq []
end

it 'saves empty set as nil' do
tweets = Tweet.import([{group: 'one', tags: []}])

tweet = Tweet.find_by_tweet_id(tweets[0].tweet_id)
expect(tweet.tags).to eq nil
end

it 'saves empty string as nil' do
users = User.import([{name: ''}])

user = User.find(users[0].id)
expect(user.name).to eq nil
end

it 'saves attributes with nil value' do
users = User.import([{name: nil}])

user = User.find(users[0].id)
expect(user.name).to eq nil
end

it 'supports container types being nil' do
users = User.import([{name: 'Philip', todo_list: nil}])

user = User.find(users[0].id)
expect(user.todo_list).to eq nil
end
end
end

0 comments on commit 77e1447

Please sign in to comment.