Skip to content

Commit

Permalink
Merge pull request #133 from lsglick/lsglick/BUGFIX-set-default
Browse files Browse the repository at this point in the history
Allow default value for String/Numeric Sets to be unset
  • Loading branch information
mullermp authored Sep 25, 2023
2 parents 1a82279 + b6b98f2 commit c75d136
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/aws-record/record/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ def initialize(name, options = {})
@dynamodb_type = options[:dynamodb_type]
@marshaler = options[:marshaler] || DefaultMarshaler
@persist_nil = options[:persist_nil]
dv = options[:default_value]
@default_value_or_lambda = _is_lambda?(dv) ? dv : type_cast(dv)
@default_value_or_lambda = if options.key?(:default_value)
dv = options[:default_value]
_is_lambda?(dv) ? dv : type_cast(dv)
else
nil
end
end

# Attempts to type cast a raw value into the attribute's type. This call
Expand Down
12 changes: 12 additions & 0 deletions spec/aws-record/record/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ module Record

expect(a.default_value).to eq({})
end

it 'does not type_cast unset value' do
m = Marshalers::StringSetMarshaler.new
a = Attribute.new(:foo, marshaler: m)
expect(a.default_value).to be_nil
end

it 'type casts nil value' do
m = Marshalers::StringSetMarshaler.new
a = Attribute.new(:foo, marshaler: m, default_value: nil)
expect(a.default_value).to be_a(Set)
end
end
end
end
Expand Down

0 comments on commit c75d136

Please sign in to comment.