Skip to content

Commit

Permalink
property end comma from v2
Browse files Browse the repository at this point in the history
Signed-off-by: jace-roell <jace.roell@hotmail.com>
  • Loading branch information
jace-roell committed Jun 4, 2024
1 parent 68d024d commit ae1b1ce
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to the Zowe core SDK package will be documented in this file.

## Recent Changes

- Enhancement: Modified Services.ts to have commented properties end with commas. [#1049](https://github.com/zowe/zowe-cli/issues/1049)


## `8.0.0-next.202403041352`

- BugFix: Updated engine to Node 18.12.0. [#2074](https://github.com/zowe/zowe-cli/pull/2074)
Expand Down
198 changes: 196 additions & 2 deletions packages/core/__tests__/apiml/__unit__/Services.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,56 @@ describe("APIML Services unit tests", () => {
expect(actualJson).toEqual(expectedJson);
});

it("should properly apply comments and commas to profileInfoList", () => {
// should apply a comma to the end of all values in profile kvp except the last value
// should apply a comma to all commented profile types except base
const expectedJsonSnippet_base = `
//"basePath": "test1/v2"
//"basePath": "test1/v3"
"basePath": "test1/v1"
`;
const expectedJsonSnippet_notBase= `
//"type2": "test2.2",
"type2": "test2.1"
`;
const testCase: IApimlProfileInfo[] = [
{
profName: "test1",
profType: "type1",
basePaths: [
"test1/v1",
"test1/v2",
"test1/v3"
],
pluginConfigs: new Set(),
gatewayUrlConflicts: {}
},
{
profName: "test2.1",
profType: "type2",
basePaths: [
"test2.1/v1"
],
pluginConfigs: new Set(),
gatewayUrlConflicts: {}
},
{
profName: "test2.2",
profType: "type2",
basePaths: [
"test2.2/v1"
],
pluginConfigs: new Set(),
gatewayUrlConflicts: {}
}
];
const actualJson = JSONC.stringify(Services.convertApimlProfileInfoToProfileConfig(testCase), null, ConfigConstants.INDENT);

expect(actualJson.replace(/\s+/g, '')).toContain(expectedJsonSnippet_base.replace(/\s+/g, ''));
expect(actualJson.replace(/\s+/g, '')).toContain(expectedJsonSnippet_notBase.replace(/\s+/g, ''));

});

it("should create a config object without comments about conflicts", () => {
const testCase: IApimlProfileInfo[] = [{
profName: "test0",
Expand Down Expand Up @@ -551,7 +601,7 @@ describe("APIML Services unit tests", () => {
"defaults": {
// Multiple services were detected.
// Uncomment one of the lines below to set a different default.
//"type2": "test2.2"
//"type2": "test2.2",
"type2": "test2.1"
},
"autoStore": true
Expand Down Expand Up @@ -648,10 +698,154 @@ describe("APIML Services unit tests", () => {
"type3": "test3",
// Multiple services were detected.
// Uncomment one of the lines below to set a different default.
//"type4": "test4.2"
//"type4": "test4.2",
"type4": "test4.1"
},
"autoStore": true
}`;
expect(actualJson).toEqual(expectedJson);
});
it("should produce proper default file when multiple comments for a type are passed", () => {
const testCase: IApimlProfileInfo[] = [
{
profName: "test3",
profType: "type3",
basePaths: [
"test3/v1",
"test3/v2",
"test3/v3"
],
pluginConfigs: new Set([{
apiId: "test3-apiId",
connProfType: "type3",
pluginName: "type3-plugin-name"
}]),
gatewayUrlConflicts: {}
},
{
profName: "test4.1",
profType: "type4",
basePaths: [
"test4/v1",
"test4/v2"
],
pluginConfigs: new Set([
{
apiId: "test4.1-apiId",
connProfType: "type4",
pluginName: "type4.1-plugin-name"
},
{
apiId: "test4.1-apiId",
connProfType: "type4",
pluginName: "type4.1-plugin-name-copy"
}
]),
gatewayUrlConflicts: {
"type4.1-plugin-name": ["test4/v1"],
"type4.1-plugin-name-copy": ["test4/v2", "test4/v3"]
}
},
{
profName: "test4.2",
profType: "type4",
basePaths: [
"test4/v1"
],
pluginConfigs: new Set(),
gatewayUrlConflicts: {}
},
{
profName: "test4.3",
profType: "type4",
basePaths: [
"test4/v1"
],
pluginConfigs: new Set(),
gatewayUrlConflicts: {}
},
{
profName: "base1",
profType: "base",
basePaths: [
"base1/v1"
],
pluginConfigs: new Set(),
gatewayUrlConflicts: {}
},
{
profName: "base2",
profType: "base",
basePaths: [
"base2/v1"
],
pluginConfigs: new Set(),
gatewayUrlConflicts: {}
}
];
const actualJson = JSONC.stringify(Services.convertApimlProfileInfoToProfileConfig(testCase), null, ConfigConstants.INDENT);
const expectedJson = `{
"profiles": {
"test3": {
"type": "type3",
"properties": {
// Multiple base paths were detected for this service.
// Uncomment one of the lines below to use a different one.
//"basePath": "test3/v2"
//"basePath": "test3/v3"
"basePath": "test3/v1"
}
},
"test4.1": {
"type": "type4",
"properties": {
// ---
// Warning: basePath conflict detected!
// Different plugins require different versions of the same API.
// List:
// "type4.1-plugin-name": "test4/v1"
// "type4.1-plugin-name-copy": "test4/v2", "test4/v3"
// ---
//"basePath": "test4/v2"
"basePath": "test4/v1"
}
},
"test4.2": {
"type": "type4",
"properties": {
"basePath": "test4/v1"
}
},
"test4.3": {
"type": "type4",
"properties": {
"basePath": "test4/v1"
}
},
"base1": {
"type": "base",
"properties": {
"basePath": "base1/v1"
}
},
"base2": {
"type": "base",
"properties": {
"basePath": "base2/v1"
}
}
},
"defaults": {
"type3": "test3",
// Multiple services were detected.
// Uncomment one of the lines below to set a different default.
//"type4": "test4.2",
//"type4": "test4.3",
"type4": "test4.1",
//"base": "base2"
"base": "base1"
},
"autoStore": true
}`;
expect(actualJson).toEqual(expectedJson);
});
Expand Down
32 changes: 22 additions & 10 deletions packages/core/src/apiml/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ export class Services {

const _genCommentsHelper = (key: string, elements: string[]): string => {
if (elements == null || elements.length === 0) return "";
return `//"${key}": "${elements.length === 1 ? elements[0] : elements.join('"\n//"' + key + '": "')}"`;

return elements.reduce((all, current: string, index) => {
return all.concat(key.includes("base") ? `\n//"${key}": "${current}"` : `\n//"${key}": "${current}",`);
}, "");
};

profileInfoList?.forEach((profileInfo: IApimlProfileInfo) => {
Expand Down Expand Up @@ -252,30 +255,39 @@ export class Services {
const noConflictMessage = `
// Multiple base paths were detected for this service.
// Uncomment one of the lines below to use a different one.`;
// Typecasting because of this issue: https://github.com/kaelzhang/node-comment-json/issues/42
configProfile.profiles[profileInfo.profName].properties = JSONC.parse(`
{
${basePathConflicts.length > 0 ? basepathConflictMessage : noConflictMessage}
${_genCommentsHelper("basePath", basePaths)}
"basePath": "${defaultBasePath}"
}`
) as any;
);
}
});

// Establish keys for object map for index check within loop
const defaultKeys = Object.keys(conflictingDefaults);

for (const defaultKey in conflictingDefaults) {
if (configDefaults[defaultKey] != null) {
const trueDefault = configDefaults[defaultKey];
delete configDefaults[defaultKey];
let jsonString = `
${JSONC.stringify(configDefaults, null, ConfigConstants.INDENT).slice(0, -1)}${Object.keys(configDefaults).length > 0 ? "," : ""}`;
const defaultKeyIndex = defaultKeys.indexOf(defaultKey);

// Typecasting because of this issue: https://github.com/kaelzhang/node-comment-json/issues/42
configDefaults = JSONC.parse(`
${JSONC.stringify(configDefaults, null, ConfigConstants.INDENT).slice(0, -1)}${Object.keys(configDefaults).length > 0 ? "," : ""}
// Multiple services were detected.
// Uncomment one of the lines below to set a different default.
// Logic to ensure that comment block is not duplicated
if (defaultKeyIndex === 0) {
jsonString += `
// Multiple services were detected.
// Uncomment one of the lines below to set a different default.`;
}
jsonString += `
${_genCommentsHelper(defaultKey, conflictingDefaults[defaultKey])}
"${defaultKey}": "${trueDefault}"
}`) as any;
"${defaultKey}": "${trueDefault}"`;
// Terminate the JSON string
jsonString += '\n}';
configDefaults = JSONC.parse(jsonString);
}
}

Expand Down

0 comments on commit ae1b1ce

Please sign in to comment.