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

Allow default value for String/Numeric Sets to be unset #133

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
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
Loading