-
-
Notifications
You must be signed in to change notification settings - Fork 108
refactor: init custom parser functionality #489
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
asyncapi-bot
merged 2 commits into
asyncapi:next-major
from
magicmatatjahu:next/custom-parsers
Mar 11, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,60 @@ | ||
import { JSONPath } from 'jsonpath-plus'; | ||
|
||
import { xParserOriginalTraits } from '../constants'; | ||
import { mergePatch } from '../utils'; | ||
|
||
const v2TraitPaths = [ | ||
// operations | ||
'$.channels.*.[publish,subscribe]', | ||
'$.components.channels.*.[publish,subscribe]', | ||
// messages | ||
'$.channels.*.[publish,subscribe].message', | ||
'$.channels.*.[publish,subscribe].message.oneOf.*', | ||
'$.components.channels.*.[publish,subscribe].message', | ||
'$.components.channels.*.[publish,subscribe].message.oneOf.*', | ||
'$.components.messages.*', | ||
]; | ||
|
||
export function applyTraitsV2(asyncapi: Record<string, unknown>) { | ||
applyAllTraits(asyncapi, v2TraitPaths); | ||
} | ||
|
||
const v3TraitPaths = [ | ||
// operations | ||
'$.channels.*.[publish,subscribe]', | ||
'$.components.channels.*.[publish,subscribe]', | ||
// messages | ||
'$.channels.*.[publish,subscribe].message', | ||
'$.channels.*.[publish,subscribe].message.oneOf.*', | ||
'$.components.channels.*.[publish,subscribe].message', | ||
'$.components.channels.*.[publish,subscribe].message.oneOf.*', | ||
'$.components.messages.*', | ||
]; | ||
|
||
export function applyTraitsV3(asyncapi: Record<string, unknown>) { | ||
applyAllTraits(asyncapi, v3TraitPaths); | ||
} | ||
|
||
function applyAllTraits(asyncapi: Record<string, unknown>, paths: string[]) { | ||
paths.forEach(path => { | ||
JSONPath({ | ||
path, | ||
json: asyncapi, | ||
resultType: 'value', | ||
callback(value) { applyTraits(value); }, | ||
}); | ||
}); | ||
} | ||
|
||
function applyTraits(value: Record<string, unknown>) { | ||
if (Array.isArray(value.traits)) { | ||
for (const trait of value.traits) { | ||
for (const key in trait) { | ||
value[String(key)] = mergePatch(value[String(key)], trait[String(key)]); | ||
} | ||
} | ||
|
||
value[xParserOriginalTraits] = value.traits; | ||
delete value.traits; | ||
} | ||
} |
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,27 @@ | ||
import { applyTraitsV2, applyTraitsV3 } from './apply-traits'; | ||
import { parseSchemasV2 } from './parse-schema'; | ||
|
||
import type { ParseOptions } from "../parse"; | ||
import type { DetailedAsyncAPI } from "../types"; | ||
|
||
export async function customOperations(detailed: DetailedAsyncAPI, options: ParseOptions): Promise<void> { | ||
switch (detailed.semver.major) { | ||
case 2: return operationsV2(detailed, options); | ||
case 3: return operationsV3(detailed, options); | ||
} | ||
} | ||
|
||
async function operationsV2(detailed: DetailedAsyncAPI, options: ParseOptions): Promise<void> { | ||
if (options.applyTraits) { | ||
applyTraitsV2(detailed.parsed); | ||
} | ||
if (options.parseSchemas) { | ||
await parseSchemasV2(detailed); | ||
} | ||
} | ||
|
||
async function operationsV3(detailed: DetailedAsyncAPI, options: ParseOptions): Promise<void> { | ||
if (options.applyTraits) { | ||
applyTraitsV3(detailed.parsed); | ||
} | ||
} |
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,66 @@ | ||
import { JSONPath } from 'jsonpath-plus'; | ||
import { toPath } from 'lodash'; | ||
|
||
import { parseSchema, getDefaultSchemaFormat } from '../schema-parser'; | ||
import { xParserOriginalSchemaFormat } from '../constants'; | ||
|
||
import type { ParseSchemaInput } from "../schema-parser"; | ||
import type { DetailedAsyncAPI } from "../types"; | ||
|
||
interface ToParseItem { | ||
input: ParseSchemaInput; | ||
value: any; | ||
} | ||
|
||
const customSchemasPathsV2 = [ | ||
'$.channels.*.[publish,subscribe].message', | ||
'$.channels.*.[publish,subscribe].message.oneOf.*', | ||
'$.components.channels.*.[publish,subscribe].message', | ||
'$.components.channels.*.[publish,subscribe].message.oneOf.*', | ||
'$.components.messages.*', | ||
]; | ||
|
||
export async function parseSchemasV2(detailed: DetailedAsyncAPI) { | ||
const defaultSchemaFormat = getDefaultSchemaFormat(detailed.parsed.asyncapi as string); | ||
const parseItems: Array<ToParseItem> = []; | ||
|
||
const visited: Set<unknown> = new Set(); | ||
customSchemasPathsV2.forEach(path => { | ||
JSONPath({ | ||
path, | ||
json: detailed.parsed, | ||
resultType: 'all', | ||
callback(result) { | ||
const value = result.value; | ||
if (visited.has(value)) { | ||
return; | ||
} | ||
visited.add(value); | ||
|
||
const payload = value.payload; | ||
if (!payload) { | ||
return; | ||
} | ||
|
||
parseItems.push({ | ||
input: { | ||
asyncapi: detailed, | ||
data: payload, | ||
meta: undefined, | ||
path: [...toPath(result.path.slice(1)), 'payload'], | ||
schemaFormat: value.schemaFormat || defaultSchemaFormat, | ||
defaultSchemaFormat, | ||
}, | ||
value, | ||
}); | ||
}, | ||
}); | ||
}); | ||
|
||
return Promise.all(parseItems.map(parseSchemaV2)); | ||
} | ||
|
||
async function parseSchemaV2(item: ToParseItem) { | ||
item.value[xParserOriginalSchemaFormat] = item.input.schemaFormat; | ||
item.value.payload = await parseSchema(item.input); | ||
} |
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
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
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,33 @@ | ||
import { SchemaParser } from "../schema-parser"; | ||
|
||
export function AsyncAPISchemaParser(): SchemaParser { | ||
return { | ||
validate, | ||
parse, | ||
getMimeTypes, | ||
} | ||
} | ||
|
||
function validate() { | ||
|
||
} | ||
|
||
function parse() { | ||
|
||
} | ||
|
||
function getMimeTypes() { | ||
const mimeTypes = [ | ||
'application/schema;version=draft-07', | ||
'application/schema+json;version=draft-07', | ||
'application/schema+yaml;version=draft-07', | ||
]; | ||
['2.0.0', '2.1.0', '2.2.0', '2.3.0'].forEach(version => { | ||
mimeTypes.push( | ||
`application/vnd.aai.asyncapi;version=${version}`, | ||
`application/vnd.aai.asyncapi+json;version=${version}`, | ||
`application/vnd.aai.asyncapi+yaml;version=${version}`, | ||
); | ||
}); | ||
return mimeTypes; | ||
} |
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.
v2
meansv2
of the spec here? If so, shall we extract the paths to their own files so we can do the same forv3
spec ?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.
We will have only 2 custom operations and I don't think so that we should split them to separate files. We can do it later, if we will have more logic for each version.