From 8bea0cc72e5f784a78404b39aaff4d322be5369a Mon Sep 17 00:00:00 2001 From: Mac Date: Wed, 30 Jun 2021 22:12:48 +0530 Subject: [PATCH 1/4] fix(amplify-appsync-simulator): string functions not called on items of JavaArray in the resolvers --- .../src/velocity/value-mapper/array.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts b/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts index 912c20926e4..9936b4aed4c 100644 --- a/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts +++ b/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts @@ -1,3 +1,4 @@ +import { JavaString } from './string'; import { toJSON } from './to-json'; export class JavaArray extends Array { @@ -7,6 +8,13 @@ export class JavaArray extends Array { // splice sends a single object values = [values]; } + values = values.map(value => { + if (typeof value === 'string' && !((value as any) instanceof JavaString)) { + return new JavaString(value); + } + return value; + }); + if (values.length !== 1) { super(...values); } else { From 9b8ab5ac6d395b03c4f8677c730298c3ac961237 Mon Sep 17 00:00:00 2001 From: Mac Date: Fri, 2 Jul 2021 22:44:34 +0530 Subject: [PATCH 2/4] feat(amplify-appsync-simulator): added toJavaString that accepts a string and returns a JavaString --- .../src/__tests__/velocity/value-mapper/string.test.ts | 6 +++++- .../src/velocity/value-mapper/string.ts | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts index 3657df1f5ad..2867b7e9df7 100644 --- a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts +++ b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts @@ -1,4 +1,4 @@ -import { JavaString } from '../../../velocity/value-mapper/string'; +import { JavaString, toJavaString } from '../../../velocity/value-mapper/string'; describe('JavaString', () => { it('replaceAll', () => { @@ -131,4 +131,8 @@ describe('JavaString', () => { const str4 = new JavaString('foo bar '); expect(str4.trim().toString()).toEqual('foo bar'); }); + it('toJavaString', () => { + const str = toJavaString('foo bar'); + expect(str instanceof JavaString).toBeTruthy(); + }); }); diff --git a/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts b/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts index f07fd90f119..b966b896a75 100644 --- a/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts +++ b/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts @@ -107,3 +107,7 @@ export class JavaString { return this.value && this.value.length; } } +/** + * Mapper function to convert string to JavaString + */ +export const toJavaString = (value: string): JavaString => new JavaString(value); From 775b4c169cac5b35d3e7ea6ee77120c85e7af8c1 Mon Sep 17 00:00:00 2001 From: Mac Date: Fri, 2 Jul 2021 22:45:35 +0530 Subject: [PATCH 3/4] feat(amplify-appsync-simulator): map.keySet returns an a JavaArray with elements of type JavaString --- .../src/__tests__/velocity/value-mapper/map.test.ts | 9 +++++++++ .../src/__tests__/velocity/value-mapper/string.test.ts | 6 +----- .../src/velocity/value-mapper/array.ts | 8 -------- .../src/velocity/value-mapper/map.ts | 3 ++- .../src/velocity/value-mapper/string.ts | 4 ---- 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts index 2cfbbcb56e9..4c97c00dc90 100644 --- a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts +++ b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts @@ -1,4 +1,7 @@ +import { JavaArray } from './../../../velocity/value-mapper/array'; import { JavaMap } from '../../../velocity/value-mapper/map'; +import { JavaString } from '../../../velocity/value-mapper/string'; +import { map as mapper } from '../../../velocity/value-mapper/mapper'; describe('JavaMap', () => { let identityMapper = jest.fn().mockImplementation(val => val); @@ -66,6 +69,12 @@ describe('JavaMap', () => { expect(map.keySet().toJSON()).toEqual(['foo', 'bar']); }); + it('keySet returns a JavaArray with each element of type JavaString', () => { + const obj = { foo: 'Foo Value', bar: 'Bar Value' }; + const map = new JavaMap(obj, identityMapper); + expect(map.keySet()).toEqual(new JavaArray([new JavaString('foo'), new JavaString('bar')], mapper)); + }); + it('put', () => { const map = new JavaMap({}, identityMapper); map.put('foo', 'Foo Value'); diff --git a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts index 2867b7e9df7..3657df1f5ad 100644 --- a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts +++ b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/string.test.ts @@ -1,4 +1,4 @@ -import { JavaString, toJavaString } from '../../../velocity/value-mapper/string'; +import { JavaString } from '../../../velocity/value-mapper/string'; describe('JavaString', () => { it('replaceAll', () => { @@ -131,8 +131,4 @@ describe('JavaString', () => { const str4 = new JavaString('foo bar '); expect(str4.trim().toString()).toEqual('foo bar'); }); - it('toJavaString', () => { - const str = toJavaString('foo bar'); - expect(str instanceof JavaString).toBeTruthy(); - }); }); diff --git a/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts b/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts index 9936b4aed4c..912c20926e4 100644 --- a/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts +++ b/packages/amplify-appsync-simulator/src/velocity/value-mapper/array.ts @@ -1,4 +1,3 @@ -import { JavaString } from './string'; import { toJSON } from './to-json'; export class JavaArray extends Array { @@ -8,13 +7,6 @@ export class JavaArray extends Array { // splice sends a single object values = [values]; } - values = values.map(value => { - if (typeof value === 'string' && !((value as any) instanceof JavaString)) { - return new JavaString(value); - } - return value; - }); - if (values.length !== 1) { super(...values); } else { diff --git a/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts b/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts index d075fa022b0..5a2e7528759 100644 --- a/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts +++ b/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts @@ -1,5 +1,6 @@ import { JavaArray } from './array'; import { toJSON } from './to-json'; +import { map } from './mapper'; export class JavaMap { private map: Map; @@ -56,7 +57,7 @@ export class JavaMap { } keySet() { - return new JavaArray(Array.from(this.map.keys()), this.mapper); + return map(Array.from(this.map.keys())); } put(key, value) { diff --git a/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts b/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts index b966b896a75..f07fd90f119 100644 --- a/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts +++ b/packages/amplify-appsync-simulator/src/velocity/value-mapper/string.ts @@ -107,7 +107,3 @@ export class JavaString { return this.value && this.value.length; } } -/** - * Mapper function to convert string to JavaString - */ -export const toJavaString = (value: string): JavaString => new JavaString(value); From baac61f9064d7021f452d8d4dcc537480b3ae966 Mon Sep 17 00:00:00 2001 From: Mac Date: Tue, 6 Jul 2021 22:13:05 +0530 Subject: [PATCH 4/4] feat(amplify-appsync-simulator): using this.mapper in map.keySet --- .../src/__tests__/velocity/value-mapper/map.test.ts | 2 +- .../amplify-appsync-simulator/src/velocity/value-mapper/map.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts index 4c97c00dc90..60e5fee115f 100644 --- a/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts +++ b/packages/amplify-appsync-simulator/src/__tests__/velocity/value-mapper/map.test.ts @@ -71,7 +71,7 @@ describe('JavaMap', () => { it('keySet returns a JavaArray with each element of type JavaString', () => { const obj = { foo: 'Foo Value', bar: 'Bar Value' }; - const map = new JavaMap(obj, identityMapper); + const map = new JavaMap(obj, mapper); expect(map.keySet()).toEqual(new JavaArray([new JavaString('foo'), new JavaString('bar')], mapper)); }); diff --git a/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts b/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts index 5a2e7528759..184f91f0d36 100644 --- a/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts +++ b/packages/amplify-appsync-simulator/src/velocity/value-mapper/map.ts @@ -1,6 +1,5 @@ import { JavaArray } from './array'; import { toJSON } from './to-json'; -import { map } from './mapper'; export class JavaMap { private map: Map; @@ -57,7 +56,7 @@ export class JavaMap { } keySet() { - return map(Array.from(this.map.keys())); + return new JavaArray(Array.from(this.map.keys()).map(this.mapper as any), this.mapper); } put(key, value) {