Skip to content

Commit

Permalink
recover serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
gtrias committed Aug 22, 2024
1 parent 178218a commit 6472ca3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/public/repositories/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
# frozen_string_literal: strict
# typed: false

require_relative '../../runtime_generic'
Expand All @@ -17,7 +17,7 @@ module Base

abstract!

Entity = type_member
Entity = type_member(:out)

sig { returns(T.untyped) }
def self.entity
Expand Down
51 changes: 51 additions & 0 deletions lib/public/serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# typed: true

module ResourceRegistry
class Serializer
extend T::Sig

# Maybe this should be a generic?
sig { params(resource: Resource).void }
def initialize(resource:)
@resource = resource
end

sig { params(entity: T::Struct, tags: T::Set[Symbol]).returns(T::Hash[Symbol, T.untyped]) }
def serialize(entity:, tags:)
entity_methods = Set.new(entity.methods)

resource_schema
.properties
.each_with_object({}) do |property, acc|
# If property has serialization groups and they are not included in the tags this property is skipped
next if property.serialization_groups.any? && (property.serialization_groups & tags).none?

property_value =
if entity_methods.include?(property.name.to_sym)
entity.send(property.name)
elsif property.resolvable && (property.resolver&.values&.size || 0) > 1
# Review all this mess with resolvers
property.resolver&.map { |_, value| entity.send(value) }&.join(' ')
else
next
end

acc[property.name.to_sym] = recursive_serialization(property_value)
end
end

private

sig { params(property_value: T.untyped).returns(T.untyped) }
def recursive_serialization(property_value)
return property_value.map { |pv| recursive_serialization(pv) } if property_value.is_a?(Array)

property_value.respond_to?(:serialize, false) ? property_value.serialize : property_value
end

sig { returns(SchemaRegistry::Schema) }
def resource_schema
@resource.schema
end
end
end
4 changes: 0 additions & 4 deletions sorbet/rbi/todo.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

# typed: false

module ::Employees::Repositories::Employees; end
module ::Rails; end
module ::Rails::Engine; end
module ::Repositories::Base; end
Expand All @@ -23,17 +22,14 @@ module ResourceRegistry::Oas::ParameterMapper; end
module ResourceRegistry::Oas::PayloadMapper; end
module ResourceRegistry::OpenapiSpecificationGenerator::Oas::SchemaMapper; end
module ResourceRegistry::Relationship::Rails; end
module ResourceRegistry::Repositories::Base::Outcome; end
module ResourceRegistry::Repositories::Base::Permissions::Target; end
module ResourceRegistry::Repositories::Base::SerializationTags; end
module ResourceRegistry::Repositories::OutputContexts::Filter; end
module ResourceRegistry::Repositories::OutputContexts::PaginateCursor; end
module ResourceRegistry::Repositories::OutputContexts::PaginateOffset; end
module ResourceRegistry::Repositories::OutputContexts::Sort; end
module ResourceRegistry::Repositories::ReadOutputContext; end
module ResourceRegistry::Repositories::ReadResult::Outcome; end
module ResourceRegistry::Repositories::ReadResult::OutputContexts::PageInfoDto; end
module ResourceRegistry::Serializer; end
module ResourceRegistry::Tracer::Telemetry::Tracer; end
module SchemaRegistry::GenerateFromStruct::ActionDispatch::Http::UploadedFile; end
module T::CompatibilityPatches::RSpecCompatibility::MethodDoubleExtensions; end
Expand Down
7 changes: 3 additions & 4 deletions spec/public/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
end
let(:resource) do
ResourceRegistry::Resource.new(
repository_raw: Employees::Repositories::Employees.to_s,
repository_raw: DummyRepo.to_s,
capabilities: {
rest: ResourceRegistry::Capabilities::Rest.new,
power_bi: ResourceRegistry::Capabilities::PowerBi.new
},
verbs: {
},
verbs: {},
schema: schema
)
end
Expand All @@ -50,7 +49,7 @@

describe '#fetch_for_repository' do
it do
expect(registry.fetch_for_repository(Employees::Repositories::Employees)).to(eq(resource))
expect(registry.fetch_for_repository(DummyRepo)).to(eq(resource))
end
end

Expand Down

0 comments on commit 6472ca3

Please sign in to comment.