Skip to content

Commit

Permalink
refactor: adapt components to parser-api (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Sep 1, 2022
1 parent 28ef37b commit b5ce5d9
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 141 deletions.
42 changes: 22 additions & 20 deletions src/models/components.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import type { BaseModel } from './base';
import type { ServerInterface } from './server';
import type { ChannelInterface } from './channel';
import type { OperationTraitInterface } from './operation-trait';
import type { MessageInterface } from './message';
import type { MessageTraitInterface } from './message-trait';
import type { SchemaInterface } from './schema';
import type { ChannelParameterInterface } from './channel-parameter';
import type { ServerVariableInterface } from './server-variable';
import type { CorrelationIdInterface } from './correlation-id';
import type { BindingsInterface } from './bindings';
import type { SecuritySchemeInterface } from './security-scheme';
import type { ExtensionsMixinInterface } from './mixins';
import type { ServersInterface } from './servers';
import type { ChannelsInterface } from './channels';
import type { MessagesInterface } from './messages';
import type { SchemasInterface } from './schemas';
import type { ChannelParametersInterface } from './channel-parameters';
import type { ServerVariablesInterface } from './server-variables';
import type { OperationTraitsInterface } from './operation-traits';
import type { MessageTraitsInterface } from './message-traits';
import type { SecuritySchemesInterface } from './security-schemes';
import type { CorrelationIdsInterface } from './correlation-ids';
import type { OperationsInterface } from './operations';

export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface {
servers(): Record<string, ServerInterface>;
channels(): Record<string, ChannelInterface>;
messages(): Record<string, MessageInterface>;
schemas(): Record<string, SchemaInterface>;
channelParameters(): Record<string, ChannelParameterInterface>;
serverVariables(): Record<string, ServerVariableInterface>;
operationTraits(): Record<string, OperationTraitInterface>;
messageTraits(): Record<string, MessageTraitInterface>;
correlationIds(): Record<string, CorrelationIdInterface>;
securitySchemes(): Record<string, SecuritySchemeInterface>;
servers(): ServersInterface;
channels(): ChannelsInterface;
messages(): MessagesInterface;
schemas(): SchemasInterface;
channelParameters(): ChannelParametersInterface;
serverVariables(): ServerVariablesInterface;
operations(): OperationsInterface;
operationTraits(): OperationTraitsInterface;
messageTraits(): MessageTraitsInterface;
correlationIds(): CorrelationIdsInterface;
securitySchemes(): SecuritySchemesInterface;
serverBindings(): Record<string, BindingsInterface>;
channelBindings(): Record<string, BindingsInterface>;
operationBindings(): Record<string, BindingsInterface>;
Expand Down
4 changes: 4 additions & 0 deletions src/models/correlation-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Collection } from './collection';
import type { CorrelationIdInterface } from './correlation-id';

export interface CorrelationIdsInterface extends Collection<CorrelationIdInterface> { }
3 changes: 2 additions & 1 deletion src/models/v2/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export class Channel extends BaseModel<v2.ChannelObject, { id: string, address:
operations(): OperationsInterface {
const operations: OperationInterface[] = [];
['publish', 'subscribe'].forEach(operationAction => {
const id = this._json[operationAction as 'publish' | 'subscribe'] && (this._json[operationAction as 'publish' | 'subscribe'] as v2.OperationObject).operationId || this.meta().id + "_" + operationAction;
this._json[operationAction as 'publish' | 'subscribe'] && operations.push(
this.createModel(Operation, this._json[operationAction as 'publish' | 'subscribe'], { id: operationAction, action: operationAction, pointer: `${this._meta.pointer}/${operationAction}` }),
this.createModel(Operation, this._json[operationAction as 'publish' | 'subscribe'], { id, action: operationAction, pointer: `${this._meta.pointer}/${operationAction}` }),
);
});
return new Operations(operations);
Expand Down
104 changes: 64 additions & 40 deletions src/models/v2/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,94 @@ import { Binding } from "./binding";
import { Channel } from "./channel";
import { ChannelParameter } from "./channel-parameter";
import { CorrelationId } from "./correlation-id";
import { Message } from "./message";
import { MessageTrait } from "./message-trait";
import { OperationTrait } from "./operation-trait";
import { Schema } from "./schema";
import { SecurityScheme } from "./security-scheme";
import { Server } from "./server";
import { ServerVariable } from "./server-variable";

import { extensions } from './mixins';

import type { BindingsInterface } from "../bindings";
import type { ComponentsInterface } from "../components";
import type { ChannelInterface } from "../channel";
import type { ChannelParameterInterface } from "../channel-parameter";
import type { CorrelationIdInterface } from "../correlation-id";
import type { ExtensionsInterface } from "../extensions";
import type { MessageInterface } from "../message";
import type { MessageTraitInterface } from "../message-trait";
import type { OperationTraitInterface } from "../operation-trait";
import type { SchemaInterface } from "../schema";
import type { SecuritySchemeInterface } from "../security-scheme";
import type { ServerInterface } from "../server";
import type { ServerVariableInterface } from "../server-variable";
import type { Constructor } from "../utils";

import type { ServersInterface } from "models/servers";
import { Servers } from "./servers";
import { Channels } from "./channels";
import type { ChannelsInterface } from "models/channels";
import type { MessagesInterface } from "models/messages";
import type { SchemasInterface } from "models/schemas";
import type { ChannelParametersInterface } from "models/channel-parameters";
import type { ServerVariablesInterface } from "models/server-variables";
import type { OperationTraitsInterface } from "models/operation-traits";
import type { SecuritySchemesInterface } from "models/security-schemes";
import { Messages } from "./messages";
import { Schemas } from "./schemas";
import { ChannelParameters } from "./channel-parameters";
import { ServerVariables } from "./server-variables";
import { OperationTraits } from "./operation-traits";
import { MessageTraits } from "./message-traits";
import type { MessageTraitsInterface } from "models/message-traits";
import { SecuritySchemes } from "./security-schemes";
import { Collection } from "models/collection";
import { CorrelationIds } from "./correlation-ids";
import { tilde } from '../../utils';
import type { OperationsInterface } from "models/operations";
import type { OperationInterface } from "models/operation";
import { Operations } from "./operations";
import { Message } from "./message";
import type { v2 } from "../../spec-types";
import { ComponentsObject } from "spec-types/v2";

export class Components extends BaseModel<v2.ComponentsObject> implements ComponentsInterface {
servers(): Record<string, ServerInterface> {
return this.createMap('servers', Server);
servers(): ServersInterface {
return this.createCollection('servers', Servers, Server);
}

channels(): ChannelsInterface {
return new Channels(
Object.entries(this._json.channels || {}).map(([channelAddress, channel]) =>
this.createModel(Channel, channel, { id: channelAddress, pointer: `/components/channels/${tilde(channelAddress)}` })
)
);
}

channels(): Record<string, ChannelInterface> {
return this.createMap('channels', Channel);
messages(): MessagesInterface {
return this.createCollection('messages', Messages, Message);
}

messages(): Record<string, MessageInterface> {
return this.createMap('messages', Message);
schemas(): SchemasInterface {
return this.createCollection('schemas', Schemas, Schema);
}

schemas(): Record<string, SchemaInterface> {
return this.createMap('schemas', Schema);
channelParameters(): ChannelParametersInterface {
return this.createCollection('parameters', ChannelParameters, ChannelParameter);
}

channelParameters(): Record<string, ChannelParameterInterface> {
return this.createMap('parameters', ChannelParameter);
serverVariables(): ServerVariablesInterface {
return this.createCollection('serverVariables', ServerVariables, ServerVariable);
}

serverVariables(): Record<string, ServerVariableInterface> {
return this.createMap('serverVariables', ServerVariable);
operations(): OperationsInterface {
const operations: OperationInterface[] = [];
this.channels().forEach(channel => operations.push(...channel.operations().all()));
return new Operations(operations);
}

operationTraits(): Record<string, OperationTraitInterface> {
return this.createMap('operationTraits', OperationTrait);
operationTraits(): OperationTraitsInterface {
return this.createCollection('operationTraits', OperationTraits, OperationTrait);
}

messageTraits(): Record<string, MessageTraitInterface> {
return this.createMap('messageTraits', MessageTrait);
messageTraits(): MessageTraitsInterface {
return this.createCollection('messageTraits', MessageTraits, MessageTrait);
}

correlationIds(): Record<string, CorrelationIdInterface> {
return this.createMap('correlationIds', CorrelationId);
correlationIds(): CorrelationIds {
return this.createCollection('correlationIds', CorrelationIds, CorrelationId);
}

securitySchemes(): Record<string, SecuritySchemeInterface> {
return this.createMap('securitySchemes', SecurityScheme);
securitySchemes(): SecuritySchemesInterface {
return this.createCollection('securitySchemes', SecuritySchemes, SecurityScheme);
}

serverBindings(): Record<string, BindingsInterface> {
Expand All @@ -92,11 +114,13 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
return extensions(this);
}

protected createMap<M extends BaseModel>(itemsName: keyof v2.ComponentsObject, model: Constructor<M>): Record<string, M> {
return Object.entries(this._json[itemsName] || {}).reduce((items, [itemName, item]) => {
items[itemName] = this.createModel(model, item, { id: itemName, pointer: `/components/${itemsName}/${itemName}` })
return items;
}, {} as Record<string, M>);
protected createCollection<M extends Collection<any>, T extends BaseModel>(itemsName: keyof ComponentsObject, collectionModel: Constructor<M>, itemModel: Constructor<T>): M {
const collectionItems: T[] = [];
Object.entries(this._json[itemsName] || {}).forEach(([itemName, item]) => {
collectionItems.push(this.createModel(itemModel, item, { id: itemName, pointer: `/components/${itemsName}/${itemName}` }))
});

return new collectionModel(collectionItems);
}

protected createBindings(itemsName: 'serverBindings' | 'channelBindings' | 'operationBindings' | 'messageBindings'): Record<string, BindingsInterface> {
Expand All @@ -107,6 +131,6 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
)
);
return bindings;
}, {} as Record<string, BindingsInterface>);
}, {} as Record<string, Bindings>);
}
}
14 changes: 14 additions & 0 deletions src/models/v2/correlation-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Collection } from '../collection';

import type { CorrelationIdsInterface } from '../correlation-ids';
import type { CorrelationIdInterface } from '../correlation-id';

export class CorrelationIds extends Collection<CorrelationIdInterface> implements CorrelationIdsInterface {
override get(id: string): CorrelationIdInterface | undefined {
return this.collections.find(correlationId => correlationId.meta()["key"] === id);
}

override has(id: string): boolean {
return this.collections.some(correlationId => correlationId.meta()["key"] === id);
}
}
Loading

0 comments on commit b5ce5d9

Please sign in to comment.