-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): add AsyncApi 2.0.x YAML reference parser
- Loading branch information
Showing
16 changed files
with
351 additions
and
19 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
apidom/packages/apidom-parser-adapter-asyncapi-yaml-2-0/src/adapter-browser.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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export { default as parse, namespace } from './parser/index-browser'; | ||
export { detect, mediaTypes } from './adapter'; | ||
|
||
export { default as specification } from './parser/specification'; | ||
|
||
export { default as DocumentVisitor } from './parser/visitors/DocumentVisitor'; |
4 changes: 4 additions & 0 deletions
4
apidom/packages/apidom-parser-adapter-asyncapi-yaml-2-0/src/adapter-node.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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export { default as parse, namespace } from './parser/index-node'; | ||
export { mediaTypes, detect } from './adapter'; | ||
|
||
export { default as specification } from './parser/specification'; | ||
|
||
export { default as DocumentVisitor } from './parser/visitors/DocumentVisitor'; |
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
12 changes: 11 additions & 1 deletion
12
apidom/packages/apidom-parser-adapter-asyncapi-yaml-2-0/src/parser/predicates.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 |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import { pathSatisfies, startsWith, both, curry } from 'ramda'; | ||
import { isYamlKeyValuePair } from 'apidom-ast'; | ||
import { isYamlMapping, isYamlKeyValuePair } from 'apidom-ast'; | ||
// @ts-ignore | ||
import { hasKeys } from 'apidom-parser-adapter-yaml-1-2'; | ||
|
||
// isAsyncApiExtension :: (Options, YamlKeyValuePair) -> Boolean | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const isAsyncApiExtension = curry((options, node) => | ||
both(isYamlKeyValuePair, pathSatisfies(startsWith('x-'), ['key', 'content']))(node), | ||
); | ||
|
||
// isReferenceObject :: Options -> YamlMapping -> Boolean | ||
export const isReferenceObject = curry((options, node) => { | ||
if (!isYamlMapping(node)) { | ||
return false; | ||
} | ||
return hasKeys(['$ref'], node.properties); | ||
}); |
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
7 changes: 7 additions & 0 deletions
7
...apter-asyncapi-yaml-2-0/src/parser/visitors/async-api-2-0/parameter/DescriptionVisitor.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,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const DescriptionVisitor = stampit(KindVisitor); | ||
|
||
export default DescriptionVisitor; |
7 changes: 7 additions & 0 deletions
7
...-adapter-asyncapi-yaml-2-0/src/parser/visitors/async-api-2-0/parameter/LocationVisitor.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,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const LocationVisitor = stampit(KindVisitor); | ||
|
||
export default LocationVisitor; |
16 changes: 16 additions & 0 deletions
16
...dom-parser-adapter-asyncapi-yaml-2-0/src/parser/visitors/async-api-2-0/parameter/index.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,16 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
|
||
import FixedFieldsJsonObjectVisitor from '../../generics/FixedFieldsYamlMappingVisitor'; | ||
import { KindVisitor } from '../../generics'; | ||
|
||
const ParameterVisitor = stampit(KindVisitor, FixedFieldsJsonObjectVisitor, { | ||
props: { | ||
specPath: always(['document', 'objects', 'Parameter']), | ||
}, | ||
init() { | ||
this.element = new this.namespace.elements.Parameter(); | ||
}, | ||
}); | ||
|
||
export default ParameterVisitor; |
15 changes: 12 additions & 3 deletions
15
...om-parser-adapter-asyncapi-yaml-2-0/src/parser/visitors/async-api-2-0/parameters/index.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 |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import stampit from 'stampit'; | ||
import { test, always } from 'ramda'; | ||
import { test } from 'ramda'; | ||
import { isYamlMapping, JsonNode } from 'apidom-ast'; | ||
// @ts-ignore | ||
import { isReferenceObject } from 'apidom-parser-adapter-asyncapi-json-2-0'; | ||
|
||
import PatternedFieldsYamlMappingVisitor from '../../generics/PatternedFieldsYamlMappingVisitor'; | ||
import { KindVisitor } from '../../generics'; | ||
|
||
const ParametersVisitor = stampit(KindVisitor, PatternedFieldsYamlMappingVisitor, { | ||
props: { | ||
// TODO([email protected]): replace generic value spec with concrete objects | ||
specPath: always(['kind']), | ||
specPath: (node: JsonNode) => { | ||
// eslint-disable-next-line no-nested-ternary | ||
return isReferenceObject({}, node) | ||
? ['document', 'objects', 'Reference'] | ||
: isYamlMapping(node) | ||
? ['document', 'objects', 'Parameter'] | ||
: ['value']; | ||
}, | ||
fieldPatternPredicate: test(/^[A-Za-z0-9_\\-]+$/), | ||
}, | ||
init() { | ||
|
7 changes: 7 additions & 0 deletions
7
...rser-adapter-asyncapi-yaml-2-0/src/parser/visitors/async-api-2-0/reference/$RefVisitor.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,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const $RefVisitor = stampit(KindVisitor); | ||
|
||
export default $RefVisitor; |
17 changes: 17 additions & 0 deletions
17
...dom-parser-adapter-asyncapi-yaml-2-0/src/parser/visitors/async-api-2-0/reference/index.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,17 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
|
||
import FixedFieldsYamlMappingVisitor from '../../generics/FixedFieldsYamlMappingVisitor'; | ||
import { KindVisitor } from '../../generics'; | ||
|
||
const ReferenceVisitor = stampit(KindVisitor, FixedFieldsYamlMappingVisitor, { | ||
props: { | ||
specPath: always(['document', 'objects', 'Reference']), | ||
canSupportSpecificationExtensions: false, | ||
}, | ||
init() { | ||
this.element = new this.namespace.elements.Reference(); | ||
}, | ||
}); | ||
|
||
export default ReferenceVisitor; |
65 changes: 65 additions & 0 deletions
65
.../packages/apidom-reference/src/parsers/apidom-reference-parser-asyncapi-yaml-2-0/index.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,65 @@ | ||
import stampit from 'stampit'; | ||
import { assocPath, always } from 'ramda'; | ||
import { ParseResultElement } from 'apidom'; | ||
// @ts-ignore | ||
import { parse, specification } from 'apidom-parser-adapter-asyncapi-yaml-2-0'; | ||
|
||
import File from '../../util/File'; | ||
import { ParserError } from '../../util/errors'; | ||
import { documentVisitorFactory } from './visitors/DocumentVisitor'; | ||
|
||
interface AsyncApiYaml2_0Parser { | ||
allowEmpty: boolean; | ||
sourceMap: boolean; | ||
specPath: string; | ||
|
||
canParse(file: File): boolean; | ||
parse(file: File): Promise<ParseResultElement>; | ||
} | ||
|
||
const AsyncApiYaml2_0Parser: stampit.Stamp<AsyncApiYaml2_0Parser> = stampit({ | ||
props: { | ||
/** | ||
* Whether to allow "empty" files. This includes zero-byte files. | ||
*/ | ||
allowEmpty: true, | ||
|
||
/** | ||
* Whether to generate source map during parsing. | ||
*/ | ||
sourceMap: false, | ||
|
||
/** | ||
* Path of the Specification object where visitor is located. | ||
*/ | ||
specPath: always(['kind']), | ||
}, | ||
init( | ||
this: AsyncApiYaml2_0Parser, | ||
{ allowEmpty = this.allowEmpty, sourceMap = this.sourceMap, specPath = this.specPath } = {}, | ||
) { | ||
this.allowEmpty = allowEmpty; | ||
this.sourceMap = sourceMap; | ||
this.specPath = specPath; | ||
}, | ||
methods: { | ||
canParse(file: File): boolean { | ||
return ['.yaml', '.yml'].includes(file.extension); | ||
}, | ||
async parse(file: File): Promise<ParseResultElement> { | ||
const specObj = assocPath( | ||
['visitors', 'document', '$visitor'], | ||
documentVisitorFactory(this.specPath), | ||
specification, | ||
); | ||
|
||
try { | ||
return await parse(file.data, { sourceMap: this.sourceMap, specObj }); | ||
} catch (e) { | ||
throw new ParserError(`Error parsing "${file.url}"`, e); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default AsyncApiYaml2_0Parser; |
29 changes: 29 additions & 0 deletions
29
...ference/src/parsers/apidom-reference-parser-asyncapi-yaml-2-0/visitors/DocumentVisitor.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,29 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
// @ts-ignore | ||
import { DocumentVisitor as AsyncApiYamlDocumentVisitor } from 'apidom-parser-adapter-asyncapi-yaml-2-0'; | ||
import { YamlMapping } from 'apidom-ast'; | ||
|
||
const DocumentVisitor = stampit(AsyncApiYamlDocumentVisitor, { | ||
props: { | ||
specPath: always(['kind']), | ||
}, | ||
// @ts-ignore | ||
init({ specPath = this.specPath } = {}) { | ||
this.specPath = specPath; | ||
}, | ||
methods: { | ||
mapping(mappingNode: YamlMapping) { | ||
const element = this.nodeToElement(this.specPath(), mappingNode); | ||
this.element.content.push(element); | ||
}, | ||
}, | ||
}); | ||
|
||
type SpecPath = () => string[]; | ||
type Options = Record<string, unknown>; | ||
|
||
export const documentVisitorFactory = (specPath: SpecPath) => (opts: Options) => | ||
DocumentVisitor({ specPath, ...opts }); | ||
|
||
export default DocumentVisitor; |
5 changes: 5 additions & 0 deletions
5
...-reference/test/parsers/apidom-reference-parser-asyncapi-yaml-2-0/fixtures/parameter.yaml
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,5 @@ | ||
--- | ||
userId: | ||
description: Id of the user. | ||
schema: | ||
type: string |
Oops, something went wrong.