Skip to content

Commit

Permalink
feat: support array inputs for DictionarySetFields (#1101)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
cprice404 authored Jan 16, 2024
1 parent ea30e6c commit 83afb8f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/client-sdk-nodejs/src/internal/cache-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,8 @@ export class CacheDataClient implements IDataClient {
dictionaryName: string,
elements:
| Map<string | Uint8Array, string | Uint8Array>
| Record<string, string | Uint8Array>,
| Record<string, string | Uint8Array>
| Array<[string, string | Uint8Array]>,
ttl: CollectionTtl = CollectionTtl.fromCacheTtl()
): Promise<CacheDictionarySetFields.Response> {
try {
Expand All @@ -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,
Expand Down Expand Up @@ -3127,12 +3128,15 @@ export class CacheDataClient implements IDataClient {
return v.map(i => this.convert(i));
}

private convertMapOrRecord(
private convertElements(
elements:
| Map<string | Uint8Array, string | Uint8Array>
| Record<string, string | Uint8Array>
| 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({
Expand Down
12 changes: 8 additions & 4 deletions packages/client-sdk-web/src/internal/cache-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,8 @@ export class CacheDataClient<
dictionaryName: string,
elements:
| Map<string | Uint8Array, string | Uint8Array>
| Record<string, string | Uint8Array>,
| Record<string, string | Uint8Array>
| Array<[string, string | Uint8Array]>,
ttl: CollectionTtl = CollectionTtl.fromCacheTtl()
): Promise<CacheDictionarySetFields.Response> {
try {
Expand All @@ -1514,7 +1515,7 @@ export class CacheDataClient<
}`
);

const dictionaryFieldValuePairs = this.convertMapOrRecord(elements);
const dictionaryFieldValuePairs = this.convertElements(elements);

const result = await this.sendDictionarySetFields(
cacheName,
Expand Down Expand Up @@ -3472,12 +3473,15 @@ export class CacheDataClient<
return v.map(i => this.convertToUint8Array(i));
}

private convertMapOrRecord(
private convertElements(
elements:
| Map<string | Uint8Array, string | Uint8Array>
| Record<string, string | Uint8Array>
| 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]))
Expand Down
42 changes: 42 additions & 0 deletions packages/common-integration-tests/src/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/clients/ICacheClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ export interface ICacheClient extends IControlClient, IPingClient {
dictionaryName: string,
elements:
| Map<string | Uint8Array, string | Uint8Array>
| Record<string, string | Uint8Array>,
| Record<string, string | Uint8Array>
| Array<[string, string | Uint8Array]>,
options?: DictionarySetFieldsOptions
): Promise<CacheDictionarySetFields.Response>;
dictionaryGetField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,8 @@ export abstract class AbstractCacheClient implements ICacheClient {
dictionaryName: string,
elements:
| Map<string | Uint8Array, string | Uint8Array>
| Record<string, string | Uint8Array>,
| Record<string, string | Uint8Array>
| Array<[string, string | Uint8Array]>,
options?: DictionarySetFieldOptions
): Promise<CacheDictionarySetFields.Response> {
const client = this.getNextDataClient();
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/internal/clients/cache/IDataClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ export interface IDataClient {
dictionaryName: string,
elements:
| Map<string | Uint8Array, string | Uint8Array>
| Record<string, string | Uint8Array>,
| Record<string, string | Uint8Array>
| Array<[string, string | Uint8Array]>,
ttl?: CollectionTtl
): Promise<CacheDictionarySetFields.Response>;
dictionaryGetField(
Expand Down

0 comments on commit 83afb8f

Please sign in to comment.