@@ -362,72 +362,12 @@ function toJsType(field, parentIsInterface = false) {
362362 return type ;
363363}
364364
365- function syntaxForType ( type ) {
366-
367- var syntax = null ;
368- var namespace = type ;
369-
370- while ( syntax === null && namespace !== null ) {
371- if ( namespace . options != null && "syntax" in namespace . options ) {
372- syntax = namespace . options [ "syntax" ] ;
373- }
374- else {
375- namespace = namespace . parent ;
376- }
377- }
378-
379- return syntax !== null ? syntax : "proto2" ;
380- }
381-
382- function isExplicitPresence ( field , syntax ) {
383-
384- // In proto3, optional fields are explicit
385- if ( syntax === "proto3" ) {
386- return field . options != null && field . options [ "proto3_optional" ] === true ;
387- }
388-
389- // In proto2, fields are explicitly optional if they are not part of a map, array or oneOf group
390- if ( syntax === "proto2" ) {
391- return field . optional && ! ( field . partOf || field . repeated || field . map ) ;
392- }
393-
394- throw new Error ( "Unknown proto syntax: [" + syntax + "]" ) ;
395- }
396-
397- function isImplicitPresence ( field , syntax ) {
398-
399- // In proto3, everything not marked optional has implicit presence (including maps and repeated fields)
400- if ( syntax === "proto3" ) {
401- return field . options == null || field . options [ "proto3_optional" ] !== true ;
402- }
403-
404- // In proto2, nothing has implicit presence
405- if ( syntax === "proto2" ) {
406- return false ;
407- }
408-
409- throw new Error ( "Unknown proto syntax: [" + syntax + "]" ) ;
410- }
411-
412- function isOptionalOneOf ( oneof , syntax ) {
413-
414- if ( syntax === "proto2" ) {
415- return false ;
416- }
417-
418- if ( oneof . fieldsArray == null || oneof . fieldsArray . length !== 1 ) {
419- return false ;
420- }
421-
422- var field = oneof . fieldsArray [ 0 ] ;
423-
424- return field . options != null && field . options [ "proto3_optional" ] === true ;
365+ function isNullable ( field ) {
366+ return field . hasPresence && ! field . required ;
425367}
426368
427369function buildType ( ref , type ) {
428370
429- var syntax = syntaxForType ( type ) ;
430-
431371 if ( config . comments ) {
432372 var typeDef = [
433373 "Properties of " + aOrAn ( type . name ) + "." ,
@@ -443,13 +383,15 @@ function buildType(ref, type) {
443383 // With semantic nulls, only explicit optional fields and one-of members can be set to null
444384 // Implicit fields (proto3), maps and lists can be omitted, but if specified must be non-null
445385 // Implicit fields will take their default value when the message is constructed
446- if ( isExplicitPresence ( field , syntax ) || field . partOf ) {
447- jsType = jsType + "|null|undefined" ;
448- nullable = true ;
449- }
450- else if ( isImplicitPresence ( field , syntax ) || field . repeated || field . map ) {
451- jsType = jsType + "|undefined" ;
452- nullable = true ;
386+ if ( field . optional ) {
387+ if ( isNullable ( field ) ) {
388+ jsType = jsType + "|null|undefined" ;
389+ nullable = true ;
390+ }
391+ else {
392+ jsType = jsType + "|undefined" ;
393+ nullable = true ;
394+ }
453395 }
454396 }
455397 else {
@@ -490,7 +432,7 @@ function buildType(ref, type) {
490432 // With semantic nulls, fields are nullable if they are explicitly optional or part of a one-of
491433 // Maps, repeated values and fields with implicit defaults are never null after construction
492434 // Members are never undefined, at a minimum they are initialized to null
493- if ( isExplicitPresence ( field , syntax ) || field . partOf ) {
435+ if ( isNullable ( field ) ) {
494436 jsType = jsType + "|null" ;
495437 }
496438 }
@@ -514,7 +456,7 @@ function buildType(ref, type) {
514456 // With semantic nulls, only explict optional fields and one-of members are null by default
515457 // Otherwise use field.optional, which doesn't consider proto3, maps, repeated fields etc.
516458 var nullDefault = config [ "null-semantics" ]
517- ? isExplicitPresence ( field , syntax )
459+ ? isNullable ( field )
518460 : field . optional && config [ "null-defaults" ] ;
519461 if ( field . repeated )
520462 push ( escapeName ( type . name ) + ".prototype" + prop + " = $util.emptyArray;" ) ; // overwritten in constructor
@@ -546,7 +488,7 @@ function buildType(ref, type) {
546488 }
547489 oneof . resolve ( ) ;
548490 push ( "" ) ;
549- if ( isOptionalOneOf ( oneof , syntax ) ) {
491+ if ( oneof . isProto3Optional ) {
550492 push ( "// Virtual OneOf for proto3 optional field" ) ;
551493 }
552494 else {
0 commit comments