From 5b542c64d71c58c9703ea13a1c10f82fd8acb3f1 Mon Sep 17 00:00:00 2001 From: Max VelDink Date: Sat, 3 Aug 2024 19:54:52 -0400 Subject: [PATCH] refactor!: Replace deeply_nested_hash with options passing in struct ext --- lib/sorbet-schema/serialize_value.rb | 2 +- lib/sorbet-schema/t/struct.rb | 20 +++++++++----------- test/t/struct_test.rb | 4 ++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/sorbet-schema/serialize_value.rb b/lib/sorbet-schema/serialize_value.rb index e39c387..637b400 100644 --- a/lib/sorbet-schema/serialize_value.rb +++ b/lib/sorbet-schema/serialize_value.rb @@ -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 diff --git a/lib/sorbet-schema/t/struct.rb b/lib/sorbet-schema/t/struct.rb index 75de9df..33fc689 100644 --- a/lib/sorbet-schema/t/struct.rb +++ b/lib/sorbet-schema/t/struct.rb @@ -12,13 +12,11 @@ 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 @@ -26,15 +24,15 @@ def serializer(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 diff --git a/test/t/struct_test.rb b/test/t/struct_test.rb index cb48f9e..a3c12e2 100644 --- a/test/t/struct_test.rb +++ b/test/t/struct_test.rb @@ -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)