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

Freeze transform_to_value #20

Merged
merged 5 commits into from
Jul 5, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.7', '3.0', '3.1']
ruby-version: ['3.1', '3.2', '3.3']
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,11 @@ I18n keys:

#### `transform_to_value(value)`

Always returns ValidResult. The value is transformed to provided argument (disregarding the original value). See also [`default`](#defaultdefault_value-on-nil).
Always returns ValidResult. The value is transformed to provided argument (disregarding the original value).

Returned value is deeply freezed with [`Ractor::make_shareable`](https://docs.ruby-lang.org/en/master/Ractor.html#method-c-make_shareable) to prevent application bugs due to modification of unintentionally shared value. If that effect is undesired, use [`transform { value }`](#transform--value--) instead.

See also [`default`](#defaultdefault_value-on-nil).

### "Web-form" types

Expand Down
2 changes: 1 addition & 1 deletion datacaster.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |spec|

EugZol marked this conversation as resolved.
Show resolved Hide resolved
spec.summary = %q{Run-time type checker and transformer for Ruby}
spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
spec.required_ruby_version = Gem::Requirement.new('>= 3.1')

spec.metadata['source_code_uri'] = 'https://github.com/EugZol/datacaster'
spec.homepage = 'https://github.com/EugZol/datacaster'
Expand Down
4 changes: 4 additions & 0 deletions lib/datacaster/absent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ def to_s
def present?
false
end

def ==(other)
other.is_a?(self.class)
end
end
end
2 changes: 1 addition & 1 deletion lib/datacaster/predefined.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def switch(base = nil, **on_clauses)
end

def transform_to_value(value)
transform { value }
transform { Datacaster::Utils.deep_freeze(value) }
end

def with(keys, caster)
Expand Down
4 changes: 4 additions & 0 deletions lib/datacaster/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Datacaster
module Utils
extend self

def deep_freeze(value, copy: true)
Ractor.make_shareable(value, copy:)
end

def merge_errors(left, right)
add_error_to_base = ->(hash, error) {
hash[:base] ||= []
Expand Down
2 changes: 1 addition & 1 deletion lib/datacaster/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Datacaster
VERSION = "3.2.8"
VERSION = "3.3.0"
end
18 changes: 16 additions & 2 deletions spec/datacaster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1033,10 +1033,24 @@
end

describe "constant mapping" do
let(:transform_to_value_caster) do
Datacaster.schema { transform_to_value({ a: { b: { c: ["d"] } } }) }
end

let(:returned_value) do
transform_to_value_caster.(123).value!
end

it "returns exact value" do
t = Datacaster.schema { transform_to_value("Test") }
expect(returned_value).to eq({ a: { b: { c: ["d"] } } })
end

it "freezes returned value" do
expect(returned_value).to be_frozen
end

expect(t.(123).to_dry_result).to eq Success("Test")
it "freezes deeply" do
expect { returned_value[:a][:b][:c].pop }.to raise_error FrozenError
end
end

Expand Down
Loading