@@ -260,10 +260,10 @@ function flattenWithBlankLines(...items) {
260
260
. flatMap ( item => item )
261
261
}
262
262
263
- function protoifyChildren ( container ) {
263
+ function protoifyChildren ( container , proto3 ) {
264
264
return flattenWithBlankLines (
265
265
...Object . values ( container . enums ) . map ( protoifyEnum ) ,
266
- ...Object . values ( container . messages ) . map ( protoifyMessage ) ,
266
+ ...Object . values ( container . messages ) . map ( msg => protoifyMessage ( msg , proto3 ) ) ,
267
267
)
268
268
}
269
269
@@ -289,6 +289,14 @@ function protoifyEnum(enumDef) {
289
289
290
290
const { TYPES , TYPE_MASK , FLAGS } = requireModule ( "WAProtoConst" )
291
291
292
+ function mapFieldTypeName ( ref , parentModule , parentPath ) {
293
+ if ( typeof ref === "object" ) {
294
+ return fieldTypeName ( TYPES . MESSAGE , ref , parentModule , parentPath )
295
+ } else {
296
+ return fieldTypeName ( ref , undefined , parentModule , parentPath )
297
+ }
298
+ }
299
+
292
300
function fieldTypeName ( typeID , typeRef , parentModule , parentPath ) {
293
301
switch ( typeID ) {
294
302
case TYPES . INT32 :
@@ -323,6 +331,8 @@ function fieldTypeName(typeID, typeRef, parentModule, parentPath) {
323
331
namePath . push ( ...typeRef . __path__ . slice ( pathStartIndex ) )
324
332
namePath . push ( typeRef . __name__ )
325
333
return namePath . join ( "." )
334
+ case TYPES . MAP :
335
+ return `map<${ mapFieldTypeName ( typeRef [ 0 ] , parentModule , parentPath ) } , ${ mapFieldTypeName ( typeRef [ 1 ] , parentModule , parentPath ) } >`
326
336
case TYPES . FIXED64 :
327
337
return "fixed64"
328
338
case TYPES . SFIXED64 :
@@ -374,16 +384,19 @@ function fixFieldName(name) {
374
384
. replace ( "Sha256" , "SHA256" )
375
385
}
376
386
377
- function protoifyField ( name , [ index , flags , typeRef ] , parentModule , parentPath , isOneOf ) {
387
+ function protoifyField ( name , [ index , flags , typeRef ] , parentModule , parentPath , isOneOf , proto3 ) {
378
388
const preflags = [ ]
379
389
const postflags = [ "" ]
390
+ const isMap = ( flags & TYPE_MASK ) === TYPES . MAP
380
391
if ( ! isOneOf ) {
381
392
if ( ( flags & FLAGS . REPEATED ) !== 0 ) {
382
393
preflags . push ( "repeated" )
383
- } else if ( ( flags & FLAGS . REQUIRED ) === 0 ) {
384
- preflags . push ( "optional" )
385
- } else {
386
- preflags . push ( "required" )
394
+ } else if ( ! proto3 ) {
395
+ if ( ( flags & FLAGS . REQUIRED ) === 0 ) {
396
+ preflags . push ( "optional" )
397
+ } else {
398
+ preflags . push ( "required" )
399
+ }
387
400
}
388
401
}
389
402
preflags . push ( fieldTypeName ( flags & TYPE_MASK , typeRef , parentModule , parentPath ) )
@@ -393,12 +406,12 @@ function protoifyField(name, [index, flags, typeRef], parentModule, parentPath,
393
406
return `${ preflags . join ( " " ) } ${ fixFieldName ( name ) } = ${ index } ${ postflags . join ( " " ) } ;`
394
407
}
395
408
396
- function protoifyFields ( fields , parentModule , parentPath , isOneOf ) {
397
- return Object . entries ( fields ) . map ( ( [ name , definition ] ) => protoifyField ( name , definition , parentModule , parentPath , isOneOf ) )
409
+ function protoifyFields ( fields , parentModule , parentPath , isOneOf , proto3 ) {
410
+ return Object . entries ( fields ) . map ( ( [ name , definition ] ) => protoifyField ( name , definition , parentModule , parentPath , isOneOf , proto3 ) )
398
411
}
399
412
400
- function protoifyMessage ( message ) {
401
- const sections = [ protoifyChildren ( message ) ]
413
+ function protoifyMessage ( message , proto3 ) {
414
+ const sections = [ protoifyChildren ( message , proto3 ) ]
402
415
const spec = message . message
403
416
const fullMessagePath = message . __path__ . concat ( [ message . __name__ ] )
404
417
for ( const [ name , fieldNames ] of Object . entries ( spec . __oneofs__ ?? { } ) ) {
@@ -407,24 +420,33 @@ function protoifyMessage(message) {
407
420
delete spec [ fieldName ]
408
421
return [ fieldName , def ]
409
422
} ) )
410
- sections . push ( [ `oneof ${ name } ` + "{" , ...indent ( protoifyFields ( fields , message . __module__ , fullMessagePath , true ) ) , "}" ] )
423
+ sections . push ( [ `oneof ${ name } ` + "{" , ...indent ( protoifyFields ( fields , message . __module__ , fullMessagePath , true , proto3 ) ) , "}" ] )
411
424
}
412
425
if ( spec . __reserved__ ) {
413
426
console . warn ( "Found reserved keys:" , message . __name__ , spec . __reserved__ )
414
427
}
415
428
delete spec . __oneofs__
416
429
delete spec . __reserved__
417
- sections . push ( protoifyFields ( spec , message . __module__ , fullMessagePath , false ) )
430
+ sections . push ( protoifyFields ( spec , message . __module__ , fullMessagePath , false , proto3 ) )
418
431
return [ `message ${ message . __name__ } ` + "{" , ...indent ( flattenWithBlankLines ( ...sections ) ) , "}" ]
419
432
}
420
433
421
434
function goPackageName ( name ) {
422
435
return name . replace ( / ^ W A / , "wa" ) . replace ( "WebProtobufs" , "" ) . replace ( "Protobufs" , "" )
423
436
}
424
437
438
+ const needsProto3 = {
439
+ "WAWebProtobufsReporting" : true
440
+ }
441
+
425
442
function protoifyModule ( module ) {
426
443
const output = [ ]
427
- output . push ( `syntax = "proto2";` )
444
+ const proto3 = needsProto3 [ module . __name__ ]
445
+ if ( proto3 ) {
446
+ output . push ( `syntax = "proto3";` )
447
+ } else {
448
+ output . push ( `syntax = "proto2";` )
449
+ }
428
450
output . push ( `package ${ module . __name__ } ;` )
429
451
output . push ( `option go_package = "go.mau.fi/whatsmeow/proto/${ goPackageName ( module . __name__ ) } ";` )
430
452
output . push ( "" )
@@ -434,7 +456,7 @@ function protoifyModule(module) {
434
456
}
435
457
output . push ( "" )
436
458
}
437
- const children = protoifyChildren ( module )
459
+ const children = protoifyChildren ( module , proto3 )
438
460
children . push ( "" )
439
461
return output . concat ( children )
440
462
}
0 commit comments