-
-
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: Introduce simple HashSerializer (#19)
- Loading branch information
1 parent
1f335b7
commit 80f20a9
Showing
8 changed files
with
112 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# typed: strict | ||
|
||
module Typed | ||
class HashSerializer < Serializer | ||
Input = type_member { {fixed: T::Hash[T.any(Symbol, String), T.untyped]} } | ||
Output = type_member { {fixed: T::Hash[Symbol, T.untyped]} } | ||
|
||
sig { override.params(source: Input).returns(Result[T::Struct, DeserializeError]) } | ||
def deserialize(source) | ||
deserialize_from_creation_params(symbolize_keys(source)) | ||
end | ||
|
||
sig { override.params(struct: T::Struct).returns(Output) } | ||
def serialize(struct) | ||
symbolize_keys(struct.serialize) | ||
end | ||
|
||
private | ||
|
||
sig { params(hash: T::Hash[T.any(String, Symbol), T.untyped]).returns(T::Hash[Symbol, T.untyped]) } | ||
def symbolize_keys(hash) | ||
hash.each_with_object({}) { |(k, v), h| h[k.intern] = v } | ||
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
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 | ||
|
||
class HashSerializerTest < Minitest::Test | ||
def setup | ||
@serializer = Typed::HashSerializer.new(schema: PersonSchema) | ||
end | ||
|
||
# Serialize Tests | ||
|
||
def test_it_can_simple_serialize | ||
max = PersonSchema.target.new(name: "Max", age: 29) | ||
|
||
assert_equal({name: "Max", age: 29}, @serializer.serialize(max)) | ||
end | ||
|
||
# Deserialize Tests | ||
|
||
def test_it_can_simple_deserialize | ||
max_hash = {name: "Max", age: 29} | ||
|
||
result = @serializer.deserialize(max_hash) | ||
|
||
assert_success(result) | ||
assert_payload(PersonSchema.target.new(name: "Max", age: 29), result) | ||
end | ||
|
||
def test_it_can_simple_deserialize_from_string_keys | ||
max_hash = {"name" => "Max", "age" => 29} | ||
|
||
result = @serializer.deserialize(max_hash) | ||
|
||
assert_success(result) | ||
assert_payload(PersonSchema.target.new(name: "Max", age: 29), result) | ||
end | ||
|
||
def test_it_reports_validation_errors_on_deserialize | ||
max_hash = {name: "Max"} | ||
|
||
result = @serializer.deserialize(max_hash) | ||
|
||
assert_failure(result) | ||
assert_error(Typed::Validations::RequiredFieldError.new(field_name: :age), result) | ||
end | ||
|
||
def test_it_reports_multiple_validation_errors_on_deserialize | ||
result = @serializer.deserialize({}) | ||
|
||
assert_failure(result) | ||
assert_error( | ||
Typed::Validations::MultipleValidationError.new( | ||
errors: [ | ||
Typed::Validations::RequiredFieldError.new(field_name: :name), | ||
Typed::Validations::RequiredFieldError.new(field_name: :age) | ||
] | ||
), | ||
result | ||
) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.