-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for nested json (#623)
- Loading branch information
Justin Shih
committed
Sep 7, 2022
1 parent
5e63847
commit 8f0bff0
Showing
23 changed files
with
1,421 additions
and
435 deletions.
There are no files selected for viewing
484 changes: 362 additions & 122 deletions
484
...s/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/form-state.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`nested state should generate state structure for nested keyPath 1`] = `"{ ...bio, favoriteAnimal: { ...bio?.favoriteAnimal, animalMeta: { ...bio?.favoriteAnimal?.animalMeta, family: { ...bio?.favoriteAnimal?.animalMeta?.family, genus: value } } } }"`; | ||
exports[`nested state should generate value for 2nd level deep object 1`] = `"{ ...bio, firstName: \\"John C\\" }"`; | ||
exports[`set field state should generate state call for nested object 1`] = `"setBio({ ...bio, favoriteAnimal: { ...bio?.favoriteAnimal, animalMeta: { ...bio?.favoriteAnimal?.animalMeta, family: { ...bio?.favoriteAnimal?.animalMeta?.family, genus: \\"hello World\\" } } } })"`; | ||
exports[`set field state should generate state call for non-nested objects 1`] = `"setFirstName(\\"john c\\")"`; |
25 changes: 25 additions & 0 deletions
25
packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/type-helper.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should generate nested object should generate type for nested object 1`] = ` | ||
"export declare type myCreateFormInputValues = { | ||
firstName?: ValidationFunction<string>; | ||
isExplorer?: ValidationFunction<boolean>; | ||
bio?: { | ||
favoriteAnimal?: { | ||
animalMeta?: { | ||
family?: { | ||
genus?: ValidationFunction<string>; | ||
}; | ||
earliestRecord?: ValidationFunction<number>; | ||
}; | ||
}; | ||
}; | ||
};" | ||
`; | ||
exports[`should generate nested object should generate type for non nested object 1`] = ` | ||
"export declare type myCreateFormInputValues = { | ||
firstName?: ValidationFunction<string>; | ||
isExplorer?: ValidationFunction<boolean>; | ||
};" | ||
`; |
59 changes: 59 additions & 0 deletions
59
packages/codegen-ui-react/lib/__tests__/forms/form-state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
import { factory } from 'typescript'; | ||
import { buildNestedStateSet, setFieldState } from '../../forms/form-state'; | ||
import { genericPrinter } from '../__utils__'; | ||
|
||
describe('nested state', () => { | ||
it('should generate state structure for nested keyPath', () => { | ||
const state = buildNestedStateSet( | ||
['bio', 'favoriteAnimal', 'animalMeta', 'family', 'genus'], | ||
['bio'], | ||
factory.createIdentifier('value'), | ||
); | ||
const response = genericPrinter(state); | ||
expect(response).toMatchSnapshot(); | ||
}); | ||
|
||
it('should generate value for 2nd level deep object', () => { | ||
const state = buildNestedStateSet(['bio', 'firstName'], ['bio'], factory.createStringLiteral('John C')); | ||
const response = genericPrinter(state); | ||
expect(response).toMatchSnapshot(); | ||
}); | ||
|
||
it('should throw error for 1 level deep path', () => { | ||
expect(() => buildNestedStateSet(['firstName'], ['firstName'], factory.createStringLiteral('John C'))).toThrowError( | ||
'keyPath needs a length larger than 1 to build nested state object', | ||
); | ||
}); | ||
}); | ||
|
||
describe('set field state', () => { | ||
it('should generate state call for nested object', () => { | ||
const fieldStateSetter = setFieldState( | ||
'bio.favoriteAnimal.animalMeta.family.genus', | ||
factory.createStringLiteral('hello World'), | ||
); | ||
const response = genericPrinter(fieldStateSetter); | ||
expect(response).toMatchSnapshot(); | ||
}); | ||
|
||
it('should generate state call for non-nested objects', () => { | ||
const fieldStateSetter = setFieldState('firstName', factory.createStringLiteral('john c')); | ||
const response = genericPrinter(fieldStateSetter); | ||
expect(response).toMatchSnapshot(); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
packages/codegen-ui-react/lib/__tests__/forms/type-helper.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { FieldConfigMetadata } from '@aws-amplify/codegen-ui'; | ||
import { generateOnValidationType } from '../../forms/type-helper'; | ||
import { genericPrinter } from '../__utils__'; | ||
|
||
describe('should generate nested object', () => { | ||
it('should generate type for nested object', () => { | ||
const fieldConfigs: Record<string, FieldConfigMetadata> = { | ||
'bio.favoriteAnimal.animalMeta.family.genus': { | ||
dataType: 'String', | ||
validationRules: [], | ||
}, | ||
'bio.favoriteAnimal.animalMeta.earliestRecord': { | ||
dataType: 'AWSTimestamp', | ||
validationRules: [], | ||
}, | ||
firstName: { | ||
dataType: 'String', | ||
validationRules: [], | ||
}, | ||
isExplorer: { | ||
dataType: 'Boolean', | ||
validationRules: [], | ||
}, | ||
}; | ||
const types = generateOnValidationType('myCreateForm', fieldConfigs); | ||
const response = genericPrinter(types); | ||
expect(response).toMatchSnapshot(); | ||
}); | ||
|
||
it('should generate type for non nested object', () => { | ||
const fieldConfigs: Record<string, FieldConfigMetadata> = { | ||
firstName: { | ||
dataType: 'String', | ||
validationRules: [], | ||
}, | ||
isExplorer: { | ||
dataType: 'Boolean', | ||
validationRules: [], | ||
}, | ||
}; | ||
const types = generateOnValidationType('myCreateForm', fieldConfigs); | ||
const response = genericPrinter(types); | ||
expect(response).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/codegen-ui-react/lib/__tests__/utils/fetch-by-path.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { fetchByPath } from '../../utils/json-path-fetch'; | ||
|
||
describe('fetch by path util', () => { | ||
const nestedObj = { | ||
levelOne: { | ||
levelTwo: { | ||
levelThree: { | ||
bingo: (value: string) => `Winner Winner ${value}!`, | ||
}, | ||
}, | ||
}, | ||
}; | ||
it('should fetch value from nested object', () => { | ||
const fn: Function = fetchByPath(nestedObj, 'levelOne.levelTwo.levelThree.bingo'); | ||
const result = fn('helloWorld'); | ||
expect(result).toEqual('Winner Winner helloWorld!'); | ||
}); | ||
|
||
it('should return undefined if value does not exist in nested object', () => { | ||
const result = fetchByPath(nestedObj, 'levelOne.levelTwo.nonExistentLevel'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.