sorbet-schema 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b4040ccaa87f90e0d32acee9868fffb0f4d645e599fe29726738fdb54c4f1e6
4
- data.tar.gz: b83d96b38d8b7d786a3e174e576053f531bf03e237d7c1f049e98b9c813dc117
3
+ metadata.gz: d2adb1981aeb5218f9d137f472bd602a2b81bbe6e54166e2974f1c157f92d50a
4
+ data.tar.gz: 1f4ee3983069f2674aab270bb7d138ce3fc611154a4d5c37581b6698ac6f3189
5
5
  SHA512:
6
- metadata.gz: 248121230fc7a86b616259a89db47c210360034aad0ec70a23e2b25d73b350e16eff09c09ea87efd0aa859ef86b6f5c7f7700043b2e0d3e1f570555e98baac31
7
- data.tar.gz: 6f0c35a33ff1e84ebe704c30a6d0ba4b3520030c8ca59c897c33cc96f736c48c16e8672e7f4efef6f81f4a9816326db41c1cc625f49addd520a437066a04db1c
6
+ metadata.gz: d8b876a03ee53cafde5a9bbd9f46823d517aa6d69a3417a4e3c04591f1471066bc2912c2c26e00ba9e9294642912e4a4d58dafada77e8206685b4f726c23ae52
7
+ data.tar.gz: 9bf510fe1ef9e4b56ef9206ed1c5ea43bcb3934b1749c427afe76291a9b09871383d443f226f15cac733c39540180c2eed810d653b1728f73e11a8572f5b80be
data/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.7.0](https://github.com/maxveldink/sorbet-schema/compare/v0.6.0...v0.7.0) (2024-07-08)
8
+
9
+
10
+ ### ⚠ BREAKING CHANGES
11
+
12
+ * Fix mis-serializing hash keys that were suppose to be strings ([#111](https://github.com/maxveldink/sorbet-schema/issues/111))
13
+
14
+ ### Bug Fixes
15
+
16
+ * Fix mis-serializing hash keys that were suppose to be strings ([#111](https://github.com/maxveldink/sorbet-schema/issues/111)) ([485a6c7](https://github.com/maxveldink/sorbet-schema/commit/485a6c7a83b9e70c731930d8406925304efa04a8))
17
+
7
18
  ## [0.6.0](https://github.com/maxveldink/sorbet-schema/compare/v0.5.1...v0.6.0) (2024-07-07)
8
19
 
9
20
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sorbet-schema (0.6.0)
4
+ sorbet-schema (0.7.0)
5
5
  sorbet-result (~> 1.1)
6
6
  sorbet-runtime (~> 0.5)
7
7
  sorbet-struct-comparable (~> 1.3)
@@ -4,44 +4,21 @@
4
4
  # This is a simplified version of ActiveSupport's Key Hash extension
5
5
  # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/hash/keys.rb
6
6
  class HashTransformer
7
- extend T::Sig
7
+ class << self
8
+ extend T::Sig
8
9
 
9
- sig { params(should_serialize_values: T::Boolean).void }
10
- def initialize(should_serialize_values: false)
11
- @should_serialize_values = should_serialize_values
12
- end
13
-
14
- sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T::Hash[Symbol, T.untyped]) }
15
- def deep_symbolize_keys(hash)
16
- hash.each_with_object({}) do |(key, value), result|
17
- result[key.to_sym] = transform_value(value, hash_transformation_method: :deep_symbolize_keys)
18
- end
19
- end
20
-
21
- sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T::Hash[String, T.untyped]) }
22
- def deep_stringify_keys(hash)
23
- hash.each_with_object({}) do |(key, value), result|
24
- result[key.to_s] = transform_value(value, hash_transformation_method: :deep_stringify_keys)
10
+ sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T::Hash[Symbol, T.untyped]) }
11
+ def symbolize_keys(hash)
12
+ hash.each_with_object({}) do |(key, value), result|
13
+ result[key.to_sym] = value
14
+ end
25
15
  end
26
- end
27
-
28
- private
29
-
30
- sig { returns(T::Boolean) }
31
- attr_reader :should_serialize_values
32
16
 
