Skip to content

Commit

Permalink
Support alba transform keys in the type generation (#14)
Browse files Browse the repository at this point in the history
* Support alba transform keys in the type generation

* fixed linter

* corrected autoformat mistake

* added missing commas
  • Loading branch information
patvice authored Nov 26, 2024
1 parent c8fcf9a commit 8cce3a2
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/typelizer/serializer_plugins/alba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ class Alba < Base

def properties
serializer._attributes.map do |name, attr|
build_property(name, attr)
key = if has_transform_key?(serializer)
fetch_key(serializer, name)
else
name
end

build_property(key, attr)
end
end

Expand All @@ -34,7 +40,12 @@ def typelize_method_transform(method:, name:, binding:, type:, attrs:)
end

def root_key
serializer.new({}).send(:_key)
root = serializer.new({}).send(:_key)
if !root.nil? && has_transform_key?(serializer) && should_transform_root_key?(serializer)
fetch_key(serializer, root)
else
root
end
end

def meta_fields
Expand Down Expand Up @@ -112,6 +123,18 @@ def build_property(name, attr, **options)
end
end

def has_transform_key?(serializer)
serializer._transform_type != :none
end

def should_transform_root_key?(serializer)
serializer._transforming_root_key
end

def fetch_key(serializer, key)
::Alba.transform_key(key, transform_type: serializer._transform_type)
end

private

def ts_mapper
Expand Down

0 comments on commit 8cce3a2

Please sign in to comment.