From b47cafb9820141c7e8e7759e3a561f23bc01f9aa Mon Sep 17 00:00:00 2001 From: Max VelDink Date: Wed, 14 Aug 2024 11:31:50 -0400 Subject: [PATCH] fix: add sorbet-schema shim for better T:Struct extension compatability (#123) --- lib/sorbet-schema/t/struct.rb | 12 ++---------- rbi/sorbet-schema.rbi | 21 +++++++++++++++++++++ sorbet-schema.gemspec | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 rbi/sorbet-schema.rbi diff --git a/lib/sorbet-schema/t/struct.rb b/lib/sorbet-schema/t/struct.rb index 33fc689..935efe7 100644 --- a/lib/sorbet-schema/t/struct.rb +++ b/lib/sorbet-schema/t/struct.rb @@ -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 @@ -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 diff --git a/rbi/sorbet-schema.rbi b/rbi/sorbet-schema.rbi new file mode 100644 index 0000000..979f959 --- /dev/null +++ b/rbi/sorbet-schema.rbi @@ -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 diff --git a/sorbet-schema.gemspec b/sorbet-schema.gemspec index 6e207bd..8e8acb4 100644 --- a/sorbet-schema.gemspec +++ b/sorbet-schema.gemspec @@ -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"