diff --git a/lib/public/repositories/base.rb b/lib/public/repositories/base.rb index 3afbb75..182e0fb 100644 --- a/lib/public/repositories/base.rb +++ b/lib/public/repositories/base.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: true +# frozen_string_literal: strict # typed: false require_relative '../../runtime_generic' @@ -17,7 +17,7 @@ module Base abstract! - Entity = type_member + Entity = type_member(:out) sig { returns(T.untyped) } def self.entity diff --git a/lib/public/serializer.rb b/lib/public/serializer.rb new file mode 100644 index 0000000..e7df035 --- /dev/null +++ b/lib/public/serializer.rb @@ -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 diff --git a/sorbet/rbi/todo.rbi b/sorbet/rbi/todo.rbi index 5b27742..17cba7e 100644 --- a/sorbet/rbi/todo.rbi +++ b/sorbet/rbi/todo.rbi @@ -4,7 +4,6 @@ # typed: false -module ::Employees::Repositories::Employees; end module ::Rails; end module ::Rails::Engine; end module ::Repositories::Base; end @@ -23,7 +22,6 @@ 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 @@ -31,9 +29,7 @@ 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 diff --git a/spec/public/registry_spec.rb b/spec/public/registry_spec.rb index 47fa5e7..5d5c76d 100644 --- a/spec/public/registry_spec.rb +++ b/spec/public/registry_spec.rb @@ -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 @@ -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