Skip to content

Commit

Permalink
Merge pull request #46 from RobertDiebels/issue-43
Browse files Browse the repository at this point in the history
fix(comma-placement): [ISSUE-43] - Resolves issue #43
  • Loading branch information
rmelian authored Mar 10, 2019
2 parents 41294a6 + 1455725 commit 8d29981
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
8 changes: 8 additions & 0 deletions lib/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ module.exports = (asyncapi) => {
});
}

if(asyncapi.topics){
asyncapi.subscribeTopics = {};
asyncapi.publishTopics = {};
}

_.each(asyncapi.topics, (topic, topicName) => {
const separator = asyncapi['x-topic-separator'] || '.';
const baseTopic = asyncapi.baseTopic.trim();
Expand All @@ -123,11 +128,14 @@ module.exports = (asyncapi) => {
if (topic.publish) {
beautifyMessage(topic.publish);
if (topic.publish.oneOf) _.each(topic.publish.oneOf, beautifyMessage);
asyncapi.publishTopics[newTopicName] = topic;

}

if (topic.subscribe) {
beautifyMessage(topic.subscribe);
if (topic.subscribe.oneOf) _.each(topic.subscribe.oneOf, beautifyMessage);
asyncapi.subscribeTopics[newTopicName] = topic;
}

if (topic.parameters) {
Expand Down
24 changes: 6 additions & 18 deletions templates/java-spring/.partials/AmqpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,15 @@ public class Config {
@Value("${amqp.broker.password}")
private String password;

{{#each asyncapi.topics as |topic key|}}
{{#if topic.publish}}
{{#each asyncapi.publishTopics as |topic key|}}
@Value("${amqp.exchange.{{~topic.x-service-name~}}}")
private String {{topic.x-service-name}}Exchange;

{{/if}}
{{/each}}
{{#each asyncapi.topics as |topic key|}}
{{#if topic.subscribe}}
{{#each asyncapi.subscribeTopics as |topic key|}}
@Value("${amqp.queue.{{~topic.x-service-name~}}}")
private String {{topic.x-service-name}}Queue;

{{/if}}
{{/each}}

@Bean
Expand All @@ -65,21 +61,17 @@ public AmqpAdmin amqpAdmin() {
@Bean
public Declarables exchanges() {
return new Declarables(
{{#each asyncapi.topics as |topic key|}}
{{#if topic.publish}}
{{#each asyncapi.publishTopics as |topic key|}}
new TopicExchange({{topic.x-service-name}}Exchange, true, false){{#unless @last}},{{/unless}}
{{/if}}
{{/each}}
);
}

@Bean
public Declarables queues() {
return new Declarables(
{{#each asyncapi.topics as |topic key|}}
{{#if topic.subscribe}}
{{#each asyncapi.subscribeTopics as |topic key|}}
new Queue({{topic.x-service-name}}Queue, true, false, false){{#unless @last}},{{/unless}}
{{/if}}
{{/each}}
);
}
Expand All @@ -88,16 +80,14 @@ public Declarables queues() {

@Autowired
MessageHandlerService messageHandlerService;
{{#each asyncapi.topics as |topic key|}}
{{#each asyncapi.subscribeTopics as |topic key|}}

{{#if topic.subscribe}}
@Bean
public IntegrationFlow {{camelCase topic.x-service-name}}Flow() {
return IntegrationFlows.from(Amqp.inboundGateway(connectionFactory(), {{topic.x-service-name}}Queue))
.handle(messageHandlerService::handle{{upperFirst topic.x-service-name}})
.get();
}
{{/if}}
{{/each}}

// publisher
Expand All @@ -107,9 +97,8 @@ public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
}
{{#each asyncapi.topics as |topic key|}}
{{#each asyncapi.publishTopics as |topic key|}}

{{#if topic.publish}}
@Bean
public MessageChannel {{camelCase topic.x-service-name}}OutboundChannel() {
return new DirectChannel();
Expand All @@ -123,6 +112,5 @@ public RabbitTemplate rabbitTemplate() {
outbound.setRoutingKey("#");
return outbound;
}
{{/if}}
{{/each}}
}
8 changes: 2 additions & 6 deletions templates/java-spring/.partials/MqttConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ public MqttPahoClientFactory mqttClientFactory() {

@Autowired
MessageHandlerService messageHandlerService;
{{#each asyncapi.topics as |topic key|}}
{{#each asyncapi.subscribeTopics as |topic key|}}

{{#if topic.subscribe}}
@Bean
public IntegrationFlow {{camelCase topic.x-service-name}}Flow() {
return IntegrationFlows.from({{camelCase topic.x-service-name}}Inbound())
Expand All @@ -78,13 +77,11 @@ public MqttPahoClientFactory mqttClientFactory() {
adapter.setConverter(new DefaultPahoMessageConverter());
return adapter;
}
{{/if}}
{{/each}}

// publisher
{{#each asyncapi.topics as |topic key|}}
{{#each asyncapi.publishTopics as |topic key|}}

{{#if topic.publish}}
@Bean
public MessageChannel {{camelCase topic.x-service-name}}OutboundChannel() {
return new DirectChannel();
Expand All @@ -98,7 +95,6 @@ public MqttPahoClientFactory mqttClientFactory() {
pahoMessageHandler.setDefaultTopic({{topic.x-service-name}}Topic);
return pahoMessageHandler;
}
{{/if}}
{{/each}}

}
4 changes: 4 additions & 0 deletions test/docs/sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ security:

topics:
event.{streetlightId}.lighting.measured:
x-service-name: measures
parameters:
- name: streetlightId
description: The ID of the streetlight.
Expand All @@ -41,14 +42,17 @@ topics:
$ref: '#/components/messages/lightMeasured'

action.{streetlightId}.turn.on:
x-service-name: lightTurnOn
subscribe:
$ref: '#/components/messages/turnOnOff'

action.{streetlightId}.turn.off:
x-service-name: lightTurnOff
subscribe:
$ref: '#/components/messages/turnOnOff'

action.{streetlightId}.dim:
x-service-name: dimming
subscribe:
$ref: '#/components/messages/dimLight'

Expand Down

0 comments on commit 8d29981

Please sign in to comment.