sorbet-schema 0.9.2 → 0.9.3
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 +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -1
- data/benchmarks/helpers.rb +239 -0
- data/benchmarks/many_fields_deserialization.rb +23 -0
- data/benchmarks/many_fields_serialization.rb +27 -0
- data/benchmarks/simple_deserialization.rb +23 -0
- data/benchmarks/simple_serialization.rb +27 -0
- data/lib/sorbet-schema/version.rb +1 -1
- data/lib/typed/coercion/boolean_coercer.rb +2 -2
- data/lib/typed/coercion/coercer.rb +1 -1
- data/lib/typed/coercion/coercer_registry.rb +1 -1
- data/lib/typed/coercion/date_coercer.rb +2 -2
- data/lib/typed/coercion/date_time_coercer.rb +2 -2
- data/lib/typed/coercion/enum_coercer.rb +2 -2
- data/lib/typed/coercion/float_coercer.rb +2 -2
- data/lib/typed/coercion/integer_coercer.rb +2 -2
- data/lib/typed/coercion/string_coercer.rb +2 -2
- data/lib/typed/coercion/struct_coercer.rb +2 -2
- data/lib/typed/coercion/symbol_coercer.rb +2 -2
- data/lib/typed/coercion/typed_array_coercer.rb +2 -2
- data/lib/typed/coercion/typed_hash_coercer.rb +2 -2
- data/lib/typed/schema.rb +16 -2
- data/lib/typed/serializer.rb +41 -24
- data/sorbet/rbi/gems/benchmark-ips@2.14.0.rbi +981 -0
- metadata +12 -6
data/lib/typed/serializer.rb
CHANGED
|
@@ -15,9 +15,13 @@ module Typed
|
|
|
15
15
|
sig { returns(Schema) }
|
|
16
16
|
attr_reader :schema
|
|
17
17
|
|
|
18
|
+
sig { returns(T::Hash[T::Types::Base, T.untyped]) }
|
|
19
|
+
attr_reader :coercer_cache
|
|
20
|
+
|
|
18
21
|
sig { params(schema: Schema).void }
|
|
19
22
|
def initialize(schema:)
|
|
20
23
|
@schema = schema
|
|
24
|
+
@coercer_cache = T.let({}, T::Hash[T::Types::Base, T.untyped])
|
|
21
25
|
end
|
|
22
26
|
|
|
23
27
|
sig { abstract.params(source: Input).returns(DeserializeResult) }
|
|
@@ -34,41 +38,43 @@ module Typed
|
|
|
34
38
|
def deserialize_from_creation_params(creation_params)
|
|
35
39
|
results = schema.fields.map do |field|
|
|
36
40
|
value = creation_params.fetch(field.name, nil)
|
|
37
|
-
coercer = Coercion::CoercerRegistry.instance.select_coercer_by(type: field.type)
|
|
38
41
|
|
|
39
42
|
if value.nil? && !field.default.nil?
|
|
40
43
|
Success.new(Validations::ValidatedValue.new(name: field.name, value: field.default))
|
|
41
44
|
elsif value.nil? || field.works_with?(value)
|
|
42
45
|
field.validate(value)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
else
|
|
47
|
+
coercer_instance = fetch_coercer(field.type)
|
|
48
|
+
if !coercer_instance.nil?
|
|
49
|
+
result = coercer_instance.coerce(type: field.type, value:)
|
|
50
|
+
if result.success?
|
|
51
|
+
field.validate(result.payload)
|
|
52
|
+
else
|
|
53
|
+
Failure.new(Validations::ValidationError.new(result.error.message))
|
|
54
|
+
end
|
|
55
|
+
elsif field.type.class <= T::Types::Union
|
|
56
|
+
errors = []
|
|
57
|
+
validated_value = T.let(nil, T.nilable(Typed::Result[Typed::Validations::ValidatedValue, Typed::Validations::ValidationError]))
|
|
53
58
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
T.cast(field.type, T::Types::Union).types.each do |sub_type|
|
|
60
|
+
# the if clause took care of cases where value is nil so we can skip NilClass
|
|
61
|
+
next if sub_type.raw_type.equal?(NilClass)
|
|
57
62
|
|
|
58
|
-
|
|
63
|
+
coercion_result = Coercion.coerce(type: sub_type, value: value)
|
|
59
64
|
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
if coercion_result.success?
|
|
66
|
+
validated_value = field.validate(coercion_result.payload)
|
|
62
67
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
break
|
|
69
|
+
else
|
|
70
|
+
errors << Validations::ValidationError.new(coercion_result.error.message)
|
|
71
|
+
end
|
|
66
72
|
end
|
|
67
|
-
end
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
validated_value.nil? ? Failure.new(Validations::ValidationError.new(errors.map(&:message).join(", "))) : validated_value
|
|
75
|
+
else
|
|
76
|
+
Failure.new(Validations::ValidationError.new("Coercer not found for type #{field.type}."))
|
|
77
|
+
end
|
|
72
78
|
end
|
|
73
79
|
end
|
|
74
80
|
|
|
@@ -90,5 +96,16 @@ module Typed
|
|
|
90
96
|
|
|
91
97
|
hsh
|
|
92
98
|
end
|
|
99
|
+
|
|
100
|
+
sig { params(type: T::Types::Base).returns(T.untyped) }
|
|
101
|
+
def fetch_coercer(type)
|
|
102
|
+
cached = coercer_cache[type]
|
|
103
|
+
return cached if cached
|
|
104
|
+
|
|
105
|
+
coercer_class = Coercion::CoercerRegistry.instance.select_coercer_by(type: type)
|
|
106
|
+
return nil if coercer_class.nil?
|
|
107
|
+
|
|
108
|
+
coercer_cache[type] = coercer_class.new
|
|
109
|
+
end
|
|
93
110
|
end
|
|
94
111
|
end
|