@@ -379,6 +379,19 @@ function isOptional(field, syntax) {
379
379
return field . optional && ! ( field . partOf || field . repeated || field . map ) ;
380
380
}
381
381
382
+ function isOptionalOneOf ( oneof , syntax ) {
383
+
384
+ if ( syntax !== "proto3" )
385
+ return false ;
386
+
387
+ if ( oneof . fieldsArray == null || oneof . fieldsArray . length !== 1 )
388
+ return false ;
389
+
390
+ var field = oneof . fieldsArray [ 0 ] ;
391
+
392
+ return field . options != null && field . options [ "proto3_optional" ] === true ;
393
+ }
394
+
382
395
function buildType ( ref , type ) {
383
396
384
397
var syntax = syntaxForType ( type ) ;
@@ -494,12 +507,17 @@ function buildType(ref, type) {
494
507
}
495
508
oneof . resolve ( ) ;
496
509
push ( "" ) ;
497
- pushComment ( [
498
- oneof . comment || type . name + " " + oneof . name + "." ,
499
- "@member {" + oneof . oneof . map ( JSON . stringify ) . join ( "|" ) + "|undefined} " + escapeName ( oneof . name ) ,
500
- "@memberof " + exportName ( type ) ,
501
- "@instance"
502
- ] ) ;
510
+ if ( isOptionalOneOf ( oneof , syntax ) ) {
511
+ push ( "// Virtual OneOf for proto3 optional field" ) ;
512
+ }
513
+ else {
514
+ pushComment ( [
515
+ oneof . comment || type . name + " " + oneof . name + "." ,
516
+ "@member {" + oneof . oneof . map ( JSON . stringify ) . join ( "|" ) + "|undefined} " + escapeName ( oneof . name ) ,
517
+ "@memberof " + exportName ( type ) ,
518
+ "@instance"
519
+ ] ) ;
520
+ }
503
521
push ( "Object.defineProperty(" + escapeName ( type . name ) + ".prototype, " + JSON . stringify ( oneof . name ) + ", {" ) ;
504
522
++ indent ;
505
523
push ( "get: $util.oneOfGetter($oneOfFields = [" + oneof . oneof . map ( JSON . stringify ) . join ( ", " ) + "])," ) ;
0 commit comments