Skip to content

Add configuration with camelizing keys#160

Open
kortirso wants to merge 6 commits intoyosiat:masterfrom
kortirso:feature/configure_camelize_keys
Open

Add configuration with camelizing keys#160
kortirso wants to merge 6 commits intoyosiat:masterfrom
kortirso:feature/configure_camelize_keys

Conversation

@kortirso
Copy link

@kortirso kortirso commented Jul 17, 2024

close #52

to camelize keys rails application can have initializer

Panko.configure do |config|
  config.key_type = 'camelCase'
end

@yosiat what do you think?

Copy link
Owner

@yosiat yosiat left a comment

Choose a reason for hiding this comment

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

Thanks for contributing this!

Highly appreciated and looks good, suggesting few changes.

@@ -0,0 +1,30 @@
# frozen_string_literal: true
Copy link
Owner

@yosiat yosiat Sep 23, 2024

Choose a reason for hiding this comment

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

We should have documentation for this, can you please add?

If not, I'll add after this PR will be merged.

Copy link
Author

Choose a reason for hiding this comment

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

there is small documentation with example in lib/panko_serializer.rb how to use it

but probably you can add any documentation later

Copy link
Owner

Choose a reason for hiding this comment

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

I was talking about the documentation here: https://github.com/yosiat/panko_serializer/tree/master/docs

Attribute.new(name.to_s, alias_name)
end

def self.transform_key(name)
Copy link
Owner

Choose a reason for hiding this comment

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

can we handle the case where key_type is nil and just return the name as is?

Copy link
Author

Choose a reason for hiding this comment

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

and looks like I have added check before method calling if Panko.configuration.key_type, but I'll add in any case

Copy link
Author

Choose a reason for hiding this comment

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

and I think about memoization at this point, what do you think?

if developer serializes 1000 elements with the same key, no need to transform key 1000 times, 1 is enough

Copy link
Owner

Choose a reason for hiding this comment

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

  1. Why we need the if Panko.configuration.key_type in self.create , we already handle in transform_key now.
  2. and I think about memoization at this point, what do you think? - I don't think it's necessary, since attributes are creating when serializer is defined and not during serialization time, so you pay the "transformation cost" only at boot time.

class Attribute
def self.create(name, alias_name: nil)
alias_name = alias_name.to_s unless alias_name.nil?
alias_name = transform_key(name.to_s) if alias_name.nil? && Panko.configuration.key_type
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not entirely sure what the expected behavior should be when an alias is provided in snake case, but the configured key type is in camel case. What do you think? My suggestion is to camelize the alias if it's provided, but I'd like to hear your thoughts.

Copy link
Author

@kortirso kortirso Sep 24, 2024

Choose a reason for hiding this comment

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

I see your point, I think, while transforming keys is general setting for all keys and aliases is specific setting, then aliases should be in priority, like developer has general settings with transforming, but for specific key he can specify alias

and if you agree with this - then no changes is required in PR

Copy link
Owner

Choose a reason for hiding this comment

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

Agree with you, so please:

Copy link
Author

Choose a reason for hiding this comment

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

I added test for using configuration with alias

kortirso and others added 2 commits September 24, 2024 20:39
Co-authored-by: Yosi Attias <yosy101@gmail.com>
@kortirso kortirso requested a review from yosiat September 24, 2024 17:54
Copy link
Owner

@yosiat yosiat left a comment

Choose a reason for hiding this comment

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

This will be the last round of the CR.

Thanks again for your contribution.

Attribute.new(name.to_s, alias_name)
end

def self.transform_key(name)
Copy link
Owner

Choose a reason for hiding this comment

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

  1. Why we need the if Panko.configuration.key_type in self.create , we already handle in transform_key now.
  2. and I think about memoization at this point, what do you think? - I don't think it's necessary, since attributes are creating when serializer is defined and not during serialization time, so you pay the "transformation cost" only at boot time.

class Attribute
def self.create(name, alias_name: nil)
alias_name = alias_name.to_s unless alias_name.nil?
alias_name = transform_key(name.to_s) if alias_name.nil? && Panko.configuration.key_type
Copy link
Owner

Choose a reason for hiding this comment

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

Agree with you, so please:

@kortirso
Copy link
Author

kortirso commented Oct 3, 2024

This will be the last round of the CR.

Thanks again for your contribution.

@yosiat I made last changes, maybe it's better for you to change documentation in your way
thank you for considering these changes

@kortirso kortirso requested a review from yosiat October 3, 2024 08:36
@schovi
Copy link

schovi commented Jun 4, 2025

Hello, this PR seems to be stale, which is a big waste. Is possible to push the last mile? 🙏🏻 Can I help with that?

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.

Option to camelize keys

3 participants

Comments