Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alba): Handle inline associations #7

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/typelizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

require_relative "typelizer/railtie" if defined?(Rails)

require "logger"

module Typelizer
class << self
def enabled?
Expand Down
6 changes: 5 additions & 1 deletion lib/typelizer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class Config < Struct.new(
class << self
def instance
@instance ||= new(
serializer_name_mapper: ->(serializer) { serializer.name.ends_with?("Serializer") ? serializer.name.delete_suffix("Serializer") : serializer.name.delete_suffix("Resource") },
serializer_name_mapper: ->(serializer) do
return "" if serializer.name.nil?

serializer.name.ends_with?("Serializer") ? serializer.name&.delete_suffix("Serializer") : serializer.name&.delete_suffix("Resource")
end,
serializer_model_mapper: ->(serializer) do
base_class = serializer_name_mapper.call(serializer)
Object.const_get(base_class) if Object.const_defined?(base_class)
Expand Down
5 changes: 3 additions & 2 deletions lib/typelizer/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def target_serializers
raise ArgumentError, "Please ensure all your serializers include Typelizer::DSL." if base_classes.none?
end

(base_classes + base_classes.flat_map(&:descendants)).uniq.sort_by(&:name)
.reject { |serializer| Typelizer.reject_class.call(serializer: serializer) }
(base_classes + base_classes.flat_map(&:descendants)).uniq
.reject { |serializer| Typelizer.reject_class.call(serializer: serializer) || serializer.name.nil? }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a condition to check anonymous serializer.

.sort_by(&:name)
end

def read_serializers(files = nil)
Expand Down
22 changes: 22 additions & 0 deletions lib/typelizer/inline_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Typelizer
# InlineType is mainly used with Alba plugin to represent inline associations.
# `name `method` is the same interface as `Interface` class.
class InlineType
TEMPLATE = <<~ERB.strip
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heredoc is used as a template. This can be a separate file, but it is short enough.

{
<%- properties.each do |property| %>
<%= property %>;
<% end %>
}
ERB
def initialize(serializer:, config:)
@serializer = serializer
@config = config
end

def name
properties = SerializerPlugins::Alba.new(serializer: @serializer, config: @config).properties
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of dirty hack, but it's recursive so that inline associations can be deeply nested.

ERB.new(TEMPLATE, trim_mode: "-").result_with_hash(properties: properties)
end
end
end
1 change: 1 addition & 0 deletions lib/typelizer/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def imports
.filter_map { |interface| interface.name if interface.name != name }

custom_type_imports = attribute_types
.reject { |type| type.is_a?(InlineType) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the next line InlineType causes an error, so we need to reject it here.

.flat_map { |type| extract_typescript_types(type.to_s) }
.uniq
.reject { |type| global_type?(type) }
Expand Down
4 changes: 3 additions & 1 deletion lib/typelizer/serializer_plugins/alba.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "base"
require_relative "../inline_type"

module Typelizer
module SerializerPlugins
Expand Down Expand Up @@ -74,9 +75,10 @@ def build_property(name, attr, **options)
)
when ::Alba::Association
resource = attr.instance_variable_get(:@resource)
type = (resource.is_a?(Class) && !resource.name.nil?) ? Interface.new(serializer: resource) : InlineType.new(serializer: resource, config: {})
Property.new(
name: name,
type: Interface.new(serializer: resource),
type: type,
optional: false,
nullable: false,
multi: false, # we override this in typelize_method_transform
Expand Down
Loading