Skip to content

Commit

Permalink
fix formatting unique expression attribute names (#461)
Browse files Browse the repository at this point in the history
* fix formatting expression attribute names

* revert single quotes

* simplify logic

* use const

* clean up
  • Loading branch information
anatolzak authored Jan 28, 2025
1 parent a1b6627 commit 6cb361f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ExpressionState {
this.expression = "";
this.prefix = prefix || "";
this.refs = {};
this.formattedNameToOriginalNameMap = new Map();
}

incrementName(name) {
Expand All @@ -30,14 +31,28 @@ class ExpressionState {

formatName(name = "") {
const nameWasNotANumber = isNaN(name);
name = `${name}`.replaceAll(/[^\w]/g, "");
if (name.length === 0) {
name = "p";
} else if (nameWasNotANumber !== isNaN(name)) {
const originalName = `${name}`;
let formattedName = originalName.replaceAll(/[^\w]/g, "");

if (formattedName.length === 0) {
formattedName = "p";
} else if (nameWasNotANumber !== isNaN(formattedName)) {
// name became number due to replace
name = `p${name}`;
formattedName = `p${formattedName}`;
}
return name;

const originalFormattedName = formattedName;
let nameSuffix = 1;

while (
this.formattedNameToOriginalNameMap.has(formattedName) &&
this.formattedNameToOriginalNameMap.get(formattedName) !== originalName
) {
formattedName = `${originalFormattedName}_${++nameSuffix}`;
}

this.formattedNameToOriginalNameMap.set(formattedName, originalName);
return formattedName;
}

// todo: make the structure: name, value, paths
Expand Down
8 changes: 7 additions & 1 deletion test/connected.update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,8 @@ describe("Update Item", () => {
.delete({ tags: updates.tags })
.data((attr, op) => {
op.set(attr.custom.prop1, updates.prop1);
op.set(attr.custom["prop1 "], updates.prop1);
op.set(attr.custom["prop1 "], updates.prop1);
op.add(attr.views, created.custom.prop3);
op.set(attr.license, op.name(attr.files[0]));
op.add(attr.recentCommits[0].views, updates.recentCommitsViews);
Expand All @@ -1393,7 +1395,7 @@ describe("Update Item", () => {

expect(params).to.deep.equal({
UpdateExpression:
"SET #stars = (if_not_exists(#stars, :stars_default_value_u0) - :stars_u0), #files = list_append(if_not_exists(#files, :files_default_value_u0), :files_u0), #description = :description_u0, #custom.#prop1 = :custom_u0, #license = #files[0], #repoOwner = :repoOwner_u0, #repoName = :repoName_u0, #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0 REMOVE #about, #recentCommits[1].#message ADD #followers :followers_u0, #views :views_u0, #recentCommits[0].#views :views_u1 DELETE #tags :tags_u0",
"SET #stars = (if_not_exists(#stars, :stars_default_value_u0) - :stars_u0), #files = list_append(if_not_exists(#files, :files_default_value_u0), :files_u0), #description = :description_u0, #custom.#prop1 = :custom_u0, #custom.#prop1_2 = :custom_u1, #custom.#prop1_3 = :custom_u2, #license = #files[0], #repoOwner = :repoOwner_u0, #repoName = :repoName_u0, #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0 REMOVE #about, #recentCommits[1].#message ADD #followers :followers_u0, #views :views_u0, #recentCommits[0].#views :views_u1 DELETE #tags :tags_u0",
ExpressionAttributeNames: {
"#followers": "followers",
"#stars": "stars",
Expand All @@ -1403,6 +1405,8 @@ describe("Update Item", () => {
"#tags": "tags",
"#custom": "custom",
"#prop1": "prop1",
"#prop1_2": "prop1 ",
"#prop1_3": "prop1 ",
"#views": "views",
"#recentCommits": "recentCommits",
"#message": "message",
Expand All @@ -1421,6 +1425,8 @@ describe("Update Item", () => {
":description_u0": "updated description",
":tags_u0": params.ExpressionAttributeValues[":tags_u0"],
":custom_u0": "def",
":custom_u1": "def",
":custom_u2": "def",
":views_u0": 200,
":views_u1": 3,
":repoName_u0": repoName,
Expand Down

0 comments on commit 6cb361f

Please sign in to comment.