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

Update Configuration class to support merging objects in local config files #782

Merged
merged 10 commits into from
Jan 25, 2019

Conversation

ryanrath
Copy link
Contributor

Description

  • 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.
  • Updated the unit tests bootstrap.php so that we can use the
    integration_tests TestFiles helper class.

Motivation and Context

Config currently supports a couple of features that Configuration\Configuration does not.

  • Merging .d configuration files that contain '+' prefixed property names
  • Handling the 'extends' key / value pair
  • Retrieving which configuration options have been provided by which module.

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:

  • unit
  • component
  • integration
  • regression
  • UI

New tests were added to ensure, on a basic level, that Configuration can generate the same output as Config when presented with the same base configuration files. In this case both roles.json and datawarehouse.json are tested.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project as found in the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

- 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.
@ryanrath ryanrath requested a review from smgallo January 23, 2019 20:07
} else {
$this->logger->debug("Skip duplicate key in local config (overwrite == false)");
$msg = <<<TXT
Unable to merge files due to mismatched types.
Copy link
Contributor

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) === '+') {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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) {
Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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));
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe StripMergePrefixTransformer ?

Copy link
Contributor Author

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`
@smgallo smgallo added this to the 8.1.0 milestone Jan 25, 2019
@smgallo smgallo added enhancement Enhancement of the functionality of an existing feature Category:ETL Extract Transform Load labels Jan 25, 2019
@ryanrath ryanrath merged commit 7fc58d1 into ubccr:xdmod8.1 Jan 25, 2019
@smgallo smgallo changed the title Updating Configuration class to support merging objects w/ Tests Updating Configuration class to support merging objects Apr 10, 2019
@smgallo smgallo changed the title Updating Configuration class to support merging objects Update Configuration class to support merging objects Apr 10, 2019
@smgallo smgallo changed the title Update Configuration class to support merging objects Update Configuration class to support merging objects in local config files Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category:ETL Extract Transform Load enhancement Enhancement of the functionality of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants