Skip to content

Commit

Permalink
feat: Add more T::Struct extensions (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxveldink authored May 28, 2024
1 parent 1187fdd commit 163db1c
Show file tree
Hide file tree
Showing 49 changed files with 20,826 additions and 13,418 deletions.
70 changes: 36 additions & 34 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ GEM
specs:
ansi (1.5.0)
ast (2.4.2)
bigdecimal (3.1.7)
bigdecimal (3.1.8)
builder (3.2.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
erubi (1.12.0)
io-console (0.7.2)
irb (1.12.0)
rdoc
irb (1.13.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
minitest (5.22.3)
minitest (5.23.1)
minitest-focus (1.4.0)
minitest (>= 4, < 6)
minitest-reporters (1.6.1)
Expand All @@ -35,25 +35,26 @@ GEM
ruby-progressbar
netrc (0.11.0)
parallel (1.24.0)
parser (3.3.0.5)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
prism (0.24.0)
prism (0.29.0)
psych (5.1.2)
stringio
racc (1.7.3)
racc (1.8.0)
rainbow (3.1.1)
rake (13.2.1)
rbi (0.1.10)
prism (>= 0.18.0, < 0.25)
rbi (0.1.13)
prism (>= 0.18.0, < 1.0.0)
sorbet-runtime (>= 0.5.9204)
rdoc (6.6.3.1)
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.9.0)
reline (0.5.2)
regexp_parser (2.9.2)
reline (0.5.7)
io-console (~> 0.5)
rexml (3.2.6)
rubocop (1.62.1)
rexml (3.2.8)
strscan (>= 3.0.9)
rubocop (1.63.5)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand All @@ -64,48 +65,49 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.2)
parser (>= 3.3.0.4)
rubocop-performance (1.20.2)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-performance (1.21.0)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-sorbet (0.7.8)
rubocop (>= 0.90.0)
ruby-progressbar (1.13.0)
sorbet (0.5.11347)
sorbet-static (= 0.5.11347)
sorbet (0.5.11391)
sorbet-static (= 0.5.11391)
sorbet-result (1.1.0)
sorbet-runtime (~> 0.5)
sorbet-runtime (0.5.11347)
sorbet-static (0.5.11347-universal-darwin)
sorbet-static (0.5.11347-x86_64-linux)
sorbet-static-and-runtime (0.5.11347)
sorbet (= 0.5.11347)
sorbet-runtime (= 0.5.11347)
sorbet-runtime (0.5.11391)
sorbet-static (0.5.11391-universal-darwin)
sorbet-static (0.5.11391-x86_64-linux)
sorbet-static-and-runtime (0.5.11391)
sorbet (= 0.5.11391)
sorbet-runtime (= 0.5.11391)
sorbet-struct-comparable (1.3.0)
sorbet-runtime (>= 0.5)
spoom (1.3.0)
spoom (1.3.2)
erubi (>= 1.10.0)
prism (>= 0.19.0)
sorbet-static-and-runtime (>= 0.5.10187)
thor (>= 0.19.2)
standard (1.35.1)
standard (1.36.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.62.0)
rubocop (~> 1.63.0)
standard-custom (~> 1.0.0)
standard-performance (~> 1.3)
standard-performance (~> 1.4)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.3.1)
standard-performance (1.4.0)
lint_roller (~> 1.1)
rubocop-performance (~> 1.20.2)
rubocop-performance (~> 1.21.0)
standard-sorbet (0.0.2)
lint_roller (~> 1.1)
rubocop-sorbet (~> 0.7.0)
stringio (3.1.0)
tapioca (0.13.3)
strscan (3.1.0)
tapioca (0.14.2)
bundler (>= 2.2.25)
netrc (>= 0.11.0)
parallel (>= 1.21.0)
Expand All @@ -120,7 +122,7 @@ GEM
yard-sorbet (0.8.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
zeitwerk (2.6.13)
zeitwerk (2.6.15)

PLATFORMS
arm64-darwin-22
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Sorbet Schema is designed to be compatible with Sorbet's `T::Struct` class, and

### Getting Started

While you can directly define a `Typed::Schema` to be used for your serialization needs, you'll typically use the provider helper class method to generate a `Schema` from an existing `T::Struct`.
While you can directly define a `Typed::Schema` to be used for your serialization needs, you'll typically use the provided helper class method to generate a `Schema` from an existing `T::Struct`.

```ruby
class Person < T::Struct
Expand All @@ -44,6 +44,16 @@ result = json_serializer.serialize(max)
result.payload # == '{"name":"Max","age":29}'
```

Alternatively, you can use the built-in helper methods added to `T::Struct`s to quickly use the built-in serializers.

```ruby
result = Person.deserialize_from(:json, '{"name":"Max","age":29}')
max = result.payload # == Person.new(name: "Max", age: 29)

result = max.serialize_to(:json)
result.payload # == '{"name":"Max","age":29}'
```

Notice that both `deserialize` and `serialize` return `Typed::Result`s (from the [sorbet-result gem](https://github.com/maxveldink/sorbet-result)) that need to be checked for success or failure before being used. Check out that gem's README for more information on how to interact with `Result`s.

One benefit of using `Result`s is we can add much more details information about why a format is unsuccessfully deserialized or serialized, to provide call sites with more information for error handling, messaging and formatting.
Expand Down
32 changes: 29 additions & 3 deletions lib/sorbet-schema/t/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,35 @@ module T
class Struct
extend T::Sig

sig { overridable.returns(Typed::Schema) }
def self.schema
Typed::Schema.from_struct(self)
class << self
extend T::Sig

sig { overridable.returns(Typed::Schema) }
def schema
Typed::Schema.from_struct(self)
end

sig { params(type: Symbol).returns(Typed::Serializer[T.untyped, T.untyped]) }
def serializer(type)
case type
when :hash
Typed::HashSerializer.new(schema:)
when :json
Typed::JSONSerializer.new(schema:)
else
raise ArgumentError, "unknown serializer for #{type}"
end
end

sig { params(serializer_type: Symbol, source: T.untyped).returns(Typed::Serializer::DeserializeResult) }
def deserialize_from(serializer_type, source)
serializer(serializer_type).deserialize(source)
end
end

sig { params(serializer_type: Symbol).returns(Typed::Result[T.untyped, Typed::SerializeError]) }
def serialize_to(serializer_type)
self.class.serializer(serializer_type).serialize(self)
end
end
end
2 changes: 1 addition & 1 deletion lib/typed/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Serializer
Input = type_member
Output = type_member
Params = T.type_alias { T::Hash[Symbol, T.untyped] }
DeserializeResult = T.type_alias { Typed::Result[T::Struct, DeserializeError] }
DeserializeResult = T.type_alias { Result[T::Struct, DeserializeError] }

sig { returns(Schema) }
attr_reader :schema
Expand Down
1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 163db1c

Please sign in to comment.