Skip to content

Commit

Permalink
fix: add sorbet-schema shim for better T:Struct extension compatabili…
Browse files Browse the repository at this point in the history
…ty (#123)
  • Loading branch information
maxveldink authored Aug 14, 2024
1 parent 8572e4e commit b47cafb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
12 changes: 2 additions & 10 deletions lib/sorbet-schema/t/struct.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
# typed: strict
# typed: true

module T
class Struct
extend T::Sig

class << self
extend T::Sig

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

sig { params(type: Symbol, options: T::Hash[Symbol, T.untyped]).returns(Typed::Serializer[T.untyped, T.untyped]) }
def serializer(type, options: {})
case type
when :hash
Expand All @@ -24,13 +18,11 @@ def serializer(type, options: {})
end
end

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

sig { params(serializer_type: Symbol, options: T::Hash[Symbol, T.untyped]).returns(Typed::Result[T.untyped, Typed::SerializeError]) }
def serialize_to(serializer_type, options: {})
self.class.serializer(serializer_type, options:).serialize(self)
end
Expand Down
21 changes: 21 additions & 0 deletions rbi/sorbet-schema.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# typed: strict

class T::Struct
class << self
sig { overridable.returns(Typed::Schema) }
def schema
end

sig { params(type: Symbol, options: T::Hash[Symbol, T.untyped]).returns(Typed::Serializer[T.untyped, T.untyped]) }
def serializer(type, options: {})
end

sig { params(serializer_type: Symbol, source: T.untyped, options: T::Hash[Symbol, T.untyped]).returns(Typed::Result[T.attached_class, Typed::DeserializeError]) }
def deserialize_from(serializer_type, source, options: {})
end
end

sig { params(serializer_type: Symbol, options: T::Hash[Symbol, T.untyped]).returns(Typed::Result[T.untyped, Typed::SerializeError]) }
def serialize_to(serializer_type, options: {})
end
end
2 changes: 1 addition & 1 deletion sorbet-schema.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ["lib", "rbi"]

spec.add_runtime_dependency "sorbet-result", "~> 1.1"
spec.add_runtime_dependency "sorbet-runtime", "~> 0.5"
Expand Down

0 comments on commit b47cafb

Please sign in to comment.