33
- sig { params(value: T.untyped, hash_transformation_method: Symbol).returns(T.untyped) }
34
- def transform_value(value, hash_transformation_method:)
35
- if value.is_a?(Hash)
36
- send(hash_transformation_method, value)
37
- elsif value.is_a?(Array)
38
- value.map { |inner_val| transform_value(inner_val, hash_transformation_method: hash_transformation_method) }
39
- elsif value.is_a?(T::Struct) && should_serialize_values
40
- deep_symbolize_keys(value.serialize)
41
- elsif value.respond_to?(:serialize) && should_serialize_values
42
- value.serialize
43
- else
44
- value
17
+ sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T::Hash[T.untyped, T.untyped]) }
18
+ def serialize_values(hash)
19
+ hash.each_with_object({}) do |(key, value), result|
20
+ result[key] = SerializeValue.serialize(value)
21
+ end
45
22
  end
46
23
  end
47
24
  end
@@ -0,0 +1,20 @@
1
+ # typed: strict
2
+
3
+ class SerializeValue
4
+ extend T::Sig
5
+
6
+ sig { params(value: T.untyped).returns(T.untyped) }
7
+ def self.serialize(value)
8
+ if value.is_a?(Hash)
9
+ HashTransformer.serialize_values(value)
10
+ elsif value.is_a?(Array)
11
+ value.map { |item| serialize(item) }
12
+ elsif value.is_a?(T::Struct)
13
+ value.serialize_to(:hash).payload_or(value)
14
+ elsif value.respond_to?(:serialize)
15
+ value.serialize
16
+ else
17
+ value
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # typed: strict
2
2
 
3
3
  module SorbetSchema
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/sorbet-schema.rb CHANGED
@@ -16,10 +16,11 @@ loader.inflector.inflect(
16
16
  )
17
17
  loader.setup
18
18
 
19
- # We don't want to place this in the `Typed` module.
19
+ # We don't want to place these in the `Typed` module.
20
20
  # `sorbet-schema` is a directory that is not autoloaded
21
21
  # but contains extensions, so we need to manually require it.
22
22
  require_relative "sorbet-schema/hash_transformer"
23
+ require_relative "sorbet-schema/serialize_value"
23
24
 
24
25
  # We want to add a default `schema` method to structs
25
26
  # that will guarentee a schema can be created for use
@@ -15,14 +15,14 @@ module Typed
15
15
 
16
16
  sig { override.params(source: Input).returns(Result[T::Struct, DeserializeError]) }
17
17
  def deserialize(source)
18
- deserialize_from_creation_params(HashTransformer.new(should_serialize_values: should_serialize_values).deep_symbolize_keys(source))
18
+ deserialize_from_creation_params(HashTransformer.symbolize_keys(source))
19
19
  end
20
20
 
21
21
  sig { override.params(struct: T::Struct).returns(Result[Output, SerializeError]) }
22
22
  def serialize(struct)
23
23
  return Failure.new(SerializeError.new("'#{struct.class}' cannot be serialized to target type of '#{schema.target}'.")) if struct.class != schema.target
24
24
 
25
- Success.new(serialize_from_struct(struct: struct, should_serialize_values: should_serialize_values))
25
+ Success.new(serialize_from_struct(struct:, should_serialize_values:))
26
26
  end
27
27
 
28
28
  private
@@ -24,7 +24,7 @@ module Typed
24
24
  def serialize(struct)
25
25
  return Failure.new(SerializeError.new("'#{struct.class}' cannot be serialized to target type of '#{schema.target}'.")) if struct.class != schema.target
26
26
 
27
- Success.new(JSON.generate(serialize_from_struct(struct: struct, should_serialize_values: true)))
27
+ Success.new(JSON.generate(serialize_from_struct(struct:, should_serialize_values: true)))
28
28
  end
29
29
  end
30
30
  end
@@ -82,7 +82,11 @@ module Typed
82
82
  def serialize_from_struct(struct:, should_serialize_values: false)
83
83
  hsh = schema.fields.each_with_object({}) { |field, hsh| hsh[field.name] = field.serialize(struct.send(field.name)) }.compact
84
84
 
85
- HashTransformer.new(should_serialize_values: should_serialize_values).deep_symbolize_keys(hsh)
85
+ if should_serialize_values
86
+ hsh = HashTransformer.serialize_values(hsh)
87
+ end
88
+
89
+ hsh
86
90
  end
87
91
  end
88
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorbet-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max VelDink
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-07 00:00:00.000000000 Z
11
+ date: 2024-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sorbet-result
@@ -85,6 +85,7 @@ files:
85
85
  - Rakefile
86
86
  - lib/sorbet-schema.rb
87
87
  - lib/sorbet-schema/hash_transformer.rb
88
+ - lib/sorbet-schema/serialize_value.rb
88
89
  - lib/sorbet-schema/t/struct.rb
89
90
  - lib/sorbet-schema/version.rb
90
91
  - lib/typed/coercion.rb