-
Notifications
You must be signed in to change notification settings - Fork 348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: support proto2 optional #162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ enum EnumWithoutZero { | |
|
||
message Issue56 { | ||
required EnumWithoutZero test = 1; | ||
optional int32 foo = 2; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,7 +482,11 @@ function generateEnumToJson(fullName: string, enumDesc: EnumDescriptorProto): Fu | |
|
||
// When useOptionals=true, non-scalar fields are translated into optional properties. | ||
function isOptionalProperty(field: FieldDescriptorProto, options: Options): boolean { | ||
return (options.useOptionals && isMessage(field) && !isRepeated(field)) || field.proto3Optional; | ||
return ( | ||
(options.useOptionals && isMessage(field) && !isRepeated(field)) || | ||
field.proto3Optional || | ||
field.label === FieldDescriptorProto.Label.LABEL_OPTIONAL | ||
); | ||
} | ||
|
||
// Create the interface with properties | ||
|
@@ -847,6 +851,11 @@ function generateEncode( | |
) | ||
.addStatement('%L', writeSnippet(`message.${fieldName}`)) | ||
.endControlFlow(); | ||
} else if (field.label === FieldDescriptorProto.Label.LABEL_OPTIONAL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is proto3Optional handled in this |
||
func = func | ||
.beginControlFlow('if (message.%L !== undefined)', fieldName) | ||
.addStatement('%L', writeSnippet(`message.${fieldName}`)) | ||
.endControlFlow(); | ||
} else { | ||
func = func.addStatement('%L', writeSnippet(`message.${fieldName}`)); | ||
} | ||
|
@@ -979,7 +988,9 @@ function generateFromJson( | |
if ( | ||
!isRepeated(field) && | ||
field.type !== FieldDescriptorProto.Type.TYPE_BYTES && | ||
options.oneof !== OneofOption.UNIONS | ||
options.oneof !== OneofOption.UNIONS && | ||
!field.proto3Optional && | ||
field.label !== FieldDescriptorProto.Label.LABEL_OPTIONAL | ||
) { | ||
func = func.nextControlFlow('else'); | ||
func = func.addStatement( | ||
|
@@ -1208,7 +1219,12 @@ function generateFromPartial( | |
} | ||
} | ||
|
||
if (!isRepeated(field) && options.oneof !== OneofOption.UNIONS) { | ||
if ( | ||
!isRepeated(field) && | ||
options.oneof !== OneofOption.UNIONS && | ||
!field.proto3Optional && | ||
field.label !== FieldDescriptorProto.Label.LABEL_OPTIONAL | ||
) { | ||
func = func.nextControlFlow('else'); | ||
func = func.addStatement( | ||
`message.%L = %L`, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,7 +377,7 @@ export function toTypeName( | |
field: FieldDescriptorProto, | ||
options: Options | ||
): TypeName { | ||
let type = basicTypeName(typeMap, field, options, { keepValueType: false }); | ||
let type = basicTypeName(typeMap, field, options, { keepValueType: true }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
||
if (isRepeated(field)) { | ||
const mapType = detectMapType(typeMap, messageDesc, field, options); | ||
if (mapType) { | ||
|
@@ -404,7 +404,10 @@ export function toTypeName( | |
// clause, spelling each option out inside a large type union. No need for | ||
// union with `undefined` here, either. | ||
if ( | ||
(!isWithinOneOf(field) && isMessage(field) && !options.useOptionals) || | ||
(!isWithinOneOf(field) && | ||
isMessage(field) && | ||
!options.useOptionals && | ||
field.label !== FieldDescriptorProto.Label.LABEL_REQUIRED) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice. can we get a test? |
||
(isWithinOneOf(field) && options.oneof === OneofOption.PROPERTIES) || | ||
(isWithinOneOf(field) && field.proto3Optional) | ||
) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really seems like we could use an
isOptionalField
helper...