Skip to content

Commit

Permalink
fix: support non-alphanumeric field name (#1165)
Browse files Browse the repository at this point in the history
Currently REST mode transcode doesn't support non-alphanumeric in the field name, and it cause [firestore test](https://github.com/googleapis/nodejs-firestore/blob/main/dev/system-test/firestore.ts#L754-L771) fails.

code sample:
```
test() {
    const ref = randomCol.doc('doc');
    return ref
      .set({'!.\\`': {'!.\\`': 'value'}})
      .then(() => {
        return ref.get();
      })
      .then(doc => {
        // SEE ERROR ON THIS LINE
        expect(doc.data()).to.deep.equal({'!.\\`': {'!.\\`': 'value'}});
        return ref.update(new 
FieldPath('!.\\`', '!.\\`'), 'new-value');
      })
      .then(() => {
        return ref.get();
      })
      .then(doc => {
        expect(doc.data()).to.deep.equal({'!.\\`': {'!.\\`': 'new-value'}});
      });
}
```

Error
```
AssertionError: expected { '.': { '.': 'value' } } to deeply equal { '!.\\`': { '!.\\`': 'value' } }
```
  • Loading branch information
summer-ji-eng authored Jan 13, 2022
1 parent 7c3d7f1 commit 4f53efa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ function capitalize(str: string) {
* camelCase (used by protobuf.js)
*/
export function snakeToCamelCase(str: string) {
// split on spaces, non-alphanumeric, or capital letters
// split on spaces, underscore, or capital letters
const splitted = str
.split(/(?=[A-Z])|(?:(?!\.)[\s\W_])+/)
.filter(w => w.length > 0)
.split(/(?=[A-Z])|(?:(?!(_(\W+)))[\s_])+/)
.filter(w => w && w.length > 0)
// Keep the capitalization for the first split.
.map((word, index) => (index === 0 ? word : word.toLowerCase()));
if (splitted.length === 0) {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ describe('util.ts', () => {
assert.strictEqual(camelToSnakeCase('a.1'), 'a.1');
assert.strictEqual(camelToSnakeCase('abc.1Foo'), 'abc.1_foo');
assert.strictEqual(camelToSnakeCase('abc.foo'), 'abc.foo');
assert.strictEqual(camelToSnakeCase('a!.\\'), 'a!.\\');
assert.strictEqual(camelToSnakeCase('!._\\`'), '!._\\`');
assert.strictEqual(camelToSnakeCase('a!B`'), 'a!_b`');
assert.strictEqual(camelToSnakeCase('a.1B`'), 'a.1_b`');
});

it('snakeToCamelCase', () => {
Expand All @@ -40,5 +44,9 @@ describe('util.ts', () => {
assert.strictEqual(snakeToCamelCase('a.1'), 'a.1');
assert.strictEqual(snakeToCamelCase('abc.1_foo'), 'abc.1Foo');
assert.strictEqual(snakeToCamelCase('abc.foo'), 'abc.foo');
assert.strictEqual(snakeToCamelCase('a!.\\`'), 'a!.\\`');
assert.strictEqual(snakeToCamelCase('!._\\`'), '!._\\`');
assert.strictEqual(snakeToCamelCase('a!_b`'), 'a!B`');
assert.strictEqual(snakeToCamelCase('a.1_b`'), 'a.1B`');
});
});

0 comments on commit 4f53efa

Please sign in to comment.