diff --git a/handwritten/bigtable/src/execute-query/values.ts b/handwritten/bigtable/src/execute-query/values.ts index 258a25d11268..f1434ec35ae0 100644 --- a/handwritten/bigtable/src/execute-query/values.ts +++ b/handwritten/bigtable/src/execute-query/values.ts @@ -38,10 +38,7 @@ export type ExecuteQueryParameterValue = | null; export type SqlValue = - | ExecuteQueryParameterValue - | Struct - | Array - | BigtableMap; + ExecuteQueryParameterValue | Struct | Array | BigtableMap; /** * A custom class created to allow setting year, month and date to 0. @@ -117,19 +114,25 @@ type MapKey = bigint | string | Uint8Array | null; // internal representation of the map contains the original key and value tuple type _MapValue = [MapKey, SqlValue]; -export class EncodedKeyMap - implements Map -{ +/** + * 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; - /** - * 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. - */ + constructor( entries?: ReadonlyArray< [bigint | string | Uint8Array | null, SqlValue] diff --git a/handwritten/bigtable/test/base64keymap.ts b/handwritten/bigtable/test/base64keymap.ts index 7446126bff45..af0a83758a4a 100644 --- a/handwritten/bigtable/test/base64keymap.ts +++ b/handwritten/bigtable/test/base64keymap.ts @@ -180,5 +180,23 @@ describe('Bigtable/EncodedKeyMap', () => { 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); + assert.strictEqual('getOrInsert' in map, false); + assert.strictEqual('getOrInsertComputed' in map, false); + }); }); });