@@ -3,7 +3,13 @@ const yaml = require('js-yaml');
3
3
const _ = require ( 'lodash' ) ;
4
4
const ScsLib = require ( '../lib/scsLib.js' ) ;
5
5
const 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
+
7
13
// Library versions
8
14
const SOLACE_SPRING_CLOUD_VERSION = '1.1.1' ;
9
15
const SPRING_BOOT_VERSION = '2.3.2.RELEASE' ;
@@ -152,7 +158,6 @@ function appProperties([asyncapi, params]) {
152
158
}
153
159
}
154
160
const ym = yaml . safeDump ( doc , { lineWidth : 200 } ) ;
155
- //console.log(ym);
156
161
return ym ;
157
162
}
158
163
filter . appProperties = appProperties ;
@@ -186,13 +191,14 @@ function appExtraIncludes(asyncapi) {
186
191
filter . appExtraIncludes = appExtraIncludes ;
187
192
188
193
function schemaExtraIncludes ( [ schemaName , schema ] ) {
189
- //console.log("checkPropertyNames " + schemaName + " " + schema.type());
194
+ debugProperty ( `schemaExtraIncludes ${ schemaName } ${ schema . type ( ) } ` ) ;
195
+
190
196
const ret = { } ;
191
197
if ( checkPropertyNames ( schemaName , schema ) ) {
192
198
ret . needJsonPropertyInclude = true ;
193
199
}
194
- //console.log(" checkPropertyNames:" );
195
- //console.log (ret);
200
+ debugProperty ( ' checkPropertyNames:' ) ;
201
+ debugProperty ( ret ) ;
196
202
return ret ;
197
203
}
198
204
filter . schemaExtraIncludes = schemaExtraIncludes ;
@@ -225,29 +231,30 @@ function indent3(numTabs) {
225
231
filter . indent3 = indent3 ;
226
232
// This returns the proper Java type for a schema property.
227
233
function fixType ( [ name , javaName , property ] ) {
228
- //console.log('fixType: ' + name + " " + dump(property));
234
+ debugType ( `fixType: ${ name } ` ) ;
235
+ debugType ( property ) ;
229
236
230
237
let isArrayOfObjects = false ;
231
238
232
239
// For message headers, type is a property.
233
240
// For schema properties, type is a function.
234
241
let type = property . type ;
235
242
let format = property . format ;
236
- //console.log(" fixType: " + property);
243
+ debugType ( ` fixType: ${ property } ` ) ;
237
244
238
245
if ( typeof type === 'function' ) {
239
246
type = property . type ( ) ;
240
247
format = property . format ( ) ;
241
248
}
242
249
243
- //console.log (`fixType: type: ${type} javaNamne ${javaName}` );
244
- //console.log (property);
250
+ debugType ( `fixType: type: ${ type } javaName ${ javaName } ` ) ;
251
+ debugType ( property ) ;
245
252
// If a schema has a property that is a ref to another schema,
246
253
// the type is undefined, and the title gives the title of the referenced schema.
247
254
let typeName ;
248
255
if ( type === undefined ) {
249
256
if ( property . enum ( ) ) {
250
- //console.log("It's an enum." );
257
+ debugType ( 'It is an enum.' ) ;
251
258
typeName = _ . upperFirst ( javaName ) ;
252
259
} else {
253
260
// check to see if it's a ref to another schema.
@@ -281,7 +288,7 @@ function fixType([name, javaName, property]) {
281
288
} else if ( type === 'object' ) {
282
289
typeName = _ . upperFirst ( javaName ) ;
283
290
} else if ( property . enum ( ) ) {
284
- //console.log("It's an enum." );
291
+ debugType ( 'It is an enum.' ) ;
285
292
typeName = _ . upperFirst ( javaName ) ;
286
293
} else {
287
294
typeName = getType ( type , format ) . javaType ;
@@ -408,42 +415,42 @@ filter.topicInfo = topicInfo;
408
415
function checkPropertyNames ( name , schema ) {
409
416
const ret = false ;
410
417
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
+
414
421
let properties = schema . properties ( ) ;
415
422
416
423
if ( schema . type ( ) === 'array' ) {
417
424
properties = schema . items ( ) . properties ( ) ;
418
425
}
419
426
420
- //console.log(" schema type: " + schema.type());
427
+ debugProperty ( ` schema type : ${ schema . type ( ) } ` ) ;
421
428
422
429
for ( const propName in properties ) {
423
430
const javaName = _ . camelCase ( propName ) ;
424
431
const prop = properties [ propName ] ;
425
- //console.log(' checking ' + propName + ' ' + prop.type());
432
+ debugProperty ( ` checking ${ propName } ${ prop . type ( ) } ` ) ;
426
433
427
434
if ( javaName !== propName ) {
428
- //console.log(" Java name " + javaName + " is different from " + propName);
435
+ debugProperty ( ` Java name ${ javaName } is different from ${ propName } ` ) ;
429
436
return true ;
430
437
}
431
438
if ( prop . type ( ) === 'object' ) {
432
- //console.log(" Recursing into object" );
439
+ debugProperty ( ' Recursing into object' ) ;
433
440
const check = checkPropertyNames ( propName , prop ) ;
434
441
if ( check ) {
435
442
return true ;
436
443
}
437
444
} else if ( prop . type ( ) === 'array' ) {
438
- //console.log(' checkPropertyNames: ' + JSON.stringify( prop) );
445
+ debugProperty ( ` checkPropertyNames: ${ prop } ` ) ;
439
446
if ( ! prop . items ) {
440
447
throw new Error ( `Array named ${ propName } must have an 'items' property to indicate what type the array elements are.` ) ;
441
448
}
442
449
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 } ` ) ;
445
452
if ( itemsType === 'object' ) {
446
- //console.log(" Recursing into array" );
453
+ debugProperty ( ' Recursing into array' ) ;
447
454
const check = checkPropertyNames ( propName , prop . items ( ) ) ;
448
455
if ( check ) {
449
456
return true ;
@@ -475,12 +482,12 @@ function getAdditionalSubs(asyncapi, params) {
475
482
const functionName = getFunctionName ( channelName , subscribe , true ) ;
476
483
const topicInfo = getTopicInfo ( channelName , channel ) ;
477
484
const queue = subscribe . ext ( 'x-scs-destination' ) ;
478
- if ( topicInfo . hasParams || queue ) {
485
+ if ( queue ) {
479
486
if ( ! ret ) {
480
487
ret = { } ;
481
488
ret . bindings = { } ;
482
489
}
483
- const bindingName = `${ functionName } -in-0` ;
490
+ const bindingName = `${ functionName } -in-0` ;
484
491
ret . bindings [ bindingName ] = { } ;
485
492
ret . bindings [ bindingName ] . consumer = { } ;
486
493
ret . bindings [ bindingName ] . consumer . queueAdditionalSubscriptions = topicInfo . subscribeTopic ;
@@ -515,10 +522,10 @@ function getBindings(asyncapi, params) {
515
522
// This returns the base function name that SCSt will use to map functions with bindings.
516
523
function getFunctionName ( channelName , operation , isSubscriber ) {
517
524
let ret ;
518
- //console.log(' getFunctionName operation: ' + JSON.stringify( operation) );
519
- //console.log (operation);
525
+ debugFunction ( ` getFunctionName operation: ${ operation } ` ) ;
526
+ //debugFunction (operation);
520
527
let functionName = operation . ext ( 'x-scs-function-name' ) ;
521
- //console.log (getMethods(operation));
528
+ //debugFunction (getMethods(operation));
522
529
523
530
if ( ! functionName ) {
524
531
functionName = operation . id ( ) ;
@@ -529,15 +536,15 @@ function getFunctionName(channelName, operation, isSubscriber) {
529
536
} else {
530
537
ret = _ . camelCase ( channelName ) + ( isSubscriber ? 'Consumer' : 'Supplier' ) ;
531
538
}
539
+ debugFunction ( ret ) ;
532
540
return ret ;
533
541
}
534
542
535
543
// This returns the base function name that SCSt will use to map functions with bindings.
536
544
function getFunctionNameByChannel ( channelName , channel ) {
537
545
let ret = _ . camelCase ( channelName ) ;
538
- //console.log('functionName channel: ' + JSON.stringify(channelJson));
539
546
const functionName = channel . ext ( 'x-scs-function-name' ) ;
540
- //console.log('function name for channel ' + channelName + ': ' + functionName);
547
+ debugFunction ( `getFunctionNameByChannel ${ channel } ${ functionName } ` ) ;
541
548
if ( functionName ) {
542
549
ret = functionName ;
543
550
}
@@ -561,10 +568,10 @@ function getFunctionSpecs(asyncapi, params) {
561
568
562
569
for ( const channelName in asyncapi . channels ( ) ) {
563
570
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 ( ' =====================================' ) ;
568
575
let functionSpec ;
569
576
const publish = scsLib . getRealPublisher ( info , params , channel ) ;
570
577
if ( publish ) {
@@ -584,7 +591,7 @@ function getFunctionSpecs(asyncapi, params) {
584
591
}
585
592
const payload = getPayloadClass ( publish ) ;
586
593
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.` ) ;
588
595
}
589
596
functionSpec . publishPayload = payload ;
590
597
functionSpec . publishChannel = channelName ;
@@ -619,9 +626,13 @@ function getFunctionSpecs(asyncapi, params) {
619
626
if ( dest ) {
620
627
functionSpec . subscribeChannel = dest ;
621
628
} else {
622
- functionSpec . subscribeChannel = channelName ;
629
+ const topicInfo = getTopicInfo ( channelName , channel ) ;
630
+ functionSpec . subscribeChannel = topicInfo . subscribeTopic ;
623
631
}
624
632
}
633
+
634
+ debugFunction ( 'functionSpec:' ) ;
635
+ debugFunction ( functionSpec ) ;
625
636
}
626
637
627
638
return functionMap ;
@@ -631,7 +642,7 @@ function getPayloadClass(pubOrSub) {
631
642
let ret ;
632
643
633
644
if ( pubOrSub ) {
634
- //console.log (pubOrSub);
645
+ debugPayload ( pubOrSub ) ;
635
646
if ( pubOrSub . hasMultipleMessages ( ) ) {
636
647
ret = 'Message<?>' ;
637
648
} else {
@@ -646,7 +657,7 @@ function getPayloadClass(pubOrSub) {
646
657
}
647
658
}
648
659
}
649
- //console.log(" getPayloadClass: " + ret);
660
+ debugPayload ( ` getPayloadClass: ${ ret } ` ) ;
650
661
}
651
662
652
663
return ret ;
@@ -674,14 +685,16 @@ function getTopicInfo(channelName, channel) {
674
685
let sampleArgList = '' ;
675
686
let first = true ;
676
687
677
- //console.log("params: " + JSON.stringify(channel.parameters()));
688
+ debugTopic ( 'params:' ) ;
689
+ debugTopic ( channel . parameters ( ) ) ;
678
690
for ( const name in channel . parameters ( ) ) {
679
691
const nameWithBrackets = `{${ name } }` ;
680
692
const parameter = channel . parameter ( name ) ;
681
693
const schema = parameter . schema ( ) ;
682
694
const type = getType ( schema . type ( ) , schema . format ( ) ) ;
683
695
const param = { name : _ . lowerFirst ( name ) } ;
684
- //console.log("name: " + name + " type: " + type);
696
+ debugTopic ( `name: ${ name } type:` ) ;
697
+ debugTopic ( type ) ;
685
698
let sampleArg = 1 ;
686
699
687
700
if ( first ) {
@@ -694,32 +707,33 @@ function getTopicInfo(channelName, channel) {
694
707
sampleArgList += ', ' ;
695
708
696
709
if ( type ) {
697
- //console.log("It's a type: " + type);
710
+ debugTopic ( 'It is a type:' ) ;
711
+ debugTopic ( type ) ;
698
712
const javaType = type . javaType || typeMap . get ( type ) ;
699
713
if ( ! javaType ) throw new Error ( `topicInfo filter: type not found in typeMap: ${ type } ` ) ;
700
714
param . type = javaType ;
701
715
const printfArg = type . printFormat ;
702
- //console.log(" printf: " + printfArg);
716
+ debugTopic ( ` printf: ${ printfArg } ` ) ;
703
717
if ( ! printfArg ) throw new Error ( `topicInfo filter: type not found in formatMap: ${ type } ` ) ;
704
- //console.log(" Replacing " + nameWithBrackets);
718
+ debugTopic ( ` Replacing ${ nameWithBrackets } ` ) ;
705
719
publishTopic = publishTopic . replace ( nameWithBrackets , printfArg ) ;
706
720
sampleArg = type . sample ;
707
721
} else {
708
722
const en = schema . enum ( ) ;
709
723
if ( en ) {
710
- //console.log("It's an enum: " + en );
724
+ debugTopic ( `It is an enum: ${ en } ` ) ;
711
725
param . type = _ . upperFirst ( name ) ;
712
726
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 } ` ) ;
715
729
publishTopic = publishTopic . replace ( nameWithBrackets , '%s' ) ;
716
730
} else {
717
731
throw new Error ( `topicInfo filter: Unknown parameter type: ${ JSON . stringify ( schema ) } ` ) ;
718
732
}
719
733
}
720
734
721
735
subscribeTopic = subscribeTopic . replace ( nameWithBrackets , '*' ) ;
722
- functionParamList += `${ param . type } ${ param . name } ` ;
736
+ functionParamList += `${ param . type } ${ param . name } ` ;
723
737
functionArgList += param . name ;
724
738
sampleArgList += sampleArg ;
725
739
params . push ( param ) ;
0 commit comments