Skip to content

Commit f71b2e7

Browse files
dan-rdalelane
andauthored
fix: implement uuid format for string (#72)
Co-authored-by: Dale Lane <dale.lane@uk.ibm.com>
1 parent b2bb6c5 commit f71b2e7

File tree

7 files changed

+48
-34
lines changed

7 files changed

+48
-34
lines changed

components/Demo/DemoProducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function DemoProducer({ params, messageName, className, constructorArgs }
2424
import ${params.package}.${className}Producer;
2525
import ${params.package}.ConnectionHelper;
2626
import ${params.package}.models.${messageName};
27+
import java.util.UUID;
2728
2829
public class DemoProducer {
2930
public static void main(String[] args) {

components/Files/Models.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function Models(asyncapi, params) {
3232
<File name={`${packagePath}models/${messageNameUpperCase}.java`}>
3333
<PackageDeclaration path={`${params.package}.models`} />
3434
<ImportDeclaration path={`${params.package}.models.ModelContract`} />
35+
<ImportDeclaration path={`java.util.UUID`} />
3536

3637
<Class name={messageNameUpperCase} extendsClass="ModelContract">
3738
<Indent size={2} type={IndentationTypes.SPACES}>

components/Producer/KafkaProducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function ProducerImports({ params }) {
6767
return `
6868
import java.util.logging.*;
6969
import java.io.Serializable;
70+
import java.util.UUID;
7071
7172
import ${params.package}.ConnectionHelper;
7273
import ${params.package}.LoggingHelper;

components/Producer/MQProducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function ProducerImports({ params }) {
6666
return `
6767
import java.util.logging.*;
6868
import java.io.Serializable;
69+
import java.util.UUID;
6970
7071
import javax.jms.Destination;
7172
import javax.jms.JMSProducer;

test/Producer.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ test('Generates all imports from path', async() => {
5959
expect(testProducer.ProducerImports({asyncapi: generator.asyncapi, params: generator.templateParams})).toBe(`
6060
import java.util.logging.*;
6161
import java.io.Serializable;
62+
import java.util.UUID;
6263
6364
import javax.jms.Destination;
6465
import javax.jms.JMSProducer;

test/Types.utils.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ test('Check string type is converted to String', () => {
1818
expect(typesUtils.asyncApiToJavaType('string')).toBe('String');
1919
});
2020

21-
test('Check password type is converted to String', () => {
21+
test('Check password format is converted to String', () => {
2222
expect(typesUtils.asyncApiToJavaType('string', 'password')).toBe('String');
2323
});
2424

25+
test('Check uuid format is converted to UUID', () => {
26+
expect(typesUtils.asyncApiToJavaType('string', 'uuid')).toBe('UUID');
27+
});
28+
2529
test('Check byte type is not changed', () => {
2630
expect(typesUtils.asyncApiToJavaType('string', 'byte')).toBe('byte');
2731
});
@@ -110,9 +114,9 @@ test('Create Java Args from Properties', async () => {
110114

111115
// Test function asyncApiTypeToDemoValue
112116
test('Check integer type is a random number', () => {
113-
expect(typesUtils.asyncApiTypeToDemoValue('integer')).toEqual(expect.any(Number));
117+
expect(typesUtils.asyncApiToDemoValue('integer')).toEqual(expect.any(Number));
114118
});
115119

116120
test('Unexpected type throws an error', () => {
117-
expect(() => { typesUtils.asyncApiTypeToDemoValue('test');}).toThrow();
121+
expect(() => { typesUtils.asyncApiToDemoValue('test');}).toThrow();
118122
});

utils/Types.utils.js

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,40 @@ export function asyncApiToJavaType(type, format) {
3737

3838
function asyncApiFormatToJavaType(format) {
3939
switch (format) {
40-
case 'int32':
41-
return 'int';
42-
case 'int64':
43-
return 'long';
44-
case 'float':
45-
return 'float';
46-
case 'double':
47-
return 'double';
48-
case 'byte':
49-
return 'byte';
50-
case 'binary':
51-
return 'String';
52-
case 'date':
53-
return 'String';
54-
case 'date-time':
55-
return 'String';
56-
case 'password':
57-
return 'String';
40+
case 'int32':
41+
return 'int';
42+
case 'int64':
43+
return 'long';
44+
case 'float':
45+
return 'float';
46+
case 'double':
47+
return 'double';
48+
case 'byte':
49+
return 'byte';
50+
case 'binary':
51+
return 'String';
52+
case 'date':
53+
return 'String';
54+
case 'date-time':
55+
return 'String';
56+
case 'password':
57+
return 'String';
58+
case 'uuid':
59+
return 'UUID';
5860
}
5961
}
6062
function asyncApiTypeToJavaType(type) {
6163
switch (type) {
62-
case 'integer':
63-
return 'int';
64-
case 'number':
65-
// using double by default, as no format
66-
// was specified
67-
return 'double';
68-
case 'string':
69-
return 'String';
70-
case 'boolean':
71-
return 'boolean';
64+
case 'integer':
65+
return 'int';
66+
case 'number':
67+
// using double by default, as no format
68+
// was specified
69+
return 'double';
70+
case 'string':
71+
return 'String';
72+
case 'boolean':
73+
return 'boolean';
7274
}
7375
}
7476

@@ -115,25 +117,28 @@ export function createJavaArgsFromProperties(properties) {
115117
*/
116118
export function createJavaConstructorArgs(properties) {
117119
return Object.entries(properties).map(([name, property]) => {
118-
return `${asyncApiTypeToDemoValue(property.type())}`;
120+
return `${asyncApiToDemoValue(property.type(), property.format())}`;
119121
});
120122
}
121123

122124
/*
123125
* Generates an example value from asyncAPI datatype in Java
124126
*/
125-
export function asyncApiTypeToDemoValue(asyncApiType) {
127+
export function asyncApiToDemoValue(type, format) {
126128
const strWords = ['ASyncAPI', 'Java', 'React', 'Hackathon', 'Community', 'Open Source', 'Publish', 'Subscribe', 'Topic', 'Demo', 'Example', 'Template', 'Producer', 'Consumer', 'Generator', 'Message', 'Endpoint'];
127129
const boolWords = ['true', 'false'];
128130

129-
switch (asyncApiType) {
131+
switch (type) {
130132
case ('integer' || 'long'):
131133
return parseInt(Math.random() * 1000, 10);
132134

133135
case ('float' || 'double'):
134136
return Math.random();
135137

136138
case ('string' || 'binary' || 'password'):
139+
if (format === 'uuid') {
140+
return 'UUID.randomUUID()';
141+
}
137142
return `"${ strWords[Math.floor(Math.random()*strWords.length)]}"`;
138143

139144
case 'byte':

0 commit comments

Comments
 (0)