-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: init custom parser functionality (#489)
- Loading branch information
1 parent
9769ac4
commit ddc0383
Showing
14 changed files
with
511 additions
and
12 deletions.
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 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
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,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 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 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 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
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,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.