-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5369a57
commit 85087e4
Showing
6 changed files
with
89 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,60 @@ | ||
import type { AsyncAPIDocument, ChannelParameter, Message, MessageTrait, Schema } from '@asyncapi/parser'; | ||
|
||
import type { | ||
AsyncAPIDocument, | ||
ChannelParameter, | ||
Message, | ||
Schema, | ||
} from '@asyncapi/parser'; | ||
|
||
import { JSONPath } from 'jsonpath-plus'; | ||
import _ from 'lodash'; | ||
/** | ||
* This class will provide all sorts of data for optimizers. | ||
* | ||
* @private | ||
*/ | ||
export class ComponentProvider { | ||
messagePaths = ['$.channels.*.*.message[?(@property !== "oneOf")]^', '$.channels.*.*.message.oneOf.*', '$.components.messages.*']; | ||
schemaPaths = [ | ||
'$.channels.*.*.message.traits[*]..[?(@.type)]', | ||
'$.channels.*.*.message.headers', | ||
'$.channels.*.*.message.headers..[?(@.type)]', | ||
'$.channels.*.*.message.payload', | ||
'$.channels.*.*.message.payload..[?(@.type)]', | ||
'$.channels.*.parameters.*.schema[?(@.type)]', | ||
'$.channels.*.parameters.*.schema..[?(@.type)]', | ||
'$.components.schemas..[?(@.type)]', | ||
]; | ||
|
||
parameterPaths = ['$.channels.*.parameters.*', '$.components.parameters.*']; | ||
messages = new Map<string, Message>(); | ||
schemas = new Map<string, Schema>(); | ||
parameters = new Map<string, ChannelParameter>(); | ||
|
||
constructor(private document: AsyncAPIDocument) { | ||
this.scanChannels(); | ||
this.scanComponents(); | ||
} | ||
|
||
private scanChannels(): void { | ||
const channels = this.document.channels(); | ||
for (const channelName in channels) { | ||
const path = `channels.${channelName}`; | ||
const channel = this.document.channel(channelName); | ||
|
||
if (channel.hasPublish()) { | ||
const message = channel.publish().message(); | ||
const currentPath = `${path}.publish.message`; | ||
this.scanMessage(currentPath, message); | ||
this.messages.set(currentPath, message); | ||
} | ||
|
||
if (channel.hasSubscribe()) { | ||
const message = channel.subscribe().message(); | ||
const currentPath = `${path}.subscribe.message`; | ||
this.scanMessage(currentPath, message); | ||
this.messages.set(currentPath, message); | ||
} | ||
|
||
if (channel.hasParameters()) { | ||
const currentPath = `${path}.parameters`; | ||
for (const [name, channelParameter] of Object.entries(channel.parameters())) { | ||
this.parameters.set(`${currentPath}.${name}`, channelParameter); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private scanSchema(path: string, schema: Schema): void { | ||
if (!schema) {return;} | ||
this.schemas.set(path, schema); | ||
const schemaProperties = schema.properties(); | ||
for (const [propertyName, propertySchema] of Object.entries(schemaProperties)) { | ||
this.schemas.set(`${path}.properties.${propertyName}`, propertySchema); | ||
} | ||
} | ||
|
||
private scanMessageTraits(path: string, traits: MessageTrait[]): void { | ||
for (const [index, trait] of Object.entries(traits)) { | ||
if (trait.headers()) { | ||
this.scanSchema(`${path}[${index}].headers`, trait.headers()); | ||
} | ||
} | ||
this.messages = this.parseComponents(this.messagePaths); | ||
this.schemas = this.parseComponents(this.schemaPaths); | ||
this.parameters = this.parseComponents(this.parameterPaths); | ||
} | ||
|
||
private scanMessage(path: string, message: Message): void { | ||
this.scanSchema(`${path}.payload`, message.payload()); | ||
this.scanSchema(`${path}.headers`, message.headers()); | ||
this.scanMessageTraits(`${path}.traits`, message.traits()); | ||
private toLodashPath(path: string) { | ||
return path.replace(/'\]\['/g, '.') | ||
.slice(3, -2) | ||
.replace(/'\]/g, '') | ||
.replace(/\['/g, '.'); | ||
} | ||
|
||
private scanComponents(): void { | ||
const components = this.document.components(); | ||
if (!components) { | ||
return; | ||
} | ||
|
||
if (components.hasMessages()) { | ||
for (const [messageName, message] of Object.entries(components.messages())) { | ||
this.messages.set(`components.messages.${messageName}`, message); | ||
} | ||
} | ||
|
||
if (components.hasSchemas()) { | ||
for (const [schemaName, schema] of Object.entries(components.schemas())) { | ||
this.schemas.set(`components.schemas.${schemaName}`, schema); | ||
} | ||
} | ||
|
||
if (components.hasParameters()) { | ||
for (const [parameterName, parameter] of Object.entries(components.parameters())) { | ||
this.parameters.set(`components.parameters.${parameterName}`, parameter); | ||
} | ||
} | ||
private parseComponents(paths: string[]): any { | ||
return _.chain(paths) | ||
.map((path) => { | ||
return JSONPath({ | ||
resultType: 'all', | ||
json: this.document.json(), | ||
path | ||
}); | ||
}) | ||
.flatten() | ||
.reduce((red, component) => { | ||
return red.set(this.toLodashPath(component.path), component.value); | ||
}, new Map()) | ||
.value(); | ||
} | ||
} |
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