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

Add configuration with camelizing keys #160

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
node_modules

/docs/build/
.DS_Store
.cache/
.docusaurus/
10 changes: 9 additions & 1 deletion lib/panko/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
module Panko
class Attribute
def self.create(name, alias_name: nil)
alias_name = alias_name.to_s unless alias_name.nil?
alias_name = alias_name.nil? ? transform_key(name.to_s) : alias_name.to_s
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.

case Panko.configuration.key_type
when Panko::Configuration::CAMEL_CASE_LOWER then name.camelize(:lower)
when Panko::Configuration::CAMEL_CASE then name.camelize
else name
end
end

def ==(other)
return name.to_sym == other if other.is_a? Symbol
return name == other.name && alias_name == other.alias_name if other.is_a? Panko::Attribute
Expand Down
23 changes: 23 additions & 0 deletions lib/panko/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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


module Panko
class Configuration
InitializeError = Class.new(StandardError)

CAMEL_CASE_LOWER = "camelCase"
CAMEL_CASE = "CamelCase"
AVAILABLE_KEY_TYPES = [CAMEL_CASE_LOWER, CAMEL_CASE].freeze

attr_reader :key_type

def initialize
@key_type = nil
end

def key_type=(value)
raise InitializeError, "Invalid key type" unless AVAILABLE_KEY_TYPES.include?(key_type)

@key_type = value
end
end
end
20 changes: 20 additions & 0 deletions lib/panko_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "panko/version"
require "panko/configuration"
require "panko/attribute"
require "panko/association"
require "panko/serializer"
Expand All @@ -12,3 +13,22 @@
# C Extension
require "oj"
require "panko/panko_serializer"

module Panko
extend self

# Public: Configure Panko.
#
# Panko.configure do |config|
# config.key_type = 'camelCase'
# end
#
def configure
yield configuration
end

# Public: Returns Panko::Configuration instance.
def configuration
@configuration ||= Configuration.new
end
end
91 changes: 91 additions & 0 deletions spec/panko/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,95 @@ def created_at
end
end
end

context "key_type" do
let(:configuration) { Panko::Configuration.new }
let(:data) { {"created_at" => created_at} }
let(:created_at) { "2023-04-18T09:24:41+00:00" }

before do
allow(Panko).to receive_messages(configuration: configuration)
end

context "with camelCase" do
before { configuration.key_type = "camelCase" }

context "with key_type" do
let(:serializer_class) do
Class.new(Panko::Serializer) do
attributes :created_at
end
end

it "has createdAt" do
expect(data).to serialized_as(serializer_class,
"createdAt" => created_at)
end
end

context "with key_type + method_fields" do
let(:serializer_class) do
Class.new(Panko::Serializer) do
attributes :created_at

def created_at
"2023-04-18T09:24:41+00:00"
end
end
end

it "has createdAt" do
expect(data).to serialized_as(serializer_class,
"createdAt" => created_at)
end
end

context "with key_type and alias" do
let(:serializer_class) do
Class.new(Panko::Serializer) do
aliases({created_at: :CreatedAt})
end
end

it "alias has priority over configuration" do
expect(data).to serialized_as(serializer_class,
"CreatedAt" => created_at)
end
end
end

context "with CamelCase" do
before { configuration.key_type = "CamelCase" }

context "with key_type" do
let(:serializer_class) do
Class.new(Panko::Serializer) do
attributes :created_at
end
end

it "has CreatedAt" do
expect(data).to serialized_as(serializer_class,
"CreatedAt" => created_at)
end
end

context "with key_type + method_fields" do
let(:serializer_class) do
Class.new(Panko::Serializer) do
attributes :created_at

def created_at
"2023-04-18T09:24:41+00:00"
end
end
end

it "has CreatedAt" do
expect(data).to serialized_as(serializer_class,
"CreatedAt" => created_at)
end
end
end
end
end