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

refactor!: Replace deeply_nested_hash with options passing in struct ext #121

Merged
merged 1 commit into from
Aug 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 lib/sorbet-schema/serialize_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.serialize(value)
elsif value.is_a?(Array)
value.map { |item| serialize(item) }
elsif value.is_a?(T::Struct)
value.serialize_to(:deeply_nested_hash).payload_or(value)
value.serialize_to(:hash, options: {should_serialize_values: true}).payload_or(value)
elsif value.respond_to?(:serialize)
value.serialize
else
Expand Down
20 changes: 9 additions & 11 deletions lib/sorbet-schema/t/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,27 @@ def schema
Typed::Schema.from_struct(self)
end

sig { params(type: Symbol).returns(Typed::Serializer[T.untyped, T.untyped]) }
def serializer(type)
sig { params(type: Symbol, options: T::Hash[Symbol, T.untyped]).returns(Typed::Serializer[T.untyped, T.untyped]) }
def serializer(type, options: {})
case type
when :deeply_nested_hash
Typed::HashSerializer.new(schema:, should_serialize_values: true)
when :hash
Typed::HashSerializer.new(schema:)
Typed::HashSerializer.new(**T.unsafe({schema:, **options}))
when :json
Typed::JSONSerializer.new(schema:)
else
raise ArgumentError, "unknown serializer for #{type}"
end
end

sig { params(serializer_type: Symbol, source: T.untyped).returns(Typed::Serializer::DeserializeResult) }
def deserialize_from(serializer_type, source)
serializer(serializer_type).deserialize(source)
sig { params(serializer_type: Symbol, source: T.untyped, options: T::Hash[Symbol, T.untyped]).returns(Typed::Serializer::DeserializeResult) }
def deserialize_from(serializer_type, source, options: {})
serializer(serializer_type, options:).deserialize(source)
end
end

sig { params(serializer_type: Symbol).returns(Typed::Result[T.untyped, Typed::SerializeError]) }
def serialize_to(serializer_type)
self.class.serializer(serializer_type).serialize(self)
sig { params(serializer_type: Symbol, options: T::Hash[Symbol, T.untyped]).returns(Typed::Result[T.untyped, Typed::SerializeError]) }
def serialize_to(serializer_type, options: {})
self.class.serializer(serializer_type, options:).serialize(self)
end
end
end
4 changes: 2 additions & 2 deletions test/t/struct_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def test_schema_can_be_derived_from_struct_with_default
assert_equal(expected_schema, Job.schema)
end

def test_serializer_returns_deeply_nested_hash_serializer
serializer = City.serializer(:deeply_nested_hash)
def test_serializer_returns_hash_serializer_with_options
serializer = City.serializer(:hash, options: {should_serialize_values: true})

assert_kind_of(Typed::HashSerializer, serializer)
assert(T.cast(serializer, Typed::HashSerializer).should_serialize_values)
Expand Down