optify-from_hash 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a82bb6b8ae57bd5981e5858b9a91648d9a3f07a3b5d0e0122d775424e0784253
4
- data.tar.gz: de084f1add61ca7a2c036600e78c771757e0bf966b7face1458e2fcc4667bbc0
3
+ metadata.gz: 1632ba5c0ae9ac2b6b30c9a60ba78b00cafb89ec15199fa7ba994e2d1fc90539
4
+ data.tar.gz: 9cd72e917b0688f61f5a0ec2d0d3ecb85f4939cdcd21b9a7bbdd494af67e9a93
5
5
  SHA512:
6
- metadata.gz: f9f75938b15ea45a739f2f68c643737f4d0cbb39141075b412f131497efa2caaad8b2b85cabf2b5b0f10d8313e0281de6e9a18d9d6bc32fee74b827b4f394f52
7
- data.tar.gz: 60985b018e36d3efcdeb66ace9fe4e473ca49115c57d8f6cffe04bf61cf210fd5eaec02504a64acad3ea88a1fa927bfc08914df6fae1029a8b9b3e0c6837bc8d
6
+ metadata.gz: 9d17e0f0d1bd9e8f868b168fb90ce0edaf3de4445b11780c48e24f790ed10b98c455b1c489d085658313043a6e6e7c8773d9aa744bf5b64d4f9a7423547ad3ba
7
+ data.tar.gz: c903dc3d0ca0fe29ff6a8c3dd50e68e9c1d946d66b8fb96db1cdd45df9104f8994b5c661485e9430bf5f73819183e08a95873f3730e658feb6f9c1ee67b9502c
@@ -4,7 +4,6 @@
4
4
  require 'json'
5
5
  require 'set'
6
6
  require 'sorbet-runtime'
7
- require 'tapioca'
8
7
 
9
8
  module Optify
10
9
  # A base class for classes that can be created from a hash.
@@ -139,16 +138,58 @@ module Optify
139
138
  # The hash should be a hash, but the values might be objects to convert.
140
139
  type_for_keys = type.keys
141
140
  type_for_values = type.values
141
+ type_for_keys_allows_string = _type_allows_string?(type_for_keys)
142
+ type_for_values_allows_string = _type_allows_string?(type_for_values)
142
143
 
143
- result = hash
144
- .transform_values { |v| _convert_value(v, type_for_values) }
144
+ return hash.each_with_object(Hash.new(capacity: hash.size)) do |(k, v), result|
145
+ result[_convert_typed_hash_value(k, type_for_keys, type_for_keys_allows_string)] =
146
+ _convert_typed_hash_value(v, type_for_values, type_for_values_allows_string)
147
+ end
148
+ end
149
+
150
+ raise TypeError, "Could not convert hash #{hash} to `#{type}`."
151
+ end
145
152
 
146
- return result.transform_keys!(&:to_sym) if type_for_keys.is_a?(T::Types::Simple) && type_for_keys.raw_type == Symbol
153
+ #: (untyped, T::Types::Base, bool) -> untyped
154
+ private_class_method def self._convert_typed_hash_value(value, type, type_allows_string) # rubocop:disable Metrics/PerceivedComplexity
155
+ return _convert_value(value, type) if type_allows_string || !value.is_a?(String)
147
156
 
148
- return result
157
+ value_type = if type.respond_to?(:raw_type)
158
+ type #: as untyped
159
+ .raw_type
160
+ end
161
+ return value.to_sym if value_type == Symbol
162
+
163
+ if type.respond_to?(:types)
164
+ type #: as untyped
165
+ .types.each do |inner_type|
166
+ return _convert_typed_hash_value(value, inner_type, _type_allows_string?(inner_type))
167
+ rescue TypeError, ArgumentError
168
+ # Ignore and try the next type.
169
+ end
149
170
  end
150
171
 
151
- raise TypeError, "Could not convert hash #{hash} to `#{type}`."
172
+ return value_type.deserialize(value) if value_type && value_type != String && value_type.respond_to?(:deserialize)
173
+
174
+ _convert_value(value, type)
175
+ end
176
+
177
+ #: (T::Types::Base) -> bool
178
+ private_class_method def self._type_allows_string?(type)
179
+ if type.respond_to?(:raw_type)
180
+ value_type = type #: as untyped
181
+ .raw_type
182
+ return true if value_type == String
183
+ end
184
+
185
+ if type.respond_to?(:types)
186
+ type #: as untyped
187
+ .types.each do |value_type|
188
+ return true if _type_allows_string?(value_type)
189
+ end
190
+ end
191
+
192
+ false
152
193
  end
153
194
 
154
195
  # Unwrap `T.nilable(...)` to get the inner type, or return the type as-is.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optify-from_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin D. Harris
@@ -91,6 +91,20 @@ dependencies:
91
91
  - - "~>"
92
92
  - !ruby/object:Gem::Version
93
93
  version: 0.19.1
94
+ - !ruby/object:Gem::Dependency
95
+ name: enummify
96
+ requirement: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: 0.1.0
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: 0.1.0
94
108
  - !ruby/object:Gem::Dependency
95
109
  name: test-unit
96
110
  requirement: !ruby/object:Gem::Requirement