Skip to content

Commit

Permalink
fix: removed generation of queue additional subscriptions when a topi…
Browse files Browse the repository at this point in the history
…c has parameters. (#115)

* Replaced console.log with debug commands.

* We no longer generate queue additional subscriptions for topics with parameters.

* Changed double quotes to single quotes to pass the linter.

* Fixed linter issues.

Co-authored-by: Michael Davis <[email protected]>
  • Loading branch information
damaru-inc and MichaelDavisSolace authored Jun 8, 2021
1 parent 3ca53e4 commit 66b57cb
Showing 1 changed file with 60 additions and 46 deletions.
106 changes: 60 additions & 46 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ const yaml = require('js-yaml');
const _ = require('lodash');
const ScsLib = require('../lib/scsLib.js');
const scsLib = new ScsLib();

// To enable debug logging, set the env var DEBUG="type function" with whatever things you want to see.
const debugFunction = require('debug')('function');
const debugPayload = require('debug')('payload');
const debugProperty = require('debug')('property');
const debugTopic = require('debug')('topic');
const debugType = require('debug')('type');

// Library versions
const SOLACE_SPRING_CLOUD_VERSION = '1.1.1';
const SPRING_BOOT_VERSION = '2.3.2.RELEASE';
Expand Down Expand Up @@ -152,7 +158,6 @@ function appProperties([asyncapi, params]) {
}
}
const ym = yaml.safeDump(doc, { lineWidth: 200 });
//console.log(ym);
return ym;
}
filter.appProperties = appProperties;
Expand Down Expand Up @@ -186,13 +191,14 @@ function appExtraIncludes(asyncapi) {
filter.appExtraIncludes = appExtraIncludes;

function schemaExtraIncludes([schemaName, schema]) {
//console.log("checkPropertyNames " + schemaName + " " + schema.type());
debugProperty(`schemaExtraIncludes ${schemaName} ${schema.type()}`);

const ret = {};
if (checkPropertyNames(schemaName, schema)) {
ret.needJsonPropertyInclude = true;
}
//console.log("checkPropertyNames:");
//console.log(ret);
debugProperty('checkPropertyNames:');
debugProperty(ret);
return ret;
}
filter.schemaExtraIncludes = schemaExtraIncludes;
Expand Down Expand Up @@ -225,29 +231,30 @@ function indent3(numTabs) {
filter.indent3 = indent3;
// This returns the proper Java type for a schema property.
function fixType([name, javaName, property]) {
//console.log('fixType: ' + name + " " + dump(property));
debugType(`fixType: ${name}`);
debugType(property);

let isArrayOfObjects = false;

// For message headers, type is a property.
// For schema properties, type is a function.
let type = property.type;
let format = property.format;
//console.log("fixType: " + property);
debugType(`fixType: ${property}`);

if (typeof type === 'function') {
type = property.type();
format = property.format();
}

//console.log(`fixType: type: ${type} javaNamne ${javaName}` );
//console.log(property);
debugType(`fixType: type: ${type} javaName ${javaName}`);
debugType(property);
// If a schema has a property that is a ref to another schema,
// the type is undefined, and the title gives the title of the referenced schema.
let typeName;
if (type === undefined) {
if (property.enum()) {
//console.log("It's an enum.");
debugType('It is an enum.');
typeName = _.upperFirst(javaName);
} else {
// check to see if it's a ref to another schema.
Expand Down Expand Up @@ -281,7 +288,7 @@ function fixType([name, javaName, property]) {
} else if (type === 'object') {
typeName = _.upperFirst(javaName);
} else if (property.enum()) {
//console.log("It's an enum.");
debugType('It is an enum.');
typeName = _.upperFirst(javaName);
} else {
typeName = getType(type,format).javaType;
Expand Down Expand Up @@ -408,42 +415,42 @@ filter.topicInfo = topicInfo;
function checkPropertyNames(name, schema) {
const ret = false;

//console.log(JSON.stringify(schema));
//console.log('checkPropertyNames: checking schema ' + name + getMethods(schema));
debugProperty(`checkPropertyNames: checking schema ${name}`);
debugProperty(schema);

let properties = schema.properties();

if (schema.type() === 'array') {
properties = schema.items().properties();
}

//console.log("schema type: " + schema.type());
debugProperty(`schema type : ${schema.type()}`);

for (const propName in properties) {
const javaName = _.camelCase(propName);
const prop = properties[propName];
//console.log('checking ' + propName + ' ' + prop.type());
debugProperty(`checking ${propName} ${prop.type()}`);

if (javaName !== propName) {
//console.log("Java name " + javaName + " is different from " + propName);
debugProperty(`Java name ${javaName} is different from ${propName}`);
return true;
}
if (prop.type() === 'object') {
//console.log("Recursing into object");
debugProperty('Recursing into object');
const check = checkPropertyNames(propName, prop);
if (check) {
return true;
}
} else if (prop.type() === 'array') {
//console.log('checkPropertyNames: ' + JSON.stringify(prop));
debugProperty(`checkPropertyNames: ${prop}`);
if (!prop.items) {
throw new Error(`Array named ${ propName } must have an 'items' property to indicate what type the array elements are.`);
}
const itemsType = prop.items().type();
//console.log('checkPropertyNames: ' + JSON.stringify(prop.items));
//console.log('array of : ' + itemsType);
debugProperty(`checkPropertyNames: ${prop.items}`);
debugProperty(`array of ${itemsType}`);
if (itemsType === 'object') {
//console.log("Recursing into array");
debugProperty('Recursing into array');
const check = checkPropertyNames(propName, prop.items());
if (check) {
return true;
Expand Down Expand Up @@ -475,12 +482,12 @@ function getAdditionalSubs(asyncapi, params) {
const functionName = getFunctionName(channelName, subscribe, true);
const topicInfo = getTopicInfo(channelName, channel);
const queue = subscribe.ext('x-scs-destination');
if (topicInfo.hasParams || queue) {
if (queue) {
if (!ret) {
ret = {};
ret.bindings = {};
}
const bindingName = `${functionName }-in-0`;
const bindingName = `${functionName}-in-0`;
ret.bindings[bindingName] = {};
ret.bindings[bindingName].consumer = {};
ret.bindings[bindingName].consumer.queueAdditionalSubscriptions = topicInfo.subscribeTopic;
Expand Down Expand Up @@ -515,10 +522,10 @@ function getBindings(asyncapi, params) {
// This returns the base function name that SCSt will use to map functions with bindings.
function getFunctionName(channelName, operation, isSubscriber) {
let ret;
//console.log('getFunctionName operation: ' + JSON.stringify(operation));
//console.log(operation);
debugFunction(`getFunctionName operation: ${operation}`);
//debugFunction(operation);
let functionName = operation.ext('x-scs-function-name');
//console.log(getMethods(operation));
//debugFunction(getMethods(operation));

if (!functionName) {
functionName = operation.id();
Expand All @@ -529,15 +536,15 @@ function getFunctionName(channelName, operation, isSubscriber) {
} else {
ret = _.camelCase(channelName) + (isSubscriber ? 'Consumer' : 'Supplier');
}
debugFunction(ret);
return ret;
}

// This returns the base function name that SCSt will use to map functions with bindings.
function getFunctionNameByChannel(channelName, channel) {
let ret = _.camelCase(channelName);
//console.log('functionName channel: ' + JSON.stringify(channelJson));
const functionName = channel.ext('x-scs-function-name');
//console.log('function name for channel ' + channelName + ': ' + functionName);
debugFunction(`getFunctionNameByChannel ${channel} ${functionName}`);
if (functionName) {
ret = functionName;
}
Expand All @@ -561,10 +568,10 @@ function getFunctionSpecs(asyncapi, params) {

for (const channelName in asyncapi.channels()) {
const channel = asyncapi.channels()[channelName];
//console.log("=====================================");
//console.log("channelJson: " + JSON.stringify(channel._json));
//console.log("getFunctionSpecs: " + channelName);
//console.log("=====================================");
debugFunction('=====================================');
debugFunction(`getFunctionSpecs ${channelName}`);
debugFunction(channel._json);
debugFunction('=====================================');
let functionSpec;
const publish = scsLib.getRealPublisher(info, params, channel);
if (publish) {
Expand All @@ -584,7 +591,7 @@ function getFunctionSpecs(asyncapi, params) {
}
const payload = getPayloadClass(publish);
if (!payload) {
throw new Error(`Channel ${ channelName }: no payload class has been defined.`);
throw new Error(`Channel ${channelName}: no payload class has been defined.`);
}
functionSpec.publishPayload = payload;
functionSpec.publishChannel = channelName;
Expand Down Expand Up @@ -619,9 +626,13 @@ function getFunctionSpecs(asyncapi, params) {
if (dest) {
functionSpec.subscribeChannel = dest;
} else {
functionSpec.subscribeChannel = channelName;
const topicInfo = getTopicInfo(channelName, channel);
functionSpec.subscribeChannel = topicInfo.subscribeTopic;
}
}

debugFunction('functionSpec:');
debugFunction(functionSpec);
}

return functionMap;
Expand All @@ -631,7 +642,7 @@ function getPayloadClass(pubOrSub) {
let ret;

if (pubOrSub) {
//console.log(pubOrSub);
debugPayload(pubOrSub);
if (pubOrSub.hasMultipleMessages()) {
ret = 'Message<?>';
} else {
Expand All @@ -646,7 +657,7 @@ function getPayloadClass(pubOrSub) {
}
}
}
//console.log("getPayloadClass: " + ret);
debugPayload(`getPayloadClass: ${ret}`);
}

return ret;
Expand Down Expand Up @@ -674,14 +685,16 @@ function getTopicInfo(channelName, channel) {
let sampleArgList = '';
let first = true;

//console.log("params: " + JSON.stringify(channel.parameters()));
debugTopic('params:');
debugTopic(channel.parameters());
for (const name in channel.parameters()) {
const nameWithBrackets = `{${ name }}`;
const parameter = channel.parameter(name);
const schema = parameter.schema();
const type = getType(schema.type(), schema.format());
const param = { name: _.lowerFirst(name) };
//console.log("name: " + name + " type: " + type);
debugTopic(`name: ${name} type:`);
debugTopic(type);
let sampleArg = 1;

if (first) {
Expand All @@ -694,32 +707,33 @@ function getTopicInfo(channelName, channel) {
sampleArgList += ', ';

if (type) {
//console.log("It's a type: " + type);
debugTopic('It is a type:');
debugTopic(type);
const javaType = type.javaType || typeMap.get(type);
if (!javaType) throw new Error(`topicInfo filter: type not found in typeMap: ${ type}`);
param.type = javaType;
const printfArg = type.printFormat;
//console.log("printf: " + printfArg);
debugTopic(`printf: ${printfArg}`);
if (!printfArg) throw new Error(`topicInfo filter: type not found in formatMap: ${ type}`);
//console.log("Replacing " + nameWithBrackets);
debugTopic(`Replacing ${nameWithBrackets}`);
publishTopic = publishTopic.replace(nameWithBrackets, printfArg);
sampleArg = type.sample;
} else {
const en = schema.enum();
if (en) {
//console.log("It's an enum: " + en);
debugTopic(`It is an enum: ${en}`);
param.type = _.upperFirst(name);
param.enum = en;
sampleArg = `Messaging.${ param.type }.${ en[0]}`;
//console.log("Replacing " + nameWithBrackets);
sampleArg = `Messaging.${param.type}.${en[0]}`;
debugTopic(`Replacing ${nameWithBrackets}`);
publishTopic = publishTopic.replace(nameWithBrackets, '%s');
} else {
throw new Error(`topicInfo filter: Unknown parameter type: ${ JSON.stringify(schema)}`);
}
}

subscribeTopic = subscribeTopic.replace(nameWithBrackets, '*');
functionParamList += `${param.type } ${ param.name}`;
functionParamList += `${param.type} ${param.name}`;
functionArgList += param.name;
sampleArgList += sampleArg;
params.push(param);
Expand Down

0 comments on commit 66b57cb

Please sign in to comment.