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

DXCDT-56: Fixing @@ array replacement to work with and without wrapped quotes #421

Merged
merged 3 commits into from
Feb 24, 2022

Conversation

willvedd
Copy link
Contributor

@willvedd willvedd commented Feb 24, 2022

✏️ Changes

The way that the @@FOO@@ array variable replacement worked has caused a bit of confusion from users. Firstly, it's not obvious if these markers need to be wrapped in quotes or not. Without them, it invalidates the JSON file that they're used in, but if they do use quotes, the replacement won't work as expected. This can be seen in #415 and #184.

For example, as it currently is implemented, developers would need to have the following configurations:

{
  "web_origins": @@WEB_ORIGINS@@
}

This is invalid JSON and a bit of a wonky thing to force onto the developer. With this change, they can now use the array replacements with or without wrapped quotes:

{
   "grant_types": @@GRANT_TYPES@@,
   "web_origins": "@@WEB_ORIGINS@@"
}
 

The tricky part here is that this needs to work both for JSON and YAML input files. This fix is primarily for JSON configurations to maintain the validity of the file. However, YAML is a bit less strict about having the @@ array markers wrapped in quotes, however that is still supported, albeit not with a desired outcome. Examples of this can be seen in the test YAML test cases below.

A big bulk of this PR is adding tests to confirm and enforce the behavior that exists currently.

🎯 Testing

Separated out keywordArrayReplace and keywordStringReplace into their own functions so that they could have their own unit tests applied. Also added higher-level unit tests to the keywordReplace function to confirm expected results for both YAML and JSON inputs.

✅This change has unit test coverage

Will Vedder added 2 commits February 24, 2022 11:27
…target regex pattern with or without wrapped strings, added tests
@willvedd willvedd requested a review from sergiught February 24, 2022 17:05
@@ -4,18 +4,32 @@ import dotProp from 'dot-prop';
import _ from 'lodash';
import log from './logger';

export function keywordArrayReplace(input, mappings) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Separated out and exported to enable unit testing.

Comment on lines +10 to +11
const pattern = `@@${key}@@`;
const patternWithQuotes = `"${pattern}"`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The key functional change in this PR is using regex to replace the @@ pattern with and without wrapping quotes.

const patternWithQuotes = `"${pattern}"`;

const regex = new RegExp(`${patternWithQuotes}|${pattern}`, 'g');
input = input.replace(regex, JSON.stringify(mappings[key]));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The key difference between this function and the below keywordStringReplace function is the JSON.stringify call.

stringReplaceNoQuotes: ##STRING_REPLACEMENT##
stringReplaceWithQuotes: "##STRING_REPLACEMENT##"
arrayReplace: @@ARRAY_REPLACEMENT@@
arrayReplaceWithQuotes: "@@ARRAY_REPLACEMENT@@"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unlikely that someone would wrap this value in quotes when using YAML, but at least it's technically supported. I don't believe this behavior will interfere with any current customer workflows.

const inputJSON = '{ "arrayReplaceNoQuotes": @@ARRAY_REPLACEMENT@@, "arrayReplaceWithQuotes": "@@ARRAY_REPLACEMENT@@", "stringReplace": "##STRING_REPLACEMENT##", "noReplace": "OTHER_REPLACEMENT" }';
const output = utils.keywordReplace(inputJSON, mapping);

expect(() => JSON.parse(output)).to.not.throw();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the big test, just making sure the output JSON is validated.

Copy link
Contributor

@sergiught sergiught left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏻

…nction

Distinguishing between two loadFile functions
@willvedd willvedd merged commit 5145d3b into master Feb 24, 2022
@willvedd willvedd deleted the DXCDT-56-array-replacement-fix branch February 24, 2022 18:35
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.

2 participants