Skip to content

Commit d3a6c0e

Browse files
feat: add a comment to methods listing which message types they use. (#163)
Co-authored-by: Michael Davis <[email protected]>
1 parent f1cb7ab commit d3a6c0e

File tree

5 files changed

+150
-23
lines changed

5 files changed

+150
-23
lines changed

filters/all.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ function getFunctionSpecs(asyncapi, params) {
730730
functionSpec.sendMethodName = getSendFunctionName(channelName, publish);
731731
functionSpec.dynamicType = params.dynamicType;
732732
functionSpec.parametersToHeaders = params.parametersToHeaders;
733+
functionSpec.multipleMessageComment = getMultipleMessageComment(publish);
733734
functionMap.set(name, functionSpec);
734735
}
735736
const payload = getPayloadClass(publish);
@@ -766,7 +767,6 @@ function getFunctionSpecs(asyncapi, params) {
766767
if (!foundIt) {
767768
debugFunction(`Adding new sub ${sub}`);
768769
functionSpec.additionalSubscriptions.push(sub);
769-
770770
functionSpec.multipleMessages = true;
771771
}
772772
}
@@ -795,13 +795,14 @@ function getFunctionSpecs(asyncapi, params) {
795795
functionSpec.channelInfo = channelInfo;
796796
functionSpec.dynamicType = params.dynamicType;
797797
functionSpec.parametersToHeaders = params.parametersToHeaders;
798-
functionMap.set(name, functionSpec);
798+
functionSpec.multipleMessageComment = getMultipleMessageComment(subscribe);
799799
if (smfBinding && smfBinding.queueName && smfBinding.topicSubscriptions) {
800800
debugFunction(`A new one with subscriptions: ${smfBinding.topicSubscriptions}`);
801801
functionSpec.additionalSubscriptions = smfBinding.topicSubscriptions;
802802
functionSpec.isQueueWithSubscription = true;
803803
functionSpec.multipleMessages = smfBinding.topicSubscriptions && smfBinding.topicSubscriptions.length > 1;
804804
}
805+
functionMap.set(name, functionSpec);
805806
}
806807

807808
if (functionSpec.multipleMessages) {
@@ -836,6 +837,26 @@ function getFunctionSpecs(asyncapi, params) {
836837
return functionMap;
837838
}
838839

840+
function getSendFunctionName(channelName, operation) {
841+
return `send${_.upperFirst(getFunctionName(channelName, operation, undefined))}`;
842+
}
843+
844+
function getMultipleMessageComment(pubOrSub) {
845+
let ret;
846+
847+
// We deliberately leave out the last newline, because that makes it easier to use in the template.
848+
// Otherwise it's really hard to get rid of an extra unwanted newline.
849+
if (pubOrSub.hasMultipleMessages()) {
850+
ret = '// The message can be of type:';
851+
pubOrSub.messages().forEach(m => {
852+
ret += '\n\t// ';
853+
ret += getMessagePayloadType(m);
854+
});
855+
}
856+
857+
return ret;
858+
}
859+
839860
function getPayloadClass(pubOrSub) {
840861
let ret;
841862

@@ -845,32 +866,34 @@ function getPayloadClass(pubOrSub) {
845866
} else {
846867
const message = pubOrSub.message();
847868
if (message) {
848-
const payload = message.payload();
849-
debugPayload('payload:');
850-
debugPayload(payload);
851-
852-
if (payload) {
853-
const type = payload.type();
854-
debugPayload('type:');
855-
debugPayload(type);
856-
857-
if (!type || type === 'object') {
858-
ret = payload.ext('x-parser-schema-id');
859-
ret = _.camelCase(ret);
860-
ret = _.upperFirst(ret);
861-
} else {
862-
ret = getType(type, payload.format()).javaType;
863-
}
864-
}
869+
ret = getMessagePayloadType(message);
865870
}
866871
}
867872
debugPayload(`getPayloadClass: ${ret}`);
868873

869874
return ret;
870875
}
871876

872-
function getSendFunctionName(channelName, operation) {
873-
return `send${_.upperFirst(getFunctionName(channelName, operation, undefined))}`;
877+
function getMessagePayloadType(message) {
878+
let ret;
879+
const payload = message.payload();
880+
debugPayload('payload:');
881+
debugPayload(payload);
882+
883+
if (payload) {
884+
const type = payload.type();
885+
debugPayload('type:');
886+
debugPayload(type);
887+
888+
if (!type || type === 'object') {
889+
ret = payload.ext('x-parser-schema-id');
890+
ret = _.camelCase(ret);
891+
ret = _.upperFirst(ret);
892+
} else {
893+
ret = getType(type, payload.format()).javaType;
894+
}
895+
}
896+
return ret;
874897
}
875898

876899
// This returns the connection properties for a solace binder, for application.yaml.

template/src/main/java/Application.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public static void main(String[] args) {
9999
}
100100
{%- endif %}
101101
{%- elif funcSpec.type === 'consumer' %}
102+
{%- if funcSpec.multipleMessageComment %}
103+
{{ funcSpec.multipleMessageComment }}
104+
{%- endif %}
102105
@Bean
103106
{{ funcSpec.functionSignature | safe }} {
104107
return data -> {
@@ -108,7 +111,7 @@ public static void main(String[] args) {
108111
}
109112
{%- else %}{#- it is a supplier. #}
110113
{%- if funcSpec.dynamic %}
111-
{%- if params.dynamicType === 'header' %}
114+
{%- if params.dynamicType === 'header' -%}
112115
@Bean
113116
{{ funcSpec.functionSignature | safe }} {
114117
return () -> {
@@ -130,6 +133,9 @@ public static void main(String[] args) {
130133
{# else do nothing, we just use the void function below. #}
131134
{%- endif %}{# dynamic type #}
132135
{%- else -%}{# it is not dynamic. #}
136+
{%- if funcSpec.multipleMessageComment %}
137+
{{ funcSpec.multipleMessageComment }}
138+
{%- endif %}
133139
@Bean
134140
{{ funcSpec.functionSignature | safe }} {
135141
return () -> {

test/__snapshots__/integration.test.js.snap

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`template integration tests using the generator should generate a comment for a consumer receiving multiple messages 1`] = `
4+
"
5+
import java.util.function.Consumer;
6+
import java.util.function.Supplier;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.boot.SpringApplication;
11+
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.messaging.Message;
14+
15+
@SpringBootApplication
16+
public class Application {
17+
18+
private static final Logger logger = LoggerFactory.getLogger(Application.class);
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(Application.class);
22+
}
23+
24+
// The message can be of type:
25+
// Cat
26+
// Dog
27+
@Bean
28+
public Supplier<Message<?>> animalsSupplier() {
29+
return () -> {
30+
// Add business logic here.
31+
return new Message<?>();
32+
};
33+
}
34+
35+
// The message can be of type:
36+
// Cat
37+
// Dog
38+
@Bean
39+
public Consumer<Message<?>> animalsConsumer() {
40+
return data -> {
41+
// Add business logic here.
42+
logger.info(data.toString());
43+
};
44+
}
45+
46+
}
47+
"
48+
`;
49+
350
exports[`template integration tests using the generator should generate application files using the solace binder 1`] = `
451
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
552
<project xmlns=\\"http://maven.apache.org/POM/4.0.0\\" xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\" xsi:schemaLocation=\\"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd\\">

test/integration.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ describe('template integration tests using the generator', () => {
8585
expect(file).toMatchSnapshot();
8686
}
8787
});
88-
});
88+
89+
it('should generate a comment for a consumer receiving multiple messages', async () => {
90+
const OUTPUT_DIR = generateFolderName();
91+
92+
const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true });
93+
await generator.generateFromFile(path.resolve('test', 'mocks/animals.yaml'));
94+
95+
const expectedFiles = [
96+
'src/main/java/Application.java'
97+
];
98+
for (const index in expectedFiles) {
99+
const file = await readFile(path.join(OUTPUT_DIR, expectedFiles[index]), 'utf8');
100+
expect(file).toMatchSnapshot();
101+
}
102+
});
103+
});

test/mocks/animals.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
components:
2+
schemas:
3+
Dog:
4+
type: object
5+
properties:
6+
name:
7+
type: string
8+
Cat:
9+
type: object
10+
properties:
11+
name:
12+
type: string
13+
messages:
14+
DogMessage:
15+
payload:
16+
$ref: '#/components/schemas/Dog'
17+
CatMessage:
18+
payload:
19+
$ref: '#/components/schemas/Cat'
20+
channels:
21+
'animals':
22+
publish:
23+
message:
24+
oneOf:
25+
- $ref: '#/components/messages/CatMessage'
26+
- $ref: '#/components/messages/DogMessage'
27+
subscribe:
28+
message:
29+
oneOf:
30+
- $ref: '#/components/messages/CatMessage'
31+
- $ref: '#/components/messages/DogMessage'
32+
asyncapi: 2.0.0
33+
info:
34+
description: Testing oneOf
35+
title: animals
36+
version: 0.0.1

0 commit comments

Comments
 (0)