Skip to content

Commit 36a058f

Browse files
ivangsaAayushSaini101princerajpoot20
authored
feat: add traits field to channel object asyncapi/spec#1220 (#1193)
Co-authored-by: Ivan Garcia Sainz-Aja <ivangsa@gmail.com> Co-authored-by: Moderator <60972989+AayushSaini101@users.noreply.github.com> Co-authored-by: Prince Rajpoot <prince.rajpoot.20@gmail.com>
1 parent 0a143bf commit 36a058f

21 files changed

Lines changed: 341 additions & 3 deletions

packages/parser/src/custom-operations/apply-traits.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@ function applyTraitsToObjectV2(value: Record<string, unknown>) {
4949
}
5050

5151
const v3TraitPaths = [
52+
// channels
53+
'$.channels.*',
54+
'$.components.channels.*',
5255
// operations
5356
'$.operations.*',
5457
'$.operations.*.channel.messages.*',
5558
'$.operations.*.messages.*',
5659
'$.components.operations.*',
5760
'$.components.operations.*.channel.messages.*',
5861
'$.components.operations.*.messages.*',
59-
// Channels
62+
// messages inside channels
6063
'$.channels.*.messages.*',
6164
'$.components.channels.*.messages.*',
6265
// messages
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { BaseModel } from './base';
2+
import type { ChannelParametersInterface } from './channel-parameters';
3+
import type { CoreMixinInterface } from './mixins';
4+
import type { ServersInterface } from './servers';
5+
6+
export interface ChannelTraitInterface extends BaseModel, CoreMixinInterface {
7+
id(): string;
8+
servers(): ServersInterface;
9+
parameters(): ChannelParametersInterface;
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { Collection } from './collection';
2+
import type { ChannelTraitInterface } from './channel-trait';
3+
4+
export type ChannelTraitsInterface = Collection<ChannelTraitInterface>;

packages/parser/src/models/channel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BaseModel } from './base';
22
import type { ChannelParametersInterface } from './channel-parameters';
3+
import type { ChannelTraitsInterface } from './channel-traits';
34
import type { MessagesInterface } from './messages';
45
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';
56
import type { OperationsInterface } from './operations';
@@ -12,4 +13,5 @@ export interface ChannelInterface extends BaseModel, BindingsMixinInterface, Des
1213
operations(): OperationsInterface;
1314
messages(): MessagesInterface;
1415
parameters(): ChannelParametersInterface;
16+
traits(): ChannelTraitsInterface;
1517
}

packages/parser/src/models/components.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { ChannelsInterface } from './channels';
66
import type { MessagesInterface } from './messages';
77
import type { SchemasInterface } from './schemas';
88
import type { ChannelParametersInterface } from './channel-parameters';
9+
import type { ChannelTraitsInterface } from './channel-traits';
910
import type { ServerVariablesInterface } from './server-variables';
1011
import type { OperationTraitsInterface } from './operation-traits';
1112
import type { MessageTraitsInterface } from './message-traits';
@@ -21,6 +22,7 @@ export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface
2122
channelParameters(): ChannelParametersInterface;
2223
serverVariables(): ServerVariablesInterface;
2324
operations(): OperationsInterface;
25+
channelTraits(): ChannelTraitsInterface;
2426
operationTraits(): OperationTraitsInterface;
2527
messageTraits(): MessageTraitsInterface;
2628
correlationIds(): CorrelationIdsInterface;

packages/parser/src/models/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export * from './channel-parameter';
99
export * from './channel-parameters';
1010
export * from './channel';
1111
export * from './channels';
12+
export * from './channel-trait';
13+
export * from './channel-traits';
1214
export * from './collection';
1315
export * from './components';
1416
export * from './contact';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Collection } from '../collection';
2+
3+
import type { ChannelTraitsInterface } from '../channel-traits';
4+
import type { ChannelTraitInterface } from '../channel-trait';
5+
6+
export class ChannelTraits extends Collection<ChannelTraitInterface> implements ChannelTraitsInterface {
7+
override get(id: string): ChannelTraitInterface | undefined {
8+
return this.collections.find(trait => trait.id() === id);
9+
}
10+
}

packages/parser/src/models/v2/channel.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BaseModel } from '../base';
2+
import { ChannelTraits } from './channel-traits';
23
import { ChannelParameters } from './channel-parameters';
34
import { ChannelParameter } from './channel-parameter';
45
import { Messages } from './messages';
@@ -12,6 +13,7 @@ import { bindings, hasDescription, description, extensions } from './mixins';
1213
import type { BindingsInterface } from '../bindings';
1314
import type { ChannelInterface } from '../channel';
1415
import type { ChannelParametersInterface } from '../channel-parameters';
16+
import type { ChannelTraitsInterface } from '../channel-traits';
1517
import type { ExtensionsInterface } from '../extensions';
1618
import type { MessagesInterface } from '../messages';
1719
import type { MessageInterface } from '../message';
@@ -81,6 +83,10 @@ export class Channel extends BaseModel<v2.ChannelObject, { id: string, address:
8183
);
8284
}
8385

86+
traits(): ChannelTraitsInterface {
87+
return new ChannelTraits([]);
88+
}
89+
8490
bindings(): BindingsInterface {
8591
return bindings(this);
8692
}

packages/parser/src/models/v2/components.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BaseModel } from '../base';
22
import { Collection } from '../collection';
3+
import { ChannelTraits } from './channel-traits';
34

45
import { Bindings } from './bindings';
56
import { Binding } from './binding';
@@ -37,6 +38,7 @@ import type { ChannelsInterface } from '../channels';
3738
import type { MessagesInterface } from '../messages';
3839
import type { SchemasInterface } from '../schemas';
3940
import type { ChannelParametersInterface } from '../channel-parameters';
41+
import type { ChannelTraitsInterface } from '../channel-traits';
4042
import type { ServerVariablesInterface } from '../server-variables';
4143
import type { OperationTraitsInterface } from '../operation-traits';
4244
import type { SecuritySchemesInterface } from '../security-schemes';
@@ -75,6 +77,10 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
7577
return this.createCollection('parameters', ChannelParameters, ChannelParameter);
7678
}
7779

80+
channelTraits(): ChannelTraitsInterface {
81+
return new ChannelTraits([]);
82+
}
83+
7884
serverVariables(): ServerVariablesInterface {
7985
return this.createCollection('serverVariables', ServerVariables, ServerVariable);
8086
}

packages/parser/src/models/v2/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { ChannelParameter as ChannelParameterV2 } from './channel-parameter';
55
export { ChannelParameters as ChannelParametersV2 } from './channel-parameters';
66
export { Channel as ChannelV2 } from './channel';
77
export { Channels as ChannelsV2 } from './channels';
8+
export { ChannelTraits as ChannelTraitsV2 } from './channel-traits';
89
export { Components as ComponentsV2 } from './components';
910
export { Contact as ContactV2 } from './contact';
1011
export { CorrelationId as CorrelationIdV2 } from './correlation-id';
@@ -34,4 +35,4 @@ export { ServerVariables as ServerVariablesV2 } from './server-variables';
3435
export { Server as ServerV2 } from './server';
3536
export { Servers as ServersV2 } from './servers';
3637
export { Tag as TagV2 } from './tag';
37-
export { Tags as TagsV2 } from './tags';
38+
export { Tags as TagsV2 } from './tags';

0 commit comments

Comments
 (0)