Skip to content

fix: duplicate override path when apply template#1209

Merged
looplj merged 1 commit intorelease/v0.9.xfrom
dev-tmp
Mar 28, 2026
Merged

fix: duplicate override path when apply template#1209
looplj merged 1 commit intorelease/v0.9.xfrom
dev-tmp

Conversation

@looplj
Copy link
Copy Markdown
Owner

@looplj looplj commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the mergeOverrideHeaders and mergeOverrideOperations functions to improve performance by replacing nested loops with a Map-based lookup strategy. The review feedback correctly identifies that since the code only checks for the existence of paths and does not utilize the stored indices, using a Set would be a more idiomatic and efficient data structure for these operations.

Comment on lines +25 to +43
const templateSetOpsByPath = new Map<string, number[]>();
template.forEach((op, index) => {
if (op.op === 'set' && op.path) {
const normalizedPath = op.path.toLowerCase();
const indices = templateSetOpsByPath.get(normalizedPath) || [];
indices.push(index);
templateSetOpsByPath.set(normalizedPath, indices);
}
});

for (const templateOp of template) {
if (templateOp.op === 'set' && templateOp.path) {
const index = result.findIndex(
(op) => op.op === 'set' && op.path?.toLowerCase() === templateOp.path?.toLowerCase()
);
if (index >= 0) {
result[index] = templateOp;
} else {
result.push(templateOp);
for (const existingOp of existing) {
if (existingOp.op === 'set' && existingOp.path) {
const normalizedPath = existingOp.path.toLowerCase();
if (templateSetOpsByPath.has(normalizedPath)) {
continue;
}
} else {
result.push(templateOp);
}
result.push(existingOp);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To improve clarity and use a more appropriate data structure, you can use a Set to store the template paths instead of a Map. Since you're only checking for the existence of a path, a Set is more idiomatic and slightly more efficient.

  const templateSetPaths = new Set<string>();
  template.forEach((op) => {
    if (op.op === 'set' && op.path) {
      templateSetPaths.add(op.path.toLowerCase());
    }
  });

  for (const existingOp of existing) {
    if (existingOp.op === 'set' && existingOp.path) {
      const normalizedPath = existingOp.path.toLowerCase();
      if (templateSetPaths.has(normalizedPath)) {
        continue;
      }
    }
    result.push(existingOp);
  }

Comment on lines +61 to +77
const templateSetOpsByPath = new Map<string, number[]>();
template.forEach((op, index) => {
if ((op.op === 'set' || op.op === 'delete') && op.path) {
const indices = templateSetOpsByPath.get(op.path) || [];
indices.push(index);
templateSetOpsByPath.set(op.path, indices);
}
});

// For set and delete ops, match by path
if ((templateOp.op === 'set' || templateOp.op === 'delete') && templateOp.path) {
const index = result.findIndex(
(op) => (op.op === 'set' || op.op === 'delete') && op.path === templateOp.path
);
if (index >= 0) {
result[index] = templateOp;
} else {
result.push(templateOp);
for (const existingOp of existing) {
if ((existingOp.op === 'set' || existingOp.op === 'delete') && existingOp.path) {
if (templateSetOpsByPath.has(existingOp.path)) {
continue;
}
} else {
result.push(templateOp);
}
result.push(existingOp);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Similar to the other function, using a Set here to store the paths from the template would be more idiomatic and clearer, as you are only checking for path existence.

  const templatePaths = new Set<string>();
  template.forEach((op) => {
    if ((op.op === 'set' || op.op === 'delete') && op.path) {
      templatePaths.add(op.path);
    }
  });

  for (const existingOp of existing) {
    if ((existingOp.op === 'set' || existingOp.op === 'delete') && existingOp.path) {
      if (templatePaths.has(existingOp.path)) {
        continue;
      }
    }
    result.push(existingOp);
  }

@looplj looplj merged commit d0f9987 into release/v0.9.x Mar 28, 2026
3 checks passed
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.

1 participant