sorbet-schema 0.1.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +25 -0
- data/Gemfile.lock +1 -1
- data/lib/sorbet-schema/version.rb +1 -1
- data/lib/typed/coercion/coercer.rb +5 -1
- data/lib/typed/coercion/coercer_registry.rb +37 -0
- data/lib/typed/coercion/float_coercer.rb +7 -5
- data/lib/typed/coercion/integer_coercer.rb +7 -5
- data/lib/typed/coercion/string_coercer.rb +7 -5
- data/lib/typed/coercion/struct_coercer.rb +7 -5
- data/lib/typed/coercion.rb +10 -15
- data/lib/typed/deserialize_error.rb +11 -0
- data/release-please-config.json +14 -0
- data/release-please-manifest.json +3 -0
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d70fa44a21277553c70f83ca5bb33d012f165f4ce39921374bad60f4aafaead
|
4
|
+
data.tar.gz: ed9f24ad2785da98b16306587556c165e6218134540ce28ef22a422852a4fa3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9faa04758b3bca54e9c83024dcc30f94dcd84f6f4260bdff0f9e80a6e5ddd134626192264dbfc65df8bab1a5af0cdde9e65dd3f5d7f33ee1a0eb373729d007d5
|
7
|
+
data.tar.gz: '0649efc2845a54f4cfde1fc3adfc1330bc18dc06b22b501f0ce02d178af893228a81dc7a50373b771b230b15c42d97b35c50820b3fca9f49dbbd81667c22c78f'
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,31 @@ 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.2.0](https://github.com/maxveldink/sorbet-schema/compare/v0.1.1...v0.2.0) (2024-03-08)
|
8
|
+
|
9
|
+
|
10
|
+
### ⚠ BREAKING CHANGES
|
11
|
+
|
12
|
+
* Allow for custom Coercers ([#34](https://github.com/maxveldink/sorbet-schema/issues/34))
|
13
|
+
|
14
|
+
### Features
|
15
|
+
|
16
|
+
* Allow for custom Coercers ([#34](https://github.com/maxveldink/sorbet-schema/issues/34)) ([54c6a53](https://github.com/maxveldink/sorbet-schema/commit/54c6a53019b18d65b18d6d1130c1034f1f6b1341))
|
17
|
+
|
18
|
+
## [0.1.1](https://github.com/maxveldink/sorbet-schema/compare/v0.1.0...v0.1.1) (2024-03-08)
|
19
|
+
|
20
|
+
|
21
|
+
### Features
|
22
|
+
|
23
|
+
* adds `to_h` and `to_json` methods to Deserialize errors ([#28](https://github.com/maxveldink/sorbet-schema/issues/28)) ([bf5f770](https://github.com/maxveldink/sorbet-schema/commit/bf5f770bc3ca176f18146dd780ad7ccd7fcb05b0))
|
24
|
+
|
25
|
+
|
26
|
+
### Bug Fixes
|
27
|
+
|
28
|
+
* Add release-please config ([#30](https://github.com/maxveldink/sorbet-schema/issues/30)) ([b311c28](https://github.com/maxveldink/sorbet-schema/commit/b311c2840d4929776e0133b061e531ae9d1f453f))
|
29
|
+
* Downgrade release-please action and publish gem through there ([#31](https://github.com/maxveldink/sorbet-schema/issues/31)) ([4ef9881](https://github.com/maxveldink/sorbet-schema/commit/4ef988120c73f42fdfa749d67b5ca0bafc4e52ce))
|
30
|
+
* update release-please permissions ([#33](https://github.com/maxveldink/sorbet-schema/issues/33)) ([b5d866c](https://github.com/maxveldink/sorbet-schema/commit/b5d866ca304879fc92c8d20ca2b303a3fcdd27c3))
|
31
|
+
|
7
32
|
## 0.1.0 (2024-03-05)
|
8
33
|
|
9
34
|
### Features
|
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Typed
|
4
4
|
module Coercion
|
5
|
-
|
5
|
+
class Coercer
|
6
6
|
extend T::Sig
|
7
7
|
extend T::Generic
|
8
8
|
|
@@ -10,6 +10,10 @@ module Typed
|
|
10
10
|
|
11
11
|
Target = type_member(:out)
|
12
12
|
|
13
|
+
sig { abstract.params(type: T::Class[T.anything]).returns(T::Boolean) }
|
14
|
+
def used_for_type?(type)
|
15
|
+
end
|
16
|
+
|
13
17
|
sig { abstract.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
|
14
18
|
def coerce(field:, value:)
|
15
19
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# typed: strict
|
2
|
+
|
3
|
+
require "singleton"
|
4
|
+
|
5
|
+
module Typed
|
6
|
+
module Coercion
|
7
|
+
class CoercerRegistry
|
8
|
+
extend T::Sig
|
9
|
+
|
10
|
+
include Singleton
|
11
|
+
|
12
|
+
Registry = T.type_alias { T::Array[T.class_of(Coercer)] }
|
13
|
+
|
14
|
+
DEFAULT_COERCERS = T.let([StringCoercer, IntegerCoercer, FloatCoercer, StructCoercer], Registry)
|
15
|
+
|
16
|
+
sig { void }
|
17
|
+
def initialize
|
18
|
+
@available = T.let(DEFAULT_COERCERS.clone, Registry)
|
19
|
+
end
|
20
|
+
|
21
|
+
sig { params(coercer: T.class_of(Coercer)).void }
|
22
|
+
def register(coercer)
|
23
|
+
@available.prepend(coercer)
|
24
|
+
end
|
25
|
+
|
26
|
+
sig { void }
|
27
|
+
def reset!
|
28
|
+
@available = DEFAULT_COERCERS.clone
|
29
|
+
end
|
30
|
+
|
31
|
+
sig { params(type: T::Class[T.anything]).returns(T.nilable(T.class_of(Coercer))) }
|
32
|
+
def select_coercer_by(type:)
|
33
|
+
@available.find { |coercer| coercer.new.used_for_type?(type) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -2,16 +2,18 @@
|
|
2
2
|
|
3
3
|
module Typed
|
4
4
|
module Coercion
|
5
|
-
class FloatCoercer
|
6
|
-
extend T::Sig
|
5
|
+
class FloatCoercer < Coercer
|
7
6
|
extend T::Generic
|
8
7
|
|
9
|
-
|
8
|
+
Target = type_member { {fixed: Float} }
|
10
9
|
|
11
|
-
|
10
|
+
sig { override.params(type: T::Class[T.anything]).returns(T::Boolean) }
|
11
|
+
def used_for_type?(type)
|
12
|
+
type == Float
|
13
|
+
end
|
12
14
|
|
13
15
|
sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
|
14
|
-
def
|
16
|
+
def coerce(field:, value:)
|
15
17
|
Success.new(Float(value))
|
16
18
|
rescue ArgumentError, TypeError
|
17
19
|
Failure.new(CoercionError.new("'#{value}' cannot be coerced into Float."))
|
@@ -2,16 +2,18 @@
|
|
2
2
|
|
3
3
|
module Typed
|
4
4
|
module Coercion
|
5
|
-
class IntegerCoercer
|
6
|
-
extend T::Sig
|
5
|
+
class IntegerCoercer < Coercer
|
7
6
|
extend T::Generic
|
8
7
|
|
9
|
-
|
8
|
+
Target = type_member { {fixed: Integer} }
|
10
9
|
|
11
|
-
|
10
|
+
sig { override.params(type: T::Class[T.anything]).returns(T::Boolean) }
|
11
|
+
def used_for_type?(type)
|
12
|
+
type == Integer
|
13
|
+
end
|
12
14
|
|
13
15
|
sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
|
14
|
-
def
|
16
|
+
def coerce(field:, value:)
|
15
17
|
Success.new(Integer(value))
|
16
18
|
rescue ArgumentError, TypeError
|
17
19
|
Failure.new(CoercionError.new("'#{value}' cannot be coerced into Integer."))
|
@@ -2,16 +2,18 @@
|
|
2
2
|
|
3
3
|
module Typed
|
4
4
|
module Coercion
|
5
|
-
class StringCoercer
|
6
|
-
extend T::Sig
|
5
|
+
class StringCoercer < Coercer
|
7
6
|
extend T::Generic
|
8
7
|
|
9
|
-
|
8
|
+
Target = type_member { {fixed: String} }
|
10
9
|
|
11
|
-
|
10
|
+
sig { override.params(type: T::Class[T.anything]).returns(T::Boolean) }
|
11
|
+
def used_for_type?(type)
|
12
|
+
type == String
|
13
|
+
end
|
12
14
|
|
13
15
|
sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
|
14
|
-
def
|
16
|
+
def coerce(field:, value:)
|
15
17
|
Success.new(String(value))
|
16
18
|
end
|
17
19
|
end
|
@@ -2,16 +2,18 @@
|
|
2
2
|
|
3
3
|
module Typed
|
4
4
|
module Coercion
|
5
|
-
class StructCoercer
|
6
|
-
extend T::Sig
|
5
|
+
class StructCoercer < Coercer
|
7
6
|
extend T::Generic
|
8
7
|
|
9
|
-
|
8
|
+
Target = type_member { {fixed: T::Struct} }
|
10
9
|
|
11
|
-
|
10
|
+
sig { override.params(type: T::Class[T.anything]).returns(T::Boolean) }
|
11
|
+
def used_for_type?(type)
|
12
|
+
!!(type < T::Struct)
|
13
|
+
end
|
12
14
|
|
13
15
|
sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
|
14
|
-
def
|
16
|
+
def coerce(field:, value:)
|
15
17
|
type = field.type
|
16
18
|
|
17
19
|
return Failure.new(CoercionError.new("Field type must inherit from T::Struct for Struct coercion.")) unless type < T::Struct
|
data/lib/typed/coercion.rb
CHANGED
@@ -4,23 +4,18 @@ module Typed
|
|
4
4
|
module Coercion
|
5
5
|
extend T::Sig
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
sig { params(coercer: T.class_of(Coercer)).void }
|
8
|
+
def self.register_coercer(coercer)
|
9
|
+
CoercerRegistry.instance.register(coercer)
|
10
|
+
end
|
11
|
+
|
11
12
|
sig { type_parameters(:U).params(field: Field, value: Value).returns(Result[Value, CoercionError]) }
|
12
13
|
def self.coerce(field:, value:)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
IntegerCoercer.coerce(field: field, value: value)
|
19
|
-
elsif field.type == Float
|
20
|
-
FloatCoercer.coerce(field: field, value: value)
|
21
|
-
else
|
22
|
-
Failure.new(CoercionNotSupportedError.new)
|
23
|
-
end
|
14
|
+
coercer = CoercerRegistry.instance.select_coercer_by(type: field.type)
|
15
|
+
|
16
|
+
return Failure.new(CoercionNotSupportedError.new) unless coercer
|
17
|
+
|
18
|
+
coercer.new.coerce(field: field, value: value)
|
24
19
|
end
|
25
20
|
end
|
26
21
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"release-type": "ruby",
|
3
|
+
"packages": {
|
4
|
+
".": {
|
5
|
+
"monorepo-tags": false,
|
6
|
+
"include-component-in-tag": false,
|
7
|
+
"prerelease": false,
|
8
|
+
"bump-minor-pre-major": true,
|
9
|
+
"bump-patch-for-minor-pre-major": true,
|
10
|
+
"package-name": "sorbet-schema",
|
11
|
+
"version-file": "lib/sorbet-schema/version.rb"
|
12
|
+
}
|
13
|
+
}
|
14
|
+
}
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max VelDink
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-03-
|
11
|
+
date: 2024-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sorbet-result
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '2.6'
|
69
|
-
description:
|
69
|
+
description:
|
70
70
|
email:
|
71
71
|
- maxveldink@gmail.com
|
72
72
|
executables: []
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- lib/sorbet-schema/version.rb
|
90
90
|
- lib/typed/coercion.rb
|
91
91
|
- lib/typed/coercion/coercer.rb
|
92
|
+
- lib/typed/coercion/coercer_registry.rb
|
92
93
|
- lib/typed/coercion/coercion_error.rb
|
93
94
|
- lib/typed/coercion/coercion_not_supported_error.rb
|
94
95
|
- lib/typed/coercion/float_coercer.rb
|
@@ -111,6 +112,8 @@ files:
|
|
111
112
|
- lib/typed/validations/validated_value.rb
|
112
113
|
- lib/typed/validations/validation_error.rb
|
113
114
|
- lib/typed/validations/validation_results.rb
|
115
|
+
- release-please-config.json
|
116
|
+
- release-please-manifest.json
|
114
117
|
- sorbet/config
|
115
118
|
- sorbet/rbi/annotations/rainbow.rbi
|
116
119
|
- sorbet/rbi/gems/.gitattributes
|
@@ -169,7 +172,7 @@ metadata:
|
|
169
172
|
homepage_uri: https://github.com/maxveldink/sorbet-schema
|
170
173
|
source_code_uri: https://github.com/maxveldink/sorbet-schema
|
171
174
|
changelog_uri: https://github.com/maxveldink/sorbet-schema/blob/main/CHANGELOG.md
|
172
|
-
post_install_message:
|
175
|
+
post_install_message:
|
173
176
|
rdoc_options: []
|
174
177
|
require_paths:
|
175
178
|
- lib
|
@@ -184,8 +187,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
187
|
- !ruby/object:Gem::Version
|
185
188
|
version: '0'
|
186
189
|
requirements: []
|
187
|
-
rubygems_version: 3.5.
|
188
|
-
signing_key:
|
190
|
+
rubygems_version: 3.5.3
|
191
|
+
signing_key:
|
189
192
|
specification_version: 4
|
190
193
|
summary: Serialization and deserialization library into Sorbet structs.
|
191
194
|
test_files: []
|