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

Support alba transform keys in the type generation #14

Merged
merged 4 commits into from
Nov 26, 2024
Merged
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
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
Loading