From 258c16768423389eee947d1f58be7f5458dcf8ba Mon Sep 17 00:00:00 2001 From: Chris Price Date: Fri, 29 Dec 2023 12:37:13 -0800 Subject: [PATCH] feat: support array inputs for SortedSetPutElements (#1096) * feat: support array inputs for SortedSetPutElements Prior to this commit the SortedSetPutElements API would accept a Map or a Record containing the elements that the caller wished to add to the sorted set. 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 | 14 ++++++++-- .../src/internal/cache-data-client.ts | 14 ++++++++-- .../src/sorted-set.ts | 28 +++++++++++++++++++ packages/core/src/clients/ICacheClient.ts | 5 +++- .../clients/cache/AbstractCacheClient.ts | 5 +++- .../src/internal/clients/cache/IDataClient.ts | 5 +++- 6 files changed, 62 insertions(+), 9 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 f162ccd07..3ec1d8c52 100644 --- a/packages/client-sdk-nodejs/src/internal/cache-data-client.ts +++ b/packages/client-sdk-nodejs/src/internal/cache-data-client.ts @@ -2024,7 +2024,10 @@ export class CacheDataClient implements IDataClient { public async sortedSetPutElements( cacheName: string, sortedSetName: string, - elements: Map | Record, + elements: + | Map + | Record + | Array<[string, number]>, ttl: CollectionTtl = CollectionTtl.fromCacheTtl() ): Promise { try { @@ -2912,9 +2915,14 @@ export class CacheDataClient implements IDataClient { } private convertSortedSetMapOrRecord( - elements: Map | Record + elements: + | Map + | Record + | Array<[string, number]> ): grpcCache._SortedSetElement[] { - if (elements instanceof Map) { + if (elements instanceof Array) { + return this.convertSortedSetMapOrRecord(new Map(elements)); + } else if (elements instanceof Map) { return [...elements.entries()].map( element => new grpcCache._SortedSetElement({ 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 bc389018b..d8af40691 100644 --- a/packages/client-sdk-web/src/internal/cache-data-client.ts +++ b/packages/client-sdk-web/src/internal/cache-data-client.ts @@ -2212,7 +2212,10 @@ export class CacheDataClient< public async sortedSetPutElements( cacheName: string, sortedSetName: string, - elements: Map | Record, + elements: + | Map + | Record + | Array<[string, number]>, ttl: CollectionTtl = CollectionTtl.fromCacheTtl() ): Promise { try { @@ -3190,9 +3193,14 @@ export class CacheDataClient< } private convertSortedSetMapOrRecord( - elements: Map | Record + elements: + | Map + | Record + | Array<[string, number]> ): _SortedSetElementGrpc[] { - if (elements instanceof Map) { + if (elements instanceof Array) { + return this.convertSortedSetMapOrRecord(new Map(elements)); + } else if (elements instanceof Map) { return [...elements.entries()].map(element => new _SortedSetElementGrpc() .setValue(convertToB64String(element[0])) diff --git a/packages/common-integration-tests/src/sorted-set.ts b/packages/common-integration-tests/src/sorted-set.ts index 5925d0458..81a21d749 100644 --- a/packages/common-integration-tests/src/sorted-set.ts +++ b/packages/common-integration-tests/src/sorted-set.ts @@ -2416,6 +2416,34 @@ export function runSortedSetTests( ]); }); + it('should store elements with a string values passed via Array', async () => { + const sortedSetName = v4(); + let response = await cacheClient.sortedSetPutElements( + integrationTestCacheName, + sortedSetName, + [ + ['foo', 42], + ['bar', 84], + ] + ); + expectWithMessage(() => { + expect(response).toBeInstanceOf(CacheSortedSetPutElements.Success); + }, `expected SUCCESS but got ${response.toString()}`); + + response = await cacheClient.sortedSetFetchByRank( + integrationTestCacheName, + sortedSetName + ); + expectWithMessage(() => { + expect(response).toBeInstanceOf(CacheSortedSetFetch.Hit); + }, `expected HIT but got ${response.toString()}`); + const hitResponse = response as CacheSortedSetFetch.Hit; + expect(hitResponse.valueArray()).toEqual([ + {value: 'foo', score: 42}, + {value: 'bar', score: 84}, + ]); + }); + it('should store elements with a bytes values passed via Map', async () => { const sortedSetName = v4(); let response = await cacheClient.sortedSetPutElements( diff --git a/packages/core/src/clients/ICacheClient.ts b/packages/core/src/clients/ICacheClient.ts index d099c0079..c3a7a3323 100644 --- a/packages/core/src/clients/ICacheClient.ts +++ b/packages/core/src/clients/ICacheClient.ts @@ -250,7 +250,10 @@ export interface ICacheClient extends IControlClient, IPingClient { sortedSetPutElements( cacheName: string, sortedSetName: string, - elements: Map | Record, + elements: + | Map + | Record + | Array<[string, number]>, options?: SortedSetPutElementsOptions ): Promise; sortedSetGetRank( diff --git a/packages/core/src/internal/clients/cache/AbstractCacheClient.ts b/packages/core/src/internal/clients/cache/AbstractCacheClient.ts index fee51067d..1f69e5a4e 100644 --- a/packages/core/src/internal/clients/cache/AbstractCacheClient.ts +++ b/packages/core/src/internal/clients/cache/AbstractCacheClient.ts @@ -959,7 +959,10 @@ export abstract class AbstractCacheClient implements ICacheClient { public async sortedSetPutElements( cacheName: string, sortedSetName: string, - elements: Map | Record, + elements: + | Map + | Record + | Array<[string, number]>, options?: SortedSetPutElementsOptions ): 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 58710fd28..d964c4754 100644 --- a/packages/core/src/internal/clients/cache/IDataClient.ts +++ b/packages/core/src/internal/clients/cache/IDataClient.ts @@ -217,7 +217,10 @@ export interface IDataClient { sortedSetPutElements( cacheName: string, sortedSetName: string, - elements: Map | Record, + elements: + | Map + | Record + | Array<[string, number]>, ttl?: CollectionTtl ): Promise; sortedSetGetRank(