Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynoexpr doesn't allow to escape dynamic keys in objects #51

Closed
dawidk92 opened this issue Mar 29, 2023 · 2 comments
Closed

Dynoexpr doesn't allow to escape dynamic keys in objects #51

dawidk92 opened this issue Mar 29, 2023 · 2 comments

Comments

@dawidk92
Copy link

Current behavior

When I try to generate a command for an Update/UpdateRemove/Condition with a key in an object which is a simple string it works just fine:

import dynoexpr from '@tuplo/dynoexpr';

const dynamicKey = "key";

const params = dynoexpr({
  Update: { [`object.${dynamicKey}.value`]: `object.${dynamicKey}.value + 1` },
  Condition: { [`object.${dynamicKey}.value`]: "> 2" }
});

/*
{
  params: {
    ConditionExpression: "(#nbb017076.#nefd6a199.#n10d6f4c5 > :vaeeabc63)",
    ExpressionAttributeNames: {
      "#nbb017076": "object",
      "#nefd6a199": "key",
      "#n10d6f4c5": "value",
    },
    ExpressionAttributeValues: {
      ":vaeeabc63": 2,
      ":vc823bd86": 1,
    },
    UpdateExpression:
      "SET #nbb017076.#nefd6a199.#n10d6f4c5 = #nbb017076.#nefd6a199.#n10d6f4c5 + :vc823bd86",
  }
}
*/

The problem appears when the dynamicKey has . or a - character (maybe also a few others, but these ones I spotted in my project). When we change the key in such a way the output looks like this:

import dynoexpr from '@tuplo/dynoexpr';

const dynamicKey = "key.with-chars";

const params = dynoexpr({
  Update: { [`object.${dynamicKey}.value`]: `object.${dynamicKey}.value + 1` },
  Condition: { [`object.${dynamicKey}.value`]: "> 2" }
});

/*
{
  params: {
    ConditionExpression:
      "(#nbb017076.#nefd6a199.#n8b145925.#n10d6f4c5 > :vaeeabc63)",
    ExpressionAttributeNames: {
      "#nbb017076": "object",
      "#nefd6a199": "key",
      "#n8b145925": "with-chars",
      "#n10d6f4c5": "value",
    },
    ExpressionAttributeValues: {
      ":vaeeabc63": 2,
      ":vc823bd86": 1,
    },
    UpdateExpression:
      "SET #nbb017076.#nefd6a199.#n8b145925.#n10d6f4c5 = :veb17932c - :veb17932c - :vc823bd86",
  },
}
*/

As you can see, the object's nesting is broken and the update expression is generated incorrectly (the + sign has changed to the - sign).

Expected behavior

There should be a possibility to deal with such keys so the output would look like this:

/*
{
  params: {
    ConditionExpression: "(#nbb017076.#nefd6a199.#n10d6f4c5 > :vaeeabc63)",
    ExpressionAttributeNames: {
      "#nbb017076": "object",
      "#nefd6a199": "key.with-chars",
      "#n10d6f4c5": "value",
    },
    ExpressionAttributeValues: {
      ":vaeeabc63": 2,
      ":vc823bd86": 1,
    },
    UpdateExpression:
      "SET #nbb017076.#nefd6a199.#n10d6f4c5 = #nbb017076.#nefd6a199.#n10d6f4c5 + :vc823bd86",
  }
}
*/

Potential fix

Maybe there should be a possibility to wrap such keys in some special characters so dynoexpr could know that this string is a single key, e.g.:

`object."${dynamicKey}".value`
@ruicsh
Copy link
Member

ruicsh commented Mar 29, 2023

hey @dawidk92 can you try v3.5.0 please?

const params = dynoexpr({
  Update: {
    'foo."bar-cuz".baz': 'foo."bar-cuz".baz + 1'
  }
});

@dawidk92
Copy link
Author

@ruicsh it works as expected, thank you!

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

No branches or pull requests

2 participants