-
-
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: implement TypedHashCoercer (#110)
- Loading branch information
1 parent
422a995
commit 6d64db7
Showing
7 changed files
with
123 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# typed: strict | ||
|
||
module Typed | ||
module Coercion | ||
class TypedHashCoercer < Coercer | ||
extend T::Generic | ||
|
||
Target = type_member { {fixed: T::Hash[T.untyped, T.untyped]} } | ||
|
||
sig { override.params(type: T::Types::Base).returns(T::Boolean) } | ||
def used_for_type?(type) | ||
type.is_a?(T::Types::TypedHash) | ||
end | ||
|
||
sig { override.params(type: T::Types::Base, value: Value).returns(Result[Target, CoercionError]) } | ||
def coerce(type:, value:) | ||
return Failure.new(CoercionError.new("Field type must be a T::Hash.")) unless used_for_type?(type) | ||
return Failure.new(CoercionError.new("Value must be a Hash.")) unless value.is_a?(Hash) | ||
|
||
return Success.new(value) if type.recursively_valid?(value) | ||
|
||
coerced_hash = {} | ||
errors = [] | ||
|
||
value.each do |k, v| | ||
key_result = Coercion.coerce(type: T::Utils.coerce(T.cast(type, T::Types::TypedHash).type.types.first), value: k) | ||
value_result = Coercion.coerce(type: T::Utils.coerce(T.cast(type, T::Types::TypedHash).type.types.last), value: v) | ||
|
||
if key_result.success? && value_result.success? | ||
coerced_hash[key_result.payload] = value_result.payload | ||
else | ||
if key_result.failure? | ||
errors << key_result.error | ||
end | ||
|
||
if value_result.failure? | ||
errors << value_result.error | ||
end | ||
end | ||
end | ||
|
||
if errors.empty? | ||
Success.new(coerced_hash) | ||
else | ||
Failure.new(CoercionError.new(errors.map(&:message).join(" | "))) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# typed: true | ||
|
||
require "test_helper" | ||
|
||
class TypedHashCoercerTest < Minitest::Test | ||
def setup | ||
@coercer = Typed::Coercion::TypedHashCoercer.new | ||
@type = T::Utils.coerce(T::Hash[String, Integer]) | ||
end | ||
|
||
def test_used_for_type_works | ||
assert(@coercer.used_for_type?(T::Utils.coerce(T::Hash[String, Integer]))) | ||
assert(@coercer.used_for_type?(T::Utils.coerce(T::Hash[String, String]))) | ||
assert(@coercer.used_for_type?(T::Utils.coerce(Hash))) | ||
refute(@coercer.used_for_type?(T::Utils.coerce(Integer))) | ||
end | ||
|
||
def test_when_non_hash_field_given_returns_failure | ||
result = @coercer.coerce(type: T::Utils.coerce(Integer), value: {"test" => 1}) | ||
|
||
assert_failure(result) | ||
assert_error(Typed::Coercion::CoercionError.new("Field type must be a T::Hash."), result) | ||
end | ||
|
||
def test_when_non_hash_value_given_returns_failure | ||
result = @coercer.coerce(type: @type, value: "testing") | ||
|
||
assert_failure(result) | ||
assert_error(Typed::Coercion::CoercionError.new("Value must be a Hash."), result) | ||
end | ||
|
||
def test_when_already_of_type_returns_success | ||
result = @coercer.coerce(type: @type, value: {"test" => 1}) | ||
|
||
assert_success(result) | ||
assert_payload({"test" => 1}, result) | ||
end | ||
|
||
def test_when_coercable_hash_can_be_coerced_returns_success | ||
result = @coercer.coerce(type: @type, value: {"test" => "1", 1 => 2}) | ||
|
||
assert_success(result) | ||
assert_payload({"test" => 1, "1" => 2}, result) | ||
end | ||
|
||
def test_when_untyped_hash_returns_success | ||
result = @coercer.coerce(type: T::Utils.coerce(Hash), value: {"test" => 1}) | ||
|
||
assert_success(result) | ||
assert_payload({"test" => 1}, result) | ||
end | ||
|
||
def test_when_array_cannot_be_coerced_returns_failure | ||
result = @coercer.coerce(type: @type, value: {"test" => "test"}) | ||
|
||
assert_failure(result) | ||
assert_error(Typed::Coercion::CoercionError.new("'test' cannot be coerced into Integer."), result) | ||
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