-
Notifications
You must be signed in to change notification settings - Fork 647
Feature/type generator #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a46b677
Add service model parser package
jeskew d854eb9
Rename ApiDefinition file
jeskew 963df2c
Make the parser emit shapes and operations as defined in @aws/types i…
jeskew 0f02529
Refactor parser and improve test coverage
jeskew f06eb7a
Do not rename error shapes when normalizing models
jeskew a2efdfe
Pass enum traits on to tree model
jeskew 82d42b3
Add optional documentation property to TreeModelMember interface
jeskew a59e654
Make the API model explicitly an SDK internal
jeskew c0c7a1b
Add more tests to ensure valid models are accepted
jeskew cb87483
Add documentation for public types and mark internal types as such
jeskew 43de9a8
WIP commit
jeskew 9f72cf6
100% test coverage FTW
jeskew 3425613
Update serialization model generator to use TreeModel instead of Norm…
jeskew 175542b
Update typescript definition generator to use TreeModel instead of No…
jeskew 7f9897e
Ensure generator tests are not exhausting empty shape and operation maps
jeskew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -20,5 +20,3 @@ jspm_packages | |
| .yarn-integrity | ||
|
|
||
| lerna-debug.log | ||
| *.js | ||
| *.js.map | ||
This file contains hidden or 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,3 @@ | ||
| *.js | ||
| *.js.map | ||
| *.d.ts |
This file contains hidden or 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,212 @@ | ||
| import {ShapeMap} from "../lib/ApiModel/ShapeMap"; | ||
| import {Shape, Type} from "../lib/ApiModel/Shape"; | ||
| import {NormalizedModel} from "../lib/TreeModel/types"; | ||
| import {ServiceMetadata} from "../lib/ApiModel/ServiceMetadata"; | ||
|
|
||
| export const minimalValidServiceMetadata: ServiceMetadata = { | ||
| apiVersion: 'string', | ||
| endpointPrefix: 'string', | ||
| protocol: 'json', | ||
| serviceFullName: 'string', | ||
| signatureVersion: 'v4', | ||
| uid: 'string', | ||
| }; | ||
|
|
||
| export const minimalShapeMap: ShapeMap & {[key in Type]: Shape} = { | ||
| blob: {type: 'blob'}, | ||
| boolean: {type: 'boolean'}, | ||
| byte: {type: 'byte'}, | ||
| character: {type: 'character'}, | ||
| double: {type: 'double'}, | ||
| float: {type: 'float'}, | ||
| integer: {type: 'integer'}, | ||
| list: { | ||
| type: 'list', | ||
| member: {shape: 'boolean'}, | ||
| }, | ||
| long: {type: 'long'}, | ||
| map: { | ||
| type: 'map', | ||
| key: {shape: 'string'}, | ||
| value: {shape: 'blob'}, | ||
| }, | ||
| short: {type: 'short'}, | ||
| string: {type: 'string'}, | ||
| structure: { | ||
| type: 'structure', | ||
| members: {}, | ||
| }, | ||
| timestamp: {type: 'timestamp'}, | ||
| }; | ||
|
|
||
| export const stringTypes = new Set<Type>([ | ||
| 'character', | ||
| 'string', | ||
| ]); | ||
|
|
||
| export const numericTypes = new Set<Type>([ | ||
| 'byte', | ||
| 'double', | ||
| 'float', | ||
| 'integer', | ||
| 'long', | ||
| 'short', | ||
| ]); | ||
|
|
||
| export const scalarTypes = new Set<Type>([ | ||
| ...numericTypes, | ||
| ...stringTypes, | ||
| 'blob', | ||
| 'boolean', | ||
| 'timestamp', | ||
| ]); | ||
|
|
||
| export const model: NormalizedModel = { | ||
| metadata: { | ||
| apiVersion: '2017-04-30', | ||
| endpointPrefix: 'endpoint', | ||
| protocol: 'rest-json', | ||
| serviceFullName: 'AWS Fake Service', | ||
| signatureVersion: 'v4', | ||
| uid: 'fake-2017-04-30', | ||
| }, | ||
| operations: { | ||
| DeleteResource: { | ||
| name: 'DeleteResource', | ||
| http: { | ||
| method: 'DELETE', | ||
| requestUri: '/resources/{resourceId}' | ||
| }, | ||
| input: {shape: 'DeleteResourceInput'}, | ||
| output: {shape: 'DeleteResourceOutput'}, | ||
| errors: [], | ||
| }, | ||
| GetResource: { | ||
| name: 'GetResource', | ||
| http: { | ||
| method: 'GET', | ||
| requestUri: '/resources/{resourceId}' | ||
| }, | ||
| input: {shape: 'GetResourceInput'}, | ||
| output: {shape: 'GetResourceOutput'}, | ||
| errors: [ | ||
| {shape: 'ResourceNotFoundException'}, | ||
| ], | ||
| }, | ||
| PutResource: { | ||
| name: 'PutResource', | ||
| http: { | ||
| method: 'PUT', | ||
| requestUri: '/resources/{resourceId}' | ||
| }, | ||
| input: {shape: 'PutResourceInput'}, | ||
| output: {shape: 'PutResourceOutput'}, | ||
| errors: [ | ||
| {shape: 'ValidationException'}, | ||
| ], | ||
| }, | ||
| }, | ||
| shapes: { | ||
| ConsumedCapacity: { | ||
| type: 'structure', | ||
| members: {}, | ||
| }, | ||
| DeleteResourceInput: { | ||
| type: 'structure', | ||
| required: ['resourceId'], | ||
| members: { | ||
| resourceId: {shape: 'ResourceId'} | ||
| }, | ||
| topLevel: 'input', | ||
| }, | ||
| DeleteResourceOutput: { | ||
| type: 'structure', | ||
| required: [], | ||
| members: {}, | ||
| topLevel: 'output', | ||
| }, | ||
| GetResourceInput: { | ||
| type: 'structure', | ||
| required: ['resourceId'], | ||
| members: { | ||
| resourceId: {shape: 'ResourceId'} | ||
| }, | ||
| topLevel: 'input', | ||
| }, | ||
| GetResourceOutput: { | ||
| type: 'structure', | ||
| required: [], | ||
| members: { | ||
| data: {shape: 'StreamingBlob'}, | ||
| }, | ||
| payload: 'data', | ||
| topLevel: 'output', | ||
| }, | ||
| NodeId: {type: 'string'}, | ||
| NodeList: { | ||
| type: 'list', | ||
| member: {shape: 'NodeId'}, | ||
| }, | ||
| PrimaryLocationMap: { | ||
| type: 'map', | ||
| key: {shape: 'ResourceId'}, | ||
| value: {shape: 'NodeId'}, | ||
| }, | ||
| PutResourceInput: { | ||
| type: 'structure', | ||
| required: ['resourceId', 'data'], | ||
| members: { | ||
| resourceId: {shape: 'ResourceId'}, | ||
| data: {shape: 'StreamingBlob'}, | ||
| }, | ||
| topLevel: 'input', | ||
| }, | ||
| PutResourceOutput: { | ||
| type: 'structure', | ||
| required: [], | ||
| members: { | ||
| replicatedToNodes: {shape: 'NodeList'}, | ||
| resourcePrimaries: {shape: 'PrimaryLocationMap'}, | ||
| consumedCapacity: {shape: 'ConsumedCapacity'}, | ||
| }, | ||
| topLevel: 'output', | ||
| }, | ||
| ResourceId: {type: 'string'}, | ||
| ResourceNotFoundException: { | ||
| type: 'structure', | ||
| required: [], | ||
| members: {}, | ||
| exception: true, | ||
| }, | ||
| StreamingBlob: { | ||
| type: 'blob', | ||
| streaming: true, | ||
| }, | ||
| ValidationException: { | ||
| type: 'structure', | ||
| required: [], | ||
| members: {}, | ||
| exception: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| // Copies objects and arrays. Only own enumerable properties are preserved. | ||
| export function deepCopy<T>(arg: T): T { | ||
| if (!arg) { | ||
| return arg; | ||
| } | ||
|
|
||
| if (Array.isArray(arg)) { | ||
| return <T><any>arg.map(element => deepCopy(element)); | ||
| } | ||
|
|
||
| if (typeof arg === 'object') { | ||
| return <T>Object.keys(arg).reduce(( | ||
| carry: Partial<T>, | ||
| item: keyof T | ||
| ) => Object.assign(carry, {[item]: deepCopy(arg[item])}), {}); | ||
| } | ||
|
|
||
| return arg; | ||
| } | ||
This file contains hidden or 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,56 @@ | ||
| import {ApiModel, isApiModel} from "../../lib/ApiModel"; | ||
| import {minimalValidServiceMetadata} from "../../__fixtures__"; | ||
|
|
||
| describe('isApiModel', () => { | ||
| const minimalValidApiModel: ApiModel = { | ||
| metadata: minimalValidServiceMetadata, | ||
| operations: {}, | ||
| shapes: {}, | ||
| }; | ||
|
|
||
| it('should accept a valid ApiModel', () => { | ||
| expect(isApiModel(minimalValidApiModel)).toBe(true); | ||
| }); | ||
|
|
||
| it( | ||
| 'should reject objects where "metadata" is not a valid ServiceMetadata object', | ||
| () => { | ||
| expect(isApiModel( | ||
| Object.assign({}, minimalValidApiModel, {metadata: 'string'}) | ||
| )).toBe(false); | ||
| } | ||
| ); | ||
|
|
||
| it('should reject objects where "operations" is not a valid OperationMap', () => { | ||
| expect(isApiModel( | ||
| Object.assign({}, minimalValidApiModel, {operations: 'string'}) | ||
| )).toBe(false); | ||
| }); | ||
|
|
||
| it('should reject objects where "shapes" is not a valid ShapeMap', () => { | ||
| expect(isApiModel( | ||
| Object.assign({}, minimalValidApiModel, {shapes: 'string'}) | ||
| )).toBe(false); | ||
| }); | ||
|
|
||
| it('should accept an ApiModel where "documentation" is present and a string', () => { | ||
| expect(isApiModel( | ||
| Object.assign({}, minimalValidApiModel, {documentation: 'foo'}) | ||
| )).toBe(true); | ||
| }); | ||
|
|
||
| it( | ||
| 'should reject objects where a "documentation" property is present and not a string', | ||
| () => { | ||
| expect(isApiModel( | ||
| Object.assign({}, minimalValidApiModel, {documentation: {}}) | ||
| )).toBe(false); | ||
| } | ||
| ); | ||
|
|
||
| it('should reject scalar values', () => { | ||
| for (let scalar of [null, void 0, 1, 'string', true]) { | ||
| expect(isApiModel(scalar)).toBe(false); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious, is it possible to have a structure without any members, aside from an input/output shape?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not explicitly forbidden, but I don't think any services have empty structures. They could be used as sigils (do one thing if an object exists; do another if it is
null), but I don't think that's a common pattern.