@@ -3,7 +3,13 @@ const yaml = require('js-yaml');
33const _ = require ( 'lodash' ) ;
44const ScsLib = require ( '../lib/scsLib.js' ) ;
55const scsLib = new ScsLib ( ) ;
6-
6+ // To enable debug logging, set the env var DEBUG="type function" with whatever things you want to see.
7+ const debugFunction = require ( 'debug' ) ( 'function' ) ;
8+ const debugPayload = require ( 'debug' ) ( 'payload' ) ;
9+ const debugProperty = require ( 'debug' ) ( 'property' ) ;
10+ const debugTopic = require ( 'debug' ) ( 'topic' ) ;
11+ const debugType = require ( 'debug' ) ( 'type' ) ;
12+
713// Library versions
814const SOLACE_SPRING_CLOUD_VERSION = '1.1.1' ;
915const SPRING_BOOT_VERSION = '2.3.2.RELEASE' ;
@@ -152,7 +158,6 @@ function appProperties([asyncapi, params]) {
152158 }
153159 }
154160 const ym = yaml . safeDump ( doc , { lineWidth : 200 } ) ;
155- //console.log(ym);
156161 return ym ;
157162}
158163filter . appProperties = appProperties ;
@@ -186,13 +191,14 @@ function appExtraIncludes(asyncapi) {
186191filter . appExtraIncludes = appExtraIncludes ;
187192
188193function schemaExtraIncludes ( [ schemaName , schema ] ) {
189- //console.log("checkPropertyNames " + schemaName + " " + schema.type());
194+ debugProperty ( `schemaExtraIncludes ${ schemaName } ${ schema . type ( ) } ` ) ;
195+
190196 const ret = { } ;
191197 if ( checkPropertyNames ( schemaName , schema ) ) {
192198 ret . needJsonPropertyInclude = true ;
193199 }
194- //console.log(" checkPropertyNames:" );
195- //console.log (ret);
200+ debugProperty ( ' checkPropertyNames:' ) ;
201+ debugProperty ( ret ) ;
196202 return ret ;
197203}
198204filter . schemaExtraIncludes = schemaExtraIncludes ;
@@ -225,29 +231,30 @@ function indent3(numTabs) {
225231filter . indent3 = indent3 ;
226232// This returns the proper Java type for a schema property.
227233function fixType ( [ name , javaName , property ] ) {
228- //console.log('fixType: ' + name + " " + dump(property));
234+ debugType ( `fixType: ${ name } ` ) ;
235+ debugType ( property ) ;
229236
230237 let isArrayOfObjects = false ;
231238
232239 // For message headers, type is a property.
233240 // For schema properties, type is a function.
234241 let type = property . type ;
235242 let format = property . format ;
236- //console.log(" fixType: " + property);
243+ debugType ( ` fixType: ${ property } ` ) ;
237244
238245 if ( typeof type === 'function' ) {
239246 type = property . type ( ) ;
240247 format = property . format ( ) ;
241248 }
242249
243- //console.log (`fixType: type: ${type} javaNamne ${javaName}` );
244- //console.log (property);
250+ debugType ( `fixType: type: ${ type } javaName ${ javaName } ` ) ;
251+ debugType ( property ) ;
245252 // If a schema has a property that is a ref to another schema,
246253 // the type is undefined, and the title gives the title of the referenced schema.
247254 let typeName ;
248255 if ( type === undefined ) {
249256 if ( property . enum ( ) ) {
250- //console.log("It's an enum." );
257+ debugType ( 'It is an enum.' ) ;
251258 typeName = _ . upperFirst ( javaName ) ;
252259 } else {
253260 // check to see if it's a ref to another schema.
@@ -281,7 +288,7 @@ function fixType([name, javaName, property]) {
281288 } else if ( type === 'object' ) {
282289 typeName = _ . upperFirst ( javaName ) ;
283290 } else if ( property . enum ( ) ) {
284- //console.log("It's an enum." );
291+ debugType ( 'It is an enum.' ) ;
285292 typeName = _ . upperFirst ( javaName ) ;
286293 } else {
287294 typeName = getType ( type , format ) . javaType ;
@@ -408,42 +415,42 @@ filter.topicInfo = topicInfo;
408415function checkPropertyNames ( name , schema ) {
409416 const ret = false ;
410417
411- //console.log(JSON.stringify( schema) );
412- //console.log('checkPropertyNames: checking schema ' + name + getMethods(schema) );
413-
418+ debugProperty ( `checkPropertyNames: checking schema ${ name } ` ) ;
419+ debugProperty ( schema ) ;
420+
414421 let properties = schema . properties ( ) ;
415422
416423 if ( schema . type ( ) === 'array' ) {
417424 properties = schema . items ( ) . properties ( ) ;
418425 }
419426
420- //console.log(" schema type: " + schema.type());
427+ debugProperty ( ` schema type : ${ schema . type ( ) } ` ) ;
421428
422429 for ( const propName in properties ) {
423430 const javaName = _ . camelCase ( propName ) ;
424431 const prop = properties [ propName ] ;
425- //console.log(' checking ' + propName + ' ' + prop.type());
432+ debugProperty ( ` checking ${ propName } ${ prop . type ( ) } ` ) ;
426433
427434 if ( javaName !== propName ) {
428- //console.log(" Java name " + javaName + " is different from " + propName);
435+ debugProperty ( ` Java name ${ javaName } is different from ${ propName } ` ) ;
429436 return true ;
430437 }
431438 if ( prop . type ( ) === 'object' ) {
432- //console.log(" Recursing into object" );
439+ debugProperty ( ' Recursing into object' ) ;
433440 const check = checkPropertyNames ( propName , prop ) ;
434441 if ( check ) {
435442 return true ;
436443 }
437444 } else if ( prop . type ( ) === 'array' ) {
438- //console.log(' checkPropertyNames: ' + JSON.stringify( prop) );
445+ debugProperty ( ` checkPropertyNames: ${ prop } ` ) ;
439446 if ( ! prop . items ) {
440447 throw new Error ( `Array named ${ propName } must have an 'items' property to indicate what type the array elements are.` ) ;
441448 }
442449 const itemsType = prop . items ( ) . type ( ) ;
443- //console.log(' checkPropertyNames: ' + JSON.stringify( prop.items) );
444- //console.log(' array of : ' + itemsType);
450+ debugProperty ( ` checkPropertyNames: ${ prop . items } ` ) ;
451+ debugProperty ( ` array of ${ itemsType } ` ) ;
445452 if ( itemsType === 'object' ) {
446- //console.log(" Recursing into array" );
453+ debugProperty ( ' Recursing into array' ) ;
447454 const check = checkPropertyNames ( propName , prop . items ( ) ) ;
448455 if ( check ) {
449456 return true ;
@@ -475,12 +482,12 @@ function getAdditionalSubs(asyncapi, params) {
475482 const functionName = getFunctionName ( channelName , subscribe , true ) ;
476483 const topicInfo = getTopicInfo ( channelName , channel ) ;
477484 const queue = subscribe . ext ( 'x-scs-destination' ) ;
478- if ( topicInfo . hasParams || queue ) {
485+ if ( queue ) {
479486 if ( ! ret ) {
480487 ret = { } ;
481488 ret . bindings = { } ;
482489 }
483- const bindingName = `${ functionName } -in-0` ;
490+ const bindingName = `${ functionName } -in-0` ;
484491 ret . bindings [ bindingName ] = { } ;
485492 ret . bindings [ bindingName ] . consumer = { } ;
486493 ret . bindings [ bindingName ] . consumer . queueAdditionalSubscriptions = topicInfo . subscribeTopic ;
@@ -515,10 +522,10 @@ function getBindings(asyncapi, params) {
515522// This returns the base function name that SCSt will use to map functions with bindings.
516523function getFunctionName ( channelName , operation , isSubscriber ) {
517524 let ret ;
518- //console.log(' getFunctionName operation: ' + JSON.stringify( operation) );
519- //console.log (operation);
525+ debugFunction ( ` getFunctionName operation: ${ operation } ` ) ;
526+ //debugFunction (operation);
520527 let functionName = operation . ext ( 'x-scs-function-name' ) ;
521- //console.log (getMethods(operation));
528+ //debugFunction (getMethods(operation));
522529
523530 if ( ! functionName ) {
524531 functionName = operation . id ( ) ;
@@ -529,15 +536,15 @@ function getFunctionName(channelName, operation, isSubscriber) {
529536 } else {
530537 ret = _ . camelCase ( channelName ) + ( isSubscriber ? 'Consumer' : 'Supplier' ) ;
531538 }
539+ debugFunction ( ret ) ;
532540 return ret ;
533541}
534542
535543// This returns the base function name that SCSt will use to map functions with bindings.
536544function getFunctionNameByChannel ( channelName , channel ) {
537545 let ret = _ . camelCase ( channelName ) ;
538- //console.log('functionName channel: ' + JSON.stringify(channelJson));
539546 const functionName = channel . ext ( 'x-scs-function-name' ) ;
540- //console.log('function name for channel ' + channelName + ': ' + functionName);
547+ debugFunction ( `getFunctionNameByChannel ${ channel } ${ functionName } ` ) ;
541548 if ( functionName ) {
542549 ret = functionName ;
543550 }
@@ -561,10 +568,10 @@ function getFunctionSpecs(asyncapi, params) {
561568
562569 for ( const channelName in asyncapi . channels ( ) ) {
563570 const channel = asyncapi . channels ( ) [ channelName ] ;
564- //console.log(" =====================================" );
565- //console.log("channelJson: " + JSON.stringify(channel._json) );
566- //console.log("getFunctionSpecs: " + channelName );
567- //console.log(" =====================================" );
571+ debugFunction ( ' =====================================' ) ;
572+ debugFunction ( `getFunctionSpecs ${ channelName } ` ) ;
573+ debugFunction ( channel . _json ) ;
574+ debugFunction ( ' =====================================' ) ;
568575 let functionSpec ;
569576 const publish = scsLib . getRealPublisher ( info , params , channel ) ;
570577 if ( publish ) {
@@ -584,7 +591,7 @@ function getFunctionSpecs(asyncapi, params) {
584591 }
585592 const payload = getPayloadClass ( publish ) ;
586593 if ( ! payload ) {
587- throw new Error ( `Channel ${ channelName } : no payload class has been defined.` ) ;
594+ throw new Error ( `Channel ${ channelName } : no payload class has been defined.` ) ;
588595 }
589596 functionSpec . publishPayload = payload ;
590597 functionSpec . publishChannel = channelName ;
@@ -619,9 +626,13 @@ function getFunctionSpecs(asyncapi, params) {
619626 if ( dest ) {
620627 functionSpec . subscribeChannel = dest ;
621628 } else {
622- functionSpec . subscribeChannel = channelName ;
629+ const topicInfo = getTopicInfo ( channelName , channel ) ;
630+ functionSpec . subscribeChannel = topicInfo . subscribeTopic ;
623631 }
624632 }
633+
634+ debugFunction ( 'functionSpec:' ) ;
635+ debugFunction ( functionSpec ) ;
625636 }
626637
627638 return functionMap ;
@@ -631,7 +642,7 @@ function getPayloadClass(pubOrSub) {
631642 let ret ;
632643
633644 if ( pubOrSub ) {
634- //console.log (pubOrSub);
645+ debugPayload ( pubOrSub ) ;
635646 if ( pubOrSub . hasMultipleMessages ( ) ) {
636647 ret = 'Message<?>' ;
637648 } else {
@@ -646,7 +657,7 @@ function getPayloadClass(pubOrSub) {
646657 }
647658 }
648659 }
649- //console.log(" getPayloadClass: " + ret);
660+ debugPayload ( ` getPayloadClass: ${ ret } ` ) ;
650661 }
651662
652663 return ret ;
@@ -674,14 +685,16 @@ function getTopicInfo(channelName, channel) {
674685 let sampleArgList = '' ;
675686 let first = true ;
676687
677- //console.log("params: " + JSON.stringify(channel.parameters()));
688+ debugTopic ( 'params:' ) ;
689+ debugTopic ( channel . parameters ( ) ) ;
678690 for ( const name in channel . parameters ( ) ) {
679691 const nameWithBrackets = `{${ name } }` ;
680692 const parameter = channel . parameter ( name ) ;
681693 const schema = parameter . schema ( ) ;
682694 const type = getType ( schema . type ( ) , schema . format ( ) ) ;
683695 const param = { name : _ . lowerFirst ( name ) } ;
684- //console.log("name: " + name + " type: " + type);
696+ debugTopic ( `name: ${ name } type:` ) ;
697+ debugTopic ( type ) ;
685698 let sampleArg = 1 ;
686699
687700 if ( first ) {
@@ -694,32 +707,33 @@ function getTopicInfo(channelName, channel) {
694707 sampleArgList += ', ' ;
695708
696709 if ( type ) {
697- //console.log("It's a type: " + type);
710+ debugTopic ( 'It is a type:' ) ;
711+ debugTopic ( type ) ;
698712 const javaType = type . javaType || typeMap . get ( type ) ;
699713 if ( ! javaType ) throw new Error ( `topicInfo filter: type not found in typeMap: ${ type } ` ) ;
700714 param . type = javaType ;
701715 const printfArg = type . printFormat ;
702- //console.log(" printf: " + printfArg);
716+ debugTopic ( ` printf: ${ printfArg } ` ) ;
703717 if ( ! printfArg ) throw new Error ( `topicInfo filter: type not found in formatMap: ${ type } ` ) ;
704- //console.log(" Replacing " + nameWithBrackets);
718+ debugTopic ( ` Replacing ${ nameWithBrackets } ` ) ;
705719 publishTopic = publishTopic . replace ( nameWithBrackets , printfArg ) ;
706720 sampleArg = type . sample ;
707721 } else {
708722 const en = schema . enum ( ) ;
709723 if ( en ) {
710- //console.log("It's an enum: " + en );
724+ debugTopic ( `It is an enum: ${ en } ` ) ;
711725 param . type = _ . upperFirst ( name ) ;
712726 param . enum = en ;
713- sampleArg = `Messaging.${ param . type } .${ en [ 0 ] } ` ;
714- //console.log(" Replacing " + nameWithBrackets);
727+ sampleArg = `Messaging.${ param . type } .${ en [ 0 ] } ` ;
728+ debugTopic ( ` Replacing ${ nameWithBrackets } ` ) ;
715729 publishTopic = publishTopic . replace ( nameWithBrackets , '%s' ) ;
716730 } else {
717731 throw new Error ( `topicInfo filter: Unknown parameter type: ${ JSON . stringify ( schema ) } ` ) ;
718732 }
719733 }
720734
721735 subscribeTopic = subscribeTopic . replace ( nameWithBrackets , '*' ) ;
722- functionParamList += `${ param . type } ${ param . name } ` ;
736+ functionParamList += `${ param . type } ${ param . name } ` ;
723737 functionArgList += param . name ;
724738 sampleArgList += sampleArg ;
725739 params . push ( param ) ;
0 commit comments