-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Allow for custom Coercers (#34)
- Loading branch information
Max VelDink
authored
Mar 8, 2024
1 parent
012841f
commit 54c6a53
Showing
15 changed files
with
169 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# typed: strict | ||
|
||
require "singleton" | ||
|
||
module Typed | ||
module Coercion | ||
class CoercerRegistry | ||
extend T::Sig | ||
|
||
include Singleton | ||
|
||
Registry = T.type_alias { T::Array[T.class_of(Coercer)] } | ||
|
||
DEFAULT_COERCERS = T.let([StringCoercer, IntegerCoercer, FloatCoercer, StructCoercer], Registry) | ||
|
||
sig { void } | ||
def initialize | ||
@available = T.let(DEFAULT_COERCERS.clone, Registry) | ||
end | ||
|
||
sig { params(coercer: T.class_of(Coercer)).void } | ||
def register(coercer) | ||
@available.prepend(coercer) | ||
end | ||
|
||
sig { void } | ||
def reset! | ||
@available = DEFAULT_COERCERS.clone | ||
end | ||
|
||
sig { params(type: T::Class[T.anything]).returns(T.nilable(T.class_of(Coercer))) } | ||
def select_coercer_by(type:) | ||
@available.find { |coercer| coercer.new.used_for_type?(type) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# typed: true | ||
|
||
class SimpleStringCoercer < Typed::Coercion::Coercer | ||
extend T::Generic | ||
|
||
Target = type_member { {fixed: String} } | ||
|
||
sig { override.params(type: T::Class[T.anything]).returns(T::Boolean) } | ||
def used_for_type?(type) | ||
type == String | ||
end | ||
|
||
sig { override.params(field: Typed::Field, value: Typed::Value).returns(Typed::Result[Target, Typed::Coercion::CoercionError]) } | ||
def coerce(field:, value:) | ||
Typed::Success.new("always this value") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# typed: true | ||
|
||
class CoercerRegistryTest < Minitest::Test | ||
def teardown | ||
Typed::Coercion::CoercerRegistry.instance.reset! | ||
end | ||
|
||
def test_register_prepends_coercer_so_it_overrides_built_in_ones | ||
Typed::Coercion::CoercerRegistry.instance.register(SimpleStringCoercer) | ||
|
||
assert_equal(SimpleStringCoercer, Typed::Coercion::CoercerRegistry.instance.select_coercer_by(type: String)) | ||
end | ||
|
||
def test_when_type_doesnt_match_coercer_returns_nil | ||
assert_nil(Typed::Coercion::CoercerRegistry.instance.select_coercer_by(type: Array)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
# typed: true | ||
|
||
class StringCoercerTest < Minitest::Test | ||
def test_returns_success | ||
field = Typed::Field.new(name: :testing, type: String) | ||
def setup | ||
@coercer = Typed::Coercion::StringCoercer.new | ||
@field = Typed::Field.new(name: :testing, type: String) | ||
end | ||
|
||
assert_payload("1", Typed::Coercion::StringCoercer.coerce(field: field, value: 1)) | ||
assert_payload("[1, 2]", Typed::Coercion::StringCoercer.coerce(field: field, value: [1, 2])) | ||
def test_used_for_type_works | ||
assert(@coercer.used_for_type?(String)) | ||
refute(@coercer.used_for_type?(Integer)) | ||
end | ||
|
||
def test_returns_success | ||
assert_payload("1", @coercer.coerce(field: @field, value: 1)) | ||
assert_payload("[1, 2]", @coercer.coerce(field: @field, value: [1, 2])) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters