Skip to content

Commit

Permalink
refactor!: Replace deeply_nested_hash with options passing in struct ext
Browse files Browse the repository at this point in the history
  • Loading branch information
maxveldink committed Aug 3, 2024
1 parent 13de941 commit 5b542c6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
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

0 comments on commit 5b542c6

Please sign in to comment.