Skip to content

Commit

Permalink
fix: update script to handle component object ex
Browse files Browse the repository at this point in the history
Changes:

- modified the script so that it can handle component object example
- the script as of now add the fields and sub-fields in the updated-doc
  so that the other references remain unaffected
- the earlier version was removinga and adding the whole part of the
  document altogether which affected the references and dependencies in
  the entire document
  • Loading branch information
AnimeshKumar923 committed Jun 10, 2024
1 parent f64f32c commit 532c4c2
Showing 1 changed file with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function extractExamples(content) {
const examples = [];

while ((match = exampleRegex.exec(content)) !== null) {
examples.push(JSON.parse(match[1]));
try {
examples.push(JSON.parse(match[1]));
} catch (e) {
console.error("Failed to parse example JSON:", match[1], e);
}
}

return examples;
Expand All @@ -43,14 +47,30 @@ const examples = extractExamples(markdownContent);
// Read the base AsyncAPI document for v3
const baseDoc = JSON.parse(fs.readFileSync('ex-base-doc.json', 'utf8'));

// Function to deeply merge two objects without overwriting existing nested structures
function deepMerge(target, source) {
for (const key of Object.keys(source)) {
if (source[key] instanceof Object && key in target) {
target[key] = deepMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
return target;
}

// Function to set a value in a JSON object using JSONPath, creating missing fields if necessary
function setValueByPath(obj, path, value) {
const pathParts = path.replace(/\$/g, '').split('.').slice(1); // Remove the root "$" and split path
let current = obj;

pathParts.forEach((part, index) => {
if (index === pathParts.length - 1) {
current[part] = value; // Set value at the end of the path
if (current[part] === undefined) {
current[part] = value; // Set the new value if the path does not exist
} else {
current[part] = deepMerge(current[part], value); // Deep merge if the path exists
}
} else {
if (!current[part]) {
current[part] = {}; // Create object if it doesn't exist
Expand All @@ -69,19 +89,33 @@ const updates = comments.map((comment, index) => ({

// Apply updates
updates.forEach(update => {
const results = JSONPath({ path: update.json_path, json: baseDoc, resultType: 'all' });

if (results.length === 0) {
setValueByPath(baseDoc, update.json_path, update.data); // Create the path if it doesn't exist
} else {
results.forEach(result => {
const parent = result.parent;
const parentProperty = result.parentProperty;
parent[parentProperty] = {
...parent[parentProperty],
...update.data // Merge the existing data with the new data
};
});
try {
const results = JSONPath({ path: update.json_path, json: baseDoc, resultType: 'all' });

console.log(`Processing update for test: ${update.test} at path: ${update.json_path}`);

const pathParts = update.json_path.split('.');
const targetKey = pathParts[pathParts.length - 1];

// Check if the top-level key of the example JSON matches the target key
let dataToMerge = update.data;
if (dataToMerge.hasOwnProperty(targetKey)) {
dataToMerge = dataToMerge[targetKey];
}

if (results.length === 0) {
console.log(`Path not found, creating path: ${update.json_path}`);
setValueByPath(baseDoc, update.json_path, dataToMerge); // Create the path if it doesn't exist
} else {
results.forEach(result => {
const parent = result.parent;
const parentProperty = result.parentProperty;
console.log(`Merging data at path: ${update.json_path}`);
parent[parentProperty] = deepMerge(parent[parentProperty], dataToMerge); // Deep merge the existing data with the new data
});
}
} catch (e) {
console.error(`Error processing update for test: ${update.test} at path: ${update.json_path}`, e);
}
});

Expand Down

0 comments on commit 532c4c2

Please sign in to comment.