-
Notifications
You must be signed in to change notification settings - Fork 21
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 root argument #32
Merged
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6a68a03
Add root argument: Add specs and basic argument carrying
chrisatanasian b5c35c8
Add root argument: Refactor Surrealist::Copier#deep_copy
chrisatanasian e539eac
Add root argument: Add logic and get tests to pass
chrisatanasian d31054c
Add root argument: Add more thorough tests
chrisatanasian 02ccef2
Add root argument: Update README and comments
chrisatanasian 189e9ce
Merge branch 'master' into add_root_argument
chrisatanasian cd63060
Add root argument: Remove unused parameter
chrisatanasian 9aee27a
Add root argument: Undo README whitespace changes
chrisatanasian e316b77
Add root argument: Fix rubocop errors
chrisatanasian f96e9b0
Add root argument: Clean up README
chrisatanasian cb3ecd9
Add root argument: Add missing param name and DRY methods
chrisatanasian 7fe664e
Add root argument: Raise if root is not nil, non-empty string, or sym…
chrisatanasian f5986c1
Merge branch 'master' into add_root_argument
chrisatanasian b481a88
Add root argument: Use struct to clean up specs
chrisatanasian 7c61abc
Add root argument: Make root override include_root and include_namesp…
chrisatanasian eedb1df
Add root argument: Strip root of whitespaces
chrisatanasian da1054a
Merge branch 'add_root_argument' of https://github.com/chrisatanasian…
chrisatanasian 78375c7
Add root argument: Update README.md to match logic
chrisatanasian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,40 @@ class << self | |
def deep_copy(hash:, klass: false, carrier:) | ||
namespaces_condition = carrier.include_namespaces || carrier.namespaces_nesting_level != DEFAULT_NESTING_LEVEL # rubocop:disable Metrics/LineLength | ||
|
||
return copy_hash(hash) unless carrier.include_root || namespaces_condition | ||
if !klass && (carrier.include_root || namespaces_condition) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice decision! 👍 |
||
Surrealist::ExceptionRaiser.raise_unknown_root! | ||
end | ||
|
||
copy_before_root = copied_and_possibly_wrapped_hash(hash, klass, carrier, namespaces_condition) | ||
|
||
if carrier.root | ||
wrap_schema_into_root(schema: copy_before_root, carrier: carrier, root: carrier.root.to_s) | ||
else | ||
copy_before_root | ||
end | ||
end | ||
|
||
Surrealist::ExceptionRaiser.raise_unknown_root! unless klass | ||
private | ||
|
||
# Deeply copies the schema hash and wraps it if there is a need to. | ||
# | ||
# @param [Object] hash object to be copied. | ||
# @param [String] klass instance's class name. | ||
# @param [Object] carrier instance of Carrier class that carries arguments passed to +surrealize+ | ||
# @param [Bool] namespaces_condition whether to wrap into namespace. | ||
# | ||
# @return [Hash] deeply copied hash, possibly wrapped. | ||
def copied_and_possibly_wrapped_hash(hash, klass, carrier, namespaces_condition) | ||
if namespaces_condition | ||
wrap_schema_into_namespace(schema: hash, klass: klass, carrier: carrier) | ||
elsif carrier.include_root | ||
wrap_schema_into_root(schema: hash, klass: klass, carrier: carrier) | ||
actual_class = Surrealist::StringUtils.extract_class(klass) | ||
wrap_schema_into_root(schema: hash, carrier: carrier, root: actual_class) | ||
else | ||
copy_hash(hash) | ||
end | ||
end | ||
|
||
private | ||
|
||
# Goes through the hash recursively and deeply copies it. | ||
# | ||
# @param [Hash] hash the hash to be copied. | ||
|
@@ -42,16 +63,15 @@ def copy_hash(hash, wrapper: {}) | |
# Wraps schema into a root key if `include_root` is passed to Surrealist. | ||
# | ||
# @param [Hash] schema schema hash. | ||
# @param [String] klass name of the class where schema is defined. | ||
# @param [Object] carrier instance of Carrier class that carries arguments passed to +surrealize+ | ||
# @param [String] root what the schema will be wrapped into | ||
# | ||
# @return [Hash] a hash with schema wrapped inside a root key. | ||
def wrap_schema_into_root(schema:, klass:, carrier:) | ||
actual_class = Surrealist::StringUtils.extract_class(klass) | ||
def wrap_schema_into_root(schema:, carrier:, root:) | ||
root_key = if carrier.camelize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks very similar to |
||
Surrealist::StringUtils.camelize(actual_class, false).to_sym | ||
Surrealist::StringUtils.camelize(root, false).to_sym | ||
else | ||
Surrealist::StringUtils.underscore(actual_class).to_sym | ||
Surrealist::StringUtils.underscore(root).to_sym | ||
end | ||
result = Hash[root_key => {}] | ||
copy_hash(schema, wrapper: result[root_key]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
VALID_PARAMS = [ | ||
{ camelize: true, include_namespaces: true, include_root: true, namespaces_nesting_level: 3 }, | ||
{ camelize: false, include_namespaces: true, include_root: true, namespaces_nesting_level: 3 }, | ||
{ camelize: false, include_namespaces: false, include_root: true, namespaces_nesting_level: 3 }, | ||
{ camelize: false, include_namespaces: false, include_root: false, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: false, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: true, include_root: false, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, namespaces_nesting_level: 435 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, namespaces_nesting_level: 666 }, | ||
{ camelize: true, include_namespaces: true, include_root: true, | ||
root: 'root', namespaces_nesting_level: 3 }, | ||
{ camelize: false, include_namespaces: true, include_root: true, | ||
root: :root, namespaces_nesting_level: 3 }, | ||
{ camelize: false, include_namespaces: false, include_root: true, | ||
root: nil, namespaces_nesting_level: 3 }, | ||
{ camelize: false, include_namespaces: false, include_root: false, | ||
root: :root, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: false, | ||
root: 'root', namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: true, include_root: false, | ||
root: nil, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: 'root', namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: :root, namespaces_nesting_level: 435 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: nil, namespaces_nesting_level: 666 }, | ||
].freeze | ||
|
||
INVALID_PARAMS = [ | ||
{ camelize: 'NO', include_namespaces: false, include_root: true, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: 'false', include_root: true, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, namespaces_nesting_level: 0 }, | ||
{ camelize: true, include_namespaces: false, include_root: false, namespaces_nesting_level: -3 }, | ||
{ camelize: true, include_namespaces: false, include_root: 'yep', namespaces_nesting_level: 3 }, | ||
{ camelize: 'NO', include_namespaces: false, include_root: true, namespaces_nesting_level: '3' }, | ||
{ camelize: 'NO', include_namespaces: false, include_root: true, namespaces_nesting_level: 3.14 }, | ||
{ camelize: Integer, include_namespaces: false, include_root: true, namespaces_nesting_level: 3 }, | ||
{ camelize: 'NO', include_namespaces: 'no', include_root: true, namespaces_nesting_level: '3.4' }, | ||
{ camelize: 'f', include_namespaces: false, include_root: 't', namespaces_nesting_level: true }, | ||
{ camelize: 'NO', include_namespaces: false, include_root: true, | ||
root: 'root', namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: 'false', include_root: true, | ||
root: :root, namespaces_nesting_level: 3 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: 'root', namespaces_nesting_level: 0 }, | ||
{ camelize: true, include_namespaces: false, include_root: false, | ||
root: :root, namespaces_nesting_level: -3 }, | ||
{ camelize: true, include_namespaces: false, include_root: 'yep', | ||
root: 'root', namespaces_nesting_level: 3 }, | ||
{ camelize: 'NO', include_namespaces: false, include_root: true, | ||
root: :root, namespaces_nesting_level: '3' }, | ||
{ camelize: 'NO', include_namespaces: false, include_root: true, | ||
root: 'root', namespaces_nesting_level: 3.14 }, | ||
{ camelize: Integer, include_namespaces: false, include_root: true, | ||
root: :root, namespaces_nesting_level: 3 }, | ||
{ camelize: 'NO', include_namespaces: 'no', include_root: true, | ||
root: 'root', namespaces_nesting_level: '3.4' }, | ||
{ camelize: 'f', include_namespaces: false, include_root: 't', | ||
root: :root, namespaces_nesting_level: true }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: '', namespaces_nesting_level: 666 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: 3, namespaces_nesting_level: 666 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: -3, namespaces_nesting_level: 666 }, | ||
{ camelize: true, include_namespaces: false, include_root: true, | ||
root: 3.14, namespaces_nesting_level: 666 }, | ||
].freeze |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to add sanitization of
root
parameter to#sanitize!
method and specs for it (spec/carrier_spec.rb
). Right now there are specs only for strings and nil. It should work with symbols as well (Something.surrealize(root: :wrapper)
). I suggest that we raiseArgumentError
ifroot
is anything except non-empty string or a symbol (Class, Integer and so on).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
nil
? Seems like the way the code path is setup,Surrealist#call
is always expecting something for all of its arguments. In the case ofinclude_root
, for example, this is fine because it just defaults to false, which does nothing. But if only non-empty string and a symbol are allowed forroot
, then there is no way to prevent the schema from being wrapped intoroot
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good point. There are two ways here:
root
argument to''
instead ofnil
and then validate it on not being anything except string or symbol.root
argument as it is (nil
), and then validate it on being eithernil
, non-empty string or a symbol.I think I prefer the second option, it feels more naturally to have
nil
if we don't want to do anything. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think '2' is better and less arbitrary than empty string. I think the validation/sanitation needs to considers blank strings/symbols. We need to disallow things like " " of :" ", and maybe even consider stripping white space from these args: " hello " => "hello".
This may be something that we should also go back and check on for other params potentially @nesaulov ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree,
nil
seems more natural to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AlessandroMinali yes, you are right, we need to strip string argument. This is not relevant for other arguments, because they are either booleans or integers. @chrisatanasian could you please add a method in
Carrier.sanitize!
that will striproot
argument?And also add specs for this case:
Animal.surrealize(root: ' wat ') #=> '{ "wat" => {...} }'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.