Skip to content

Commit

Permalink
feat: support array inputs for SortedSetPutElements (#1096)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
cprice404 authored Dec 29, 2023
1 parent 3912599 commit 258c167
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
14 changes: 11 additions & 3 deletions packages/client-sdk-nodejs/src/internal/cache-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,10 @@ export class CacheDataClient implements IDataClient {
public async sortedSetPutElements(
cacheName: string,
sortedSetName: string,
elements: Map<string | Uint8Array, number> | Record<string, number>,
elements:
| Map<string | Uint8Array, number>
| Record<string, number>
| Array<[string, number]>,
ttl: CollectionTtl = CollectionTtl.fromCacheTtl()
): Promise<CacheSortedSetPutElements.Response> {
try {
Expand Down Expand Up @@ -2912,9 +2915,14 @@ export class CacheDataClient implements IDataClient {
}

private convertSortedSetMapOrRecord(
elements: Map<string | Uint8Array, number> | Record<string, number>
elements:
| Map<string | Uint8Array, number>
| Record<string, number>
| 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({
Expand Down
14 changes: 11 additions & 3 deletions packages/client-sdk-web/src/internal/cache-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,10 @@ export class CacheDataClient<
public async sortedSetPutElements(
cacheName: string,
sortedSetName: string,
elements: Map<string | Uint8Array, number> | Record<string, number>,
elements:
| Map<string | Uint8Array, number>
| Record<string, number>
| Array<[string, number]>,
ttl: CollectionTtl = CollectionTtl.fromCacheTtl()
): Promise<CacheSortedSetPutElements.Response> {
try {
Expand Down Expand Up @@ -3190,9 +3193,14 @@ export class CacheDataClient<
}

private convertSortedSetMapOrRecord(
elements: Map<string | Uint8Array, number> | Record<string, number>
elements:
| Map<string | Uint8Array, number>
| Record<string, number>
| 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]))
Expand Down
28 changes: 28 additions & 0 deletions packages/common-integration-tests/src/sorted-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/clients/ICacheClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ export interface ICacheClient extends IControlClient, IPingClient {
sortedSetPutElements(
cacheName: string,
sortedSetName: string,
elements: Map<string | Uint8Array, number> | Record<string, number>,
elements:
| Map<string | Uint8Array, number>
| Record<string, number>
| Array<[string, number]>,
options?: SortedSetPutElementsOptions
): Promise<CacheSortedSetPutElements.Response>;
sortedSetGetRank(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,10 @@ export abstract class AbstractCacheClient implements ICacheClient {
public async sortedSetPutElements(
cacheName: string,
sortedSetName: string,
elements: Map<string | Uint8Array, number> | Record<string, number>,
elements:
| Map<string | Uint8Array, number>
| Record<string, number>
| Array<[string, number]>,
options?: SortedSetPutElementsOptions
): Promise<CacheSortedSetPutElements.Response> {
const client = this.getNextDataClient();
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/internal/clients/cache/IDataClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ export interface IDataClient {
sortedSetPutElements(
cacheName: string,
sortedSetName: string,
elements: Map<string | Uint8Array, number> | Record<string, number>,
elements:
| Map<string | Uint8Array, number>
| Record<string, number>
| Array<[string, number]>,
ttl?: CollectionTtl
): Promise<CacheSortedSetPutElements.Response>;
sortedSetGetRank(
Expand Down

0 comments on commit 258c167

Please sign in to comment.