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

Conversation

patvice
Copy link
Contributor

@patvice patvice commented Oct 29, 2024

Hey! I ran into an issue when trying to get typelizer up and running on an exciting project.

Currently, the Alba serializer plugin doesn't support transform_keys attribute (Alba doc link). This update uses Alba's key transform method to use the specified transform type if present for both the root key and attributes.

Example:

Ruby Resource

class AdminResource
  include Alba::Resource
  include Typelizer::DSL
  
  attributes full_name: [String, true]
  
  transform_keys :lower_camel
end

Generated TS output

// Typelizer digest ebf32772afe4c2bbf03066005cc6884e
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

type Admin = {
  fullName: string;
}

export default Admin;

@skryukov
Copy link
Owner

Hey @patvice! Thanks for another great PR!

I'm definitely eager to add this to the gem, but I need some time to evaluate if we want to expose some kind of public API at the plugin level for key transformation. 🤔 I'll try to play with this over the week.

@patvice
Copy link
Contributor Author

patvice commented Oct 29, 2024

Hey @skryukov! Interesting idea. We originally found this issue when we realized that our typed values were not what our serializer was producing.

However, I know other projects that do the key transforms outside of the serializer to an additional public option would make sense to me.

@skryukov
Copy link
Owner

Yup, another thing is to ensure we play nice with config.properties_transformer = ->(properties) { ... } (should be fine, just a note to future self), which allows achieving case switching in a "manual mode" right now.

@patvice
Copy link
Contributor Author

patvice commented Oct 29, 2024

Good point -- I didn't have that option on when I tested my change. I'll make sure that's working.

@patvice
Copy link
Contributor Author

patvice commented Oct 31, 2024

Just to follow up with this - I was able to do some testing, and it seems like config.properties_transformer = ->(properties) { ... } works as I would have expected. The name is transformed before it's passed properties_transformer, in case the user would like to do something else with the name at that point.

Example of an output of what's passed to properties_transformer my AdminResource example:

[#<struct Typelizer::Property
  name="fullName",
  type=:string,
  optional=false,
  nullable=false,
  multi=false,
  column_name="fullName">]

@patvice patvice closed this Oct 31, 2024
@patvice
Copy link
Contributor Author

patvice commented Oct 31, 2024

Oops closed this by mistake

@patvice patvice reopened this Oct 31, 2024
@skryukov skryukov merged commit 8cce3a2 into skryukov:main Nov 26, 2024
10 checks passed
@skryukov
Copy link
Owner

Ok, I decided to merge it since props transformation might be too specific for each serialization gem/application, so we won't expose that at the plugin level. Thanks for your PR @patvice! I will add some tests and hopefully support for other plugins as well before the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants