Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions handwritten/bigtable/src/execute-query/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Duplex, Readable} from 'stream';

Check warning on line 15 in handwritten/bigtable/src/execute-query/values.ts

View workflow job for this annotation

GitHub Actions / lint

'Readable' is defined but never used
import * as SqlTypes from './types';
import {PreciseDate} from '@google-cloud/precise-date';
import {NamedList} from './namedlist';
Expand All @@ -38,10 +38,7 @@
| null;

export type SqlValue =
| ExecuteQueryParameterValue
| Struct
| Array<SqlValue>
| BigtableMap;
ExecuteQueryParameterValue | Struct | Array<SqlValue> | BigtableMap;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[Generated] Format SqlValue union onto a single line as required by gts / Prettier (node ./bin/linter.mjs --strict).


/**
* A custom class created to allow setting year, month and date to 0.
Expand Down Expand Up @@ -117,19 +114,25 @@
// internal representation of the map contains the original key and value tuple
type _MapValue = [MapKey, SqlValue];

export class EncodedKeyMap
implements Map<bigint | string | Uint8Array | null, SqlValue>
{
/**
* Class representing a Map Value returned by ExecuteQuery. Native JS Map
* does not support Buffer comparison - it compares object id and not
* buffer values. This class solves this by encoding Buffer keys as
* base64 strings.
* Please note that an empty string and an empty buffer have the same
* representation, however, we do not ever use mixed key types (all keys are
* always all buffers or all strings) so we don't need to handle this.
*
* Implements `ReadonlyMap` rather than `Map` so emitted declarations remain
* compatible with TypeScript 7.0+ (`--target esnext`) without adding ESNext
* `Map` upsert methods (`getOrInsert` / `getOrInsertComputed`).
*/
export class EncodedKeyMap implements ReadonlyMap<
bigint | string | Uint8Array | null,
SqlValue
> {
private map_impl: Map<bigint | string | null, _MapValue>;
/**
* Class representing a Map Value returned by ExecuteQuery. Native JS Map
* does not support Buffer comparison - it compares object id and not
* buffer values. This class solves this by encoding Buffer keys as
* base64 strings.
* Please note that an empty string and an empty buffer have the same
* representation, however, we do not ever use mixed key types (all keys are
* always all buffers or all strings) so we don't need to handle this.
*/

Comment on lines +117 to +135

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[Generated] Move the class-level JSDoc comment above export class EncodedKeyMap and document why implements ReadonlyMap is used for TypeScript 7.0 (--target esnext) compatibility.

constructor(
entries?: ReadonlyArray<
[bigint | string | Uint8Array | null, SqlValue]
Expand Down Expand Up @@ -162,9 +165,9 @@
key: string | bigint | Uint8Array | null,
map: Map<string | bigint | Uint8Array | null, SqlValue>,
) => void,
thisArg?: any,

Check warning on line 168 in handwritten/bigtable/src/execute-query/values.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
): void {
this.map_impl.forEach((value, key) => {

Check warning on line 170 in handwritten/bigtable/src/execute-query/values.ts

View workflow job for this annotation

GitHub Actions / lint

'key' is defined but never used
callbackfn.call(thisArg, value[1], value[0], this.map_impl);
});
}
Expand Down
18 changes: 18 additions & 0 deletions handwritten/bigtable/test/base64keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
// limitations under the License.

import * as assert from 'assert';
import {describe, it, before, beforeEach, afterEach} from 'mocha';

Check warning on line 16 in handwritten/bigtable/test/base64keymap.ts

View workflow job for this annotation

GitHub Actions / lint

'afterEach' is defined but never used

Check warning on line 16 in handwritten/bigtable/test/base64keymap.ts

View workflow job for this annotation

GitHub Actions / lint

'beforeEach' is defined but never used

Check warning on line 16 in handwritten/bigtable/test/base64keymap.ts

View workflow job for this annotation

GitHub Actions / lint

'before' is defined but never used
import Long = require('long');

Check warning on line 17 in handwritten/bigtable/test/base64keymap.ts

View workflow job for this annotation

GitHub Actions / lint

'Long' is defined but never used
import * as proxyquire from 'proxyquire';

Check warning on line 18 in handwritten/bigtable/test/base64keymap.ts

View workflow job for this annotation

GitHub Actions / lint

'proxyquire' is defined but never used
import * as sn from 'sinon';

Check warning on line 19 in handwritten/bigtable/test/base64keymap.ts

View workflow job for this annotation

GitHub Actions / lint

'sn' is defined but never used

import {RowStateEnum} from '../src/chunktransformer.js';

Check warning on line 21 in handwritten/bigtable/test/base64keymap.ts

View workflow job for this annotation

GitHub Actions / lint

'RowStateEnum' is defined but never used
import {Mutation} from '../src/mutation.js';
import {Row} from '../src/row.js';
import {EncodedKeyMap, SqlValue} from '../src/execute-query/values.js';
Expand Down Expand Up @@ -180,5 +180,23 @@
assert.deepStrictEqual(resultForEach[1][0], 'stringKey1');
assert.deepStrictEqual(resultForEach[1][1], 'valueForString1');
});
it('implements ReadonlyMap without adding extra methods', () => {
const map = new EncodedKeyMap([
[Buffer.from('existingBuffer'), 'initialBufferValue'],
['existingNullValue', null],
]);
const readonlyMap: ReadonlyMap<
string | bigint | Uint8Array | null,
SqlValue
> = map;
assert.strictEqual(
readonlyMap.get(Buffer.from('existingBuffer')),
'initialBufferValue',
);
assert.strictEqual(readonlyMap.get('existingNullValue'), null);
assert.strictEqual(map.get(Buffer.from('missingBuffer')), undefined);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[Generated] Add missing-buffer key lookup assertion alongside the ReadonlyMap compatibility test.

assert.strictEqual('getOrInsert' in map, false);
assert.strictEqual('getOrInsertComputed' in map, false);
});
});
});
Loading