Skip to content

Commit

Permalink
[W-14651333] difference in order of example payload in exchange and a…
Browse files Browse the repository at this point in the history
…cm (#62)

* chore: add computeRaw method to ExampleGenerator class

* chore: fix formatting issue in ExampleGenerator.js

* chore: add comment
  • Loading branch information
alexpmule authored Jan 9, 2024
1 parent 598f6a2 commit 54db28c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ExampleGenerator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,11 @@ declare class ExampleGenerator extends AmfHelperMixin(Object) {
* @return {XmlData}
*/
_computeXmlSerializationData(serialization): object;

/**
* Generate JSON example string value from raw value definition.
* @param {string} raw
* @return {string}
*/
computeRaw(raw: string): string
}
35 changes: 35 additions & 0 deletions src/ExampleGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,17 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
}
});
if (isJson) {
// if raw (original example) exists try to parse it to JSON
// if the parse process fails then use parts to build example value
if (result.raw) {
try {
result.value = this.computeRaw(raw)
return result
} catch (_) {
// ...
}
}

result.value = JSON.stringify(parts, null, 2);
return result;
}
Expand Down Expand Up @@ -792,6 +803,30 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
return undefined;
}

/**
* @param {String} raw
* @returns string JSON formatted
*/
computeRaw (raw) {
const accountEntries = raw.split('-\n');
const accounts = [];
for (const entry of accountEntries) {
if (entry !== '') {
const lines = entry.split('\n');
const account = {};
for (const line of lines) {
if (line !== '') {
const [key, value] = line.split(': ');
account[key.trim()] = Number(value) ? Number(value) : value.trim()
}
}
accounts.push(account);
}
}
return JSON.stringify(accounts, null, 2);
}


/**
* Computes list of examples for an array shape.
* @param {Object} schema The AMF's array shape
Expand Down
23 changes: 23 additions & 0 deletions test/ExampleGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3163,4 +3163,27 @@ describe('ExampleGenerator', () => {
});
});
});

describe('computeRaw', () => {
[
['json+ld data model', false],
['Compact data model', true],
].forEach((args) => {
const compact = /** @type boolean */ (args[1]);
/** @type ExampleGenerator */
let element;
let amf;

beforeEach(async () => {
amf = await AmfLoader.load(/** @type Boolean */(compact));
element = new ExampleGenerator(amf);
});

it('should correctly transform raw string to JSON', () => {
const raw = "-\n balance: 200\n approval-status: P\n account-id: de7228b9-f6bb-4261-9d17-a3ddd5802e03\n account-name: Plaid Saving\n account-number: '1111222233331111'\n account-routing-number: '123123123'\n institution-name: Sample Bank\n institution-id: b3dedb19-157c-4239-880f-a125ef4384e2\n created-by: WREX\n modified-by: WREX\n created-at: February 19, 2023, 3:16:01 AM\n modified-at: February 19, 2023, 3:16:01 AM\n-\n balance: 100\n approval-status: P\n account-id: e7bb8f6d-fdc0-4873-9609-d2f2713900ed\n account-name: Plaid Checking\n account-number: '1111222233330000 '\n account-routing-number: '123123123'\n institution-name: Sample Bank\n institution-id: b3dedb19-157c-4239-880f-a125ef4384e2\n created-by: WREX\n modified-by: WREX\n created-at: February 19, 2023, 3:16:01 AM\n modified-at: February 19, 2023, 3:16:01 AM";
const expectedJson = '[\n {\n "balance": 200,\n "approval-status": "P",\n "account-id": "de7228b9-f6bb-4261-9d17-a3ddd5802e03",\n "account-name": "Plaid Saving",\n "account-number": "\'1111222233331111\'",\n "account-routing-number": "\'123123123\'",\n "institution-name": "Sample Bank",\n "institution-id": "b3dedb19-157c-4239-880f-a125ef4384e2",\n "created-by": "WREX",\n "modified-by": "WREX",\n "created-at": "February 19, 2023, 3:16:01 AM",\n "modified-at": "February 19, 2023, 3:16:01 AM"\n },\n {\n "balance": 100,\n "approval-status": "P",\n "account-id": "e7bb8f6d-fdc0-4873-9609-d2f2713900ed",\n "account-name": "Plaid Checking",\n "account-number": "\'1111222233330000 \'",\n "account-routing-number": "\'123123123\'",\n "institution-name": "Sample Bank",\n "institution-id": "b3dedb19-157c-4239-880f-a125ef4384e2",\n "created-by": "WREX",\n "modified-by": "WREX",\n "created-at": "February 19, 2023, 3:16:01 AM",\n "modified-at": "February 19, 2023, 3:16:01 AM"\n }\n]';
assert.equal(element.computeRaw(raw), expectedJson);
});
});
});
});

0 comments on commit 54db28c

Please sign in to comment.