-
Notifications
You must be signed in to change notification settings - Fork 68
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
Update Configuration class to support merging objects in local config files #782
Conversation
- Updated `Configuration\Configuration` w/ the ability to merge objects. This is so that `Configuration\Configuration` can eventually replace `Config`. - Added tests to `EtlConfigurationTest` that ensure `Configuration\Configuration` can generate the same json as `Config`. - The reason these tests have been placed here instead of `ConfigurationTest` is documented here: https://app.asana.com/0/807629084565719/1101232922862525/f - Updated the unit tests `bootstrap.php` so that we can use the integration_tests `TestFiles` helper class.
} else { | ||
$this->logger->debug("Skip duplicate key in local config (overwrite == false)"); | ||
$msg = <<<TXT | ||
Unable to merge files due to mismatched types. |
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.
The files will still be merged per se, but we want to warn the user so they can fix the issue. This can be made more succinct as:
sprintf(
"Type mismatch. Unable to merge local value for key '%s' (type: %s) with global value (type: %s) from local file %s",
$k,
gettype($v),
gettype($this->transformedConfig->$k),
$localConfigObj->getFilename()
);
@@ -490,12 +490,38 @@ protected function merge(Configuration $localConfigObj, $overwrite = false) | |||
// it. | |||
|
|||
foreach ( $localConfigObj->getTransformedConfig() as $k => $v ) { | |||
|
|||
// Normalize incoming keys starting w/ '+' | |||
if (substr($k, 0, 1) === '+') { |
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.
This is specific to the XDMoD configuration files rather than the generic config files. Let's handle this using a iConfigFileKeyTransformer
then we can assume that it has been stripped before we get to this stage.
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.
*facepalm* yeah, completely forgot about that part of the convo. I'll add the transformer and push it up shortly.
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.
You should be able to use the CommentTransformer with minimal changes to suit this.
{ | ||
$properties = get_object_vars($incoming); | ||
foreach($properties as $property => $incomingValue) { | ||
if (strpos($property, '+') !== false) { |
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.
If we use a transformer as mentioned above, we can get rid of this check.
} elseif (is_array($existingValue) && is_array($incomingValue)) { | ||
$existing->$property = array_merge($existingValue, $incomingValue); | ||
} else { | ||
$existing->$property = $incomingValue; |
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.
Don't we need the check that both of these values are scalars? Otherwise if the existing value is an object and the incoming value is a scalar we will hit this clause. We should provide a warning here as well if we have a type mismatch.
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.
Actually, if we fix up this method can't we just call it from the merge()
method and get rid of the if-then-else there?
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 we still need to be able to handle types other than Object at the top level but it's entirely possible I'm missing something.
- Added the `PlusKeyTransformer.php` to handle '+' prefixed keys instead of handling it in the merge step of `Configuration`. - Updated `Configuration::merge` so that it: - Only allows merging of like-type values per key - Recursively merges objects
@@ -0,0 +1,38 @@ | |||
<?php | |||
|
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 a docblock here explaining what the plus key represents and how we're transforming it (e.g., why it is being removed - because merge is the default behavior of Configuration).
@@ -390,6 +390,7 @@ protected function preTransformTasks() | |||
{ | |||
$this->addKeyTransformer(new CommentTransformer($this->logger)); | |||
$this->addKeyTransformer(new JsonReferenceTransformer($this->logger)); | |||
$this->addKeyTransformer(new PlusKeyTransformer($this->logger)); |
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.
Is there a better name for this transformer that hints at its function?
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.
lol I'm sure there is, I honestly wasn't sure what to go with. Maybe, MergeKeyTransformer
or something similar?
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.
Maybe StripMergePrefixTransformer
?
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.
That sounds good. I'll update the name + add the docblock in the next commit.
- Renamed `PlusKeyTransformer` -> `StripMergePrefixTransformer` - Added a doc block to `StripMergePrefixTransformer`
Description
Configuration\Configuration
w/ the ability to merge objects. This isso that
Configuration\Configuration
can eventually replaceConfig
.EtlConfigurationTest
that ensureConfiguration\Configuration
can generate the same json asConfig
.ConfigurationTest
is documented here:
https://app.asana.com/0/807629084565719/1101232922862525/f
bootstrap.php
so that we can use theintegration_tests
TestFiles
helper class.Motivation and Context
Config
currently supports a couple of features thatConfiguration\Configuration
does not.This PR implements the first of these features ( Merging '+' prefixed properties ). Future PR's will address the remaining features.
Tests performed
All existing tests were performed:
New tests were added to ensure, on a basic level, that
Configuration
can generate the same output asConfig
when presented with the same base configuration files. In this case bothroles.json
anddatawarehouse.json
are tested.Types of changes
Checklist: