From 83afb8fa147dc7451493d395326a494bd75c8678 Mon Sep 17 00:00:00 2001 From: Chris Price Date: Tue, 16 Jan 2024 10:36:02 -0800 Subject: [PATCH] feat: support array inputs for DictionarySetFields (#1101) * feat: support array inputs for DictionarySetFields Prior to this commit the DictionarySetFields API would accept a Map or a Record containing the elements that the caller wished to add to the dictionary. A user reported an error when writing JS (not TS) code where they had attempted to pass in an array of tuples (equivalent to what the map entries would have looked like) rather than an actual Map. This commit adds support for passing an array of tuples without wrapping it in a Map first. --- .../src/internal/cache-data-client.ts | 12 ++++-- .../src/internal/cache-data-client.ts | 12 ++++-- .../src/dictionary.ts | 42 +++++++++++++++++++ packages/core/src/clients/ICacheClient.ts | 3 +- .../clients/cache/AbstractCacheClient.ts | 3 +- .../src/internal/clients/cache/IDataClient.ts | 3 +- 6 files changed, 64 insertions(+), 11 deletions(-) diff --git a/packages/client-sdk-nodejs/src/internal/cache-data-client.ts b/packages/client-sdk-nodejs/src/internal/cache-data-client.ts index c25c734f4..d0cdf64e4 100644 --- a/packages/client-sdk-nodejs/src/internal/cache-data-client.ts +++ b/packages/client-sdk-nodejs/src/internal/cache-data-client.ts @@ -1564,7 +1564,8 @@ export class CacheDataClient implements IDataClient { dictionaryName: string, elements: | Map - | Record, + | Record + | Array<[string, string | Uint8Array]>, ttl: CollectionTtl = CollectionTtl.fromCacheTtl() ): Promise { try { @@ -1582,7 +1583,7 @@ export class CacheDataClient implements IDataClient { }` ); - const dictionaryFieldValuePairs = this.convertMapOrRecord(elements); + const dictionaryFieldValuePairs = this.convertElements(elements); const result = await this.sendDictionarySetFields( cacheName, @@ -3127,12 +3128,15 @@ export class CacheDataClient implements IDataClient { return v.map(i => this.convert(i)); } - private convertMapOrRecord( + private convertElements( elements: | Map | Record + | Array<[string, string | Uint8Array]> ): grpcCache._DictionaryFieldValuePair[] { - if (elements instanceof Map) { + if (elements instanceof Array) { + return this.convertElements(new Map(elements)); + } else if (elements instanceof Map) { return [...elements.entries()].map( element => new grpcCache._DictionaryFieldValuePair({ diff --git a/packages/client-sdk-web/src/internal/cache-data-client.ts b/packages/client-sdk-web/src/internal/cache-data-client.ts index 9f643ab5c..088a015e5 100644 --- a/packages/client-sdk-web/src/internal/cache-data-client.ts +++ b/packages/client-sdk-web/src/internal/cache-data-client.ts @@ -1496,7 +1496,8 @@ export class CacheDataClient< dictionaryName: string, elements: | Map - | Record, + | Record + | Array<[string, string | Uint8Array]>, ttl: CollectionTtl = CollectionTtl.fromCacheTtl() ): Promise { try { @@ -1514,7 +1515,7 @@ export class CacheDataClient< }` ); - const dictionaryFieldValuePairs = this.convertMapOrRecord(elements); + const dictionaryFieldValuePairs = this.convertElements(elements); const result = await this.sendDictionarySetFields( cacheName, @@ -3472,12 +3473,15 @@ export class CacheDataClient< return v.map(i => this.convertToUint8Array(i)); } - private convertMapOrRecord( + private convertElements( elements: | Map | Record + | Array<[string, string | Uint8Array]> ): _DictionaryFieldValuePairGrpc[] { - if (elements instanceof Map) { + if (elements instanceof Array) { + return this.convertElements(new Map(elements)); + } else if (elements instanceof Map) { return [...elements.entries()].map(element => new _DictionaryFieldValuePairGrpc() .setField(convertToB64String(element[0])) diff --git a/packages/common-integration-tests/src/dictionary.ts b/packages/common-integration-tests/src/dictionary.ts index bb10b06f0..82e0a1e2f 100644 --- a/packages/common-integration-tests/src/dictionary.ts +++ b/packages/common-integration-tests/src/dictionary.ts @@ -1730,6 +1730,48 @@ export function runDictionaryTests( } }); + it('should set fields with string items Array', async () => { + const dictionaryName = v4(); + const field1 = 'foo'; + const value1 = v4(); + const field2 = 'bar'; + const value2 = v4(); + const response = await cacheClient.dictionarySetFields( + integrationTestCacheName, + dictionaryName, + [ + ['foo', value1], + ['bar', value2], + ], + {ttl: CollectionTtl.of(10)} + ); + expectWithMessage(() => { + expect(response).toBeInstanceOf(CacheDictionarySetFields.Success); + }, `expected SUCCESS but got ${response.toString()}`); + let getResponse = await cacheClient.dictionaryGetField( + integrationTestCacheName, + dictionaryName, + field1 + ); + expectWithMessage(() => { + expect(getResponse).toBeInstanceOf(CacheDictionaryGetField.Hit); + }, `expected HIT but got ${getResponse.toString()}`); + if (getResponse instanceof CacheDictionaryGetField.Hit) { + expect(getResponse.valueString()).toEqual(value1); + } + getResponse = await cacheClient.dictionaryGetField( + integrationTestCacheName, + dictionaryName, + field2 + ); + expectWithMessage(() => { + expect(getResponse).toBeInstanceOf(CacheDictionaryGetField.Hit); + }, `expected HIT but got ${getResponse.toString()}`); + if (getResponse instanceof CacheDictionaryGetField.Hit) { + expect(getResponse.valueString()).toEqual(value2); + } + }); + it('should set fields with string field/Uint8Array value items', async () => { const dictionaryName = v4(); const field1 = v4(); diff --git a/packages/core/src/clients/ICacheClient.ts b/packages/core/src/clients/ICacheClient.ts index c3a7a3323..e0e117d4e 100644 --- a/packages/core/src/clients/ICacheClient.ts +++ b/packages/core/src/clients/ICacheClient.ts @@ -192,7 +192,8 @@ export interface ICacheClient extends IControlClient, IPingClient { dictionaryName: string, elements: | Map - | Record, + | Record + | Array<[string, string | Uint8Array]>, options?: DictionarySetFieldsOptions ): Promise; dictionaryGetField( diff --git a/packages/core/src/internal/clients/cache/AbstractCacheClient.ts b/packages/core/src/internal/clients/cache/AbstractCacheClient.ts index 1f69e5a4e..8f75d67c8 100644 --- a/packages/core/src/internal/clients/cache/AbstractCacheClient.ts +++ b/packages/core/src/internal/clients/cache/AbstractCacheClient.ts @@ -747,7 +747,8 @@ export abstract class AbstractCacheClient implements ICacheClient { dictionaryName: string, elements: | Map - | Record, + | Record + | Array<[string, string | Uint8Array]>, options?: DictionarySetFieldOptions ): Promise { const client = this.getNextDataClient(); diff --git a/packages/core/src/internal/clients/cache/IDataClient.ts b/packages/core/src/internal/clients/cache/IDataClient.ts index d964c4754..bebd098ce 100644 --- a/packages/core/src/internal/clients/cache/IDataClient.ts +++ b/packages/core/src/internal/clients/cache/IDataClient.ts @@ -153,7 +153,8 @@ export interface IDataClient { dictionaryName: string, elements: | Map - | Record, + | Record + | Array<[string, string | Uint8Array]>, ttl?: CollectionTtl ): Promise; dictionaryGetField(