Skip to content

Commit

Permalink
Rename serializer type, add public getter should_serialize_values to …
Browse files Browse the repository at this point in the history
…HashSerializer
  • Loading branch information
tbconroy committed Jul 19, 2024
1 parent 7249936 commit fecd2e3
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sorbet-schema (0.7.2)
sorbet-schema (0.8.0)
sorbet-result (~> 1.1)
sorbet-runtime (~> 0.5)
sorbet-struct-comparable (~> 1.3)
Expand Down
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(:nested_hash).payload_or(value)
value.serialize_to(:deeply_nested_hash).payload_or(value)
elsif value.respond_to?(:serialize)
value.serialize
else
Expand Down
2 changes: 1 addition & 1 deletion lib/sorbet-schema/t/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def schema
sig { params(type: Symbol).returns(Typed::Serializer[T.untyped, T.untyped]) }
def serializer(type)
case type
when :nested_hash
when :deeply_nested_hash
Typed::HashSerializer.new(schema:, should_serialize_values: true)
when :hash
Typed::HashSerializer.new(schema:)
Expand Down
2 changes: 1 addition & 1 deletion lib/sorbet-schema/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# typed: strict

module SorbetSchema
VERSION = "0.7.2"
VERSION = "0.8.0"
end
8 changes: 3 additions & 5 deletions lib/typed/hash_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class HashSerializer < Serializer
Input = type_member { {fixed: InputHash} }
Output = type_member { {fixed: Params} }

sig { returns(T::Boolean) }
attr_reader :should_serialize_values

sig { params(schema: Schema, should_serialize_values: T::Boolean).void }
def initialize(schema:, should_serialize_values: false)
@should_serialize_values = should_serialize_values
Expand All @@ -24,10 +27,5 @@ def serialize(struct)

Success.new(serialize_from_struct(struct:, should_serialize_values:))
end

private

sig { returns(T::Boolean) }
attr_reader :should_serialize_values
end
end
2 changes: 0 additions & 2 deletions lib/typed/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def serialize(struct)
def deserialize_from_creation_params(creation_params)
results = schema.fields.map do |field|
value = creation_params.fetch(field.name, nil)

if value.nil? && !field.default.nil?
Success.new(Validations::ValidatedValue.new(name: field.name, value: field.default))
elsif value.nil? || field.works_with?(value)
Expand All @@ -48,7 +47,6 @@ def deserialize_from_creation_params(creation_params)
next if sub_type.raw_type.equal?(NilClass)

coercion_result = Coercion.coerce(type: sub_type, value: value)

if coercion_result.success?
validated_value = field.validate(coercion_result.payload)

Expand Down
12 changes: 9 additions & 3 deletions test/t/struct_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ def test_schema_can_be_derived_from_struct_with_default
assert_equal(expected_schema, Job.schema)
end

def test_serializer_returns_nested_hash_serializer
assert_kind_of(Typed::HashSerializer, City.serializer(:nested_hash))
def test_serializer_returns_deeply_nested_hash_serializer
serializer = City.serializer(:deeply_nested_hash)

assert_kind_of(Typed::HashSerializer, serializer)
assert_equal(true, T.cast(serializer, Typed::HashSerializer).should_serialize_values)
end

def test_serializer_returns_hash_serializer
assert_kind_of(Typed::HashSerializer, City.serializer(:hash))
serializer = City.serializer(:hash)

assert_kind_of(Typed::HashSerializer, serializer)
assert_equal(false, T.cast(serializer, Typed::HashSerializer).should_serialize_values)
end

def test_serializer_returns_json_serializer
Expand Down
2 changes: 1 addition & 1 deletion test/typed/hash_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_it_can_deep_serialize
result = serializer.serialize(ALEX_PERSON)

assert_success(result)
assert_payload({name: "Alex", age: 31, stone_rank: "pretty", job: {title: "Software Developer", salary:{cents:9000000, currency:"USD"}, needs_credential: false}}, result)
assert_payload({name: "Alex", age: 31, stone_rank: "pretty", job: {title: "Software Developer", salary: {cents: 9000000, currency: "USD"}, needs_credential: false}}, result)
end

def test_with_boolean_it_can_serialize
Expand Down

0 comments on commit fecd2e3

Please sign in to comment.