Skip to content

Commit

Permalink
fix: implement uuid format for string (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: Dale Lane <[email protected]>
  • Loading branch information
dan-r and dalelane authored Nov 2, 2022
1 parent b2bb6c5 commit f71b2e7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 34 deletions.
1 change: 1 addition & 0 deletions components/Demo/DemoProducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function DemoProducer({ params, messageName, className, constructorArgs }
import ${params.package}.${className}Producer;
import ${params.package}.ConnectionHelper;
import ${params.package}.models.${messageName};
import java.util.UUID;
public class DemoProducer {
public static void main(String[] args) {
Expand Down
1 change: 1 addition & 0 deletions components/Files/Models.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function Models(asyncapi, params) {
<File name={`${packagePath}models/${messageNameUpperCase}.java`}>
<PackageDeclaration path={`${params.package}.models`} />
<ImportDeclaration path={`${params.package}.models.ModelContract`} />
<ImportDeclaration path={`java.util.UUID`} />

<Class name={messageNameUpperCase} extendsClass="ModelContract">
<Indent size={2} type={IndentationTypes.SPACES}>
Expand Down
1 change: 1 addition & 0 deletions components/Producer/KafkaProducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function ProducerImports({ params }) {
return `
import java.util.logging.*;
import java.io.Serializable;
import java.util.UUID;
import ${params.package}.ConnectionHelper;
import ${params.package}.LoggingHelper;
Expand Down
1 change: 1 addition & 0 deletions components/Producer/MQProducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function ProducerImports({ params }) {
return `
import java.util.logging.*;
import java.io.Serializable;
import java.util.UUID;
import javax.jms.Destination;
import javax.jms.JMSProducer;
Expand Down
1 change: 1 addition & 0 deletions test/Producer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ test('Generates all imports from path', async() => {
expect(testProducer.ProducerImports({asyncapi: generator.asyncapi, params: generator.templateParams})).toBe(`
import java.util.logging.*;
import java.io.Serializable;
import java.util.UUID;
import javax.jms.Destination;
import javax.jms.JMSProducer;
Expand Down
10 changes: 7 additions & 3 deletions test/Types.utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ test('Check string type is converted to String', () => {
expect(typesUtils.asyncApiToJavaType('string')).toBe('String');
});

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

test('Check uuid format is converted to UUID', () => {
expect(typesUtils.asyncApiToJavaType('string', 'uuid')).toBe('UUID');
});

test('Check byte type is not changed', () => {
expect(typesUtils.asyncApiToJavaType('string', 'byte')).toBe('byte');
});
Expand Down Expand Up @@ -110,9 +114,9 @@ test('Create Java Args from Properties', async () => {

// Test function asyncApiTypeToDemoValue
test('Check integer type is a random number', () => {
expect(typesUtils.asyncApiTypeToDemoValue('integer')).toEqual(expect.any(Number));
expect(typesUtils.asyncApiToDemoValue('integer')).toEqual(expect.any(Number));
});

test('Unexpected type throws an error', () => {
expect(() => { typesUtils.asyncApiTypeToDemoValue('test');}).toThrow();
expect(() => { typesUtils.asyncApiToDemoValue('test');}).toThrow();
});
67 changes: 36 additions & 31 deletions utils/Types.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,40 @@ export function asyncApiToJavaType(type, format) {

function asyncApiFormatToJavaType(format) {
switch (format) {
case 'int32':
return 'int';
case 'int64':
return 'long';
case 'float':
return 'float';
case 'double':
return 'double';
case 'byte':
return 'byte';
case 'binary':
return 'String';
case 'date':
return 'String';
case 'date-time':
return 'String';
case 'password':
return 'String';
case 'int32':
return 'int';
case 'int64':
return 'long';
case 'float':
return 'float';
case 'double':
return 'double';
case 'byte':
return 'byte';
case 'binary':
return 'String';
case 'date':
return 'String';
case 'date-time':
return 'String';
case 'password':
return 'String';
case 'uuid':
return 'UUID';
}
}
function asyncApiTypeToJavaType(type) {
switch (type) {
case 'integer':
return 'int';
case 'number':
// using double by default, as no format
// was specified
return 'double';
case 'string':
return 'String';
case 'boolean':
return 'boolean';
case 'integer':
return 'int';
case 'number':
// using double by default, as no format
// was specified
return 'double';
case 'string':
return 'String';
case 'boolean':
return 'boolean';
}
}

Expand Down Expand Up @@ -115,25 +117,28 @@ export function createJavaArgsFromProperties(properties) {
*/
export function createJavaConstructorArgs(properties) {
return Object.entries(properties).map(([name, property]) => {
return `${asyncApiTypeToDemoValue(property.type())}`;
return `${asyncApiToDemoValue(property.type(), property.format())}`;
});
}

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

switch (asyncApiType) {
switch (type) {
case ('integer' || 'long'):
return parseInt(Math.random() * 1000, 10);

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

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

case 'byte':
Expand Down

0 comments on commit f71b2e7

Please sign in to comment.