Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration/simple-proto2/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ enum EnumWithoutZero {

message Issue56 {
required EnumWithoutZero test = 1;
optional int32 foo = 2;
}
16 changes: 16 additions & 0 deletions integration/simple-proto2/simple.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable */
import { Writer, Reader } from 'protobufjs/minimal';


export interface Issue56 {
test: EnumWithoutZero;
foo?: number;
}

const baseIssue56: object = {
test: 1,
foo: 0,
};

export const protobufPackage = 'simple'
Expand Down Expand Up @@ -46,6 +49,9 @@ export function enumWithoutZeroToJSON(object: EnumWithoutZero): string {
export const Issue56 = {
encode(message: Issue56, writer: Writer = Writer.create()): Writer {
writer.uint32(8).int32(message.test);
if (message.foo !== undefined) {
writer.uint32(16).int32(message.foo);
}
return writer;
},
decode(input: Uint8Array | Reader, length?: number): Issue56 {
Expand All @@ -58,6 +64,9 @@ export const Issue56 = {
case 1:
message.test = reader.int32() as any;
break;
case 2:
message.foo = reader.int32();
break;
default:
reader.skipType(tag & 7);
break;
Expand All @@ -72,6 +81,9 @@ export const Issue56 = {
} else {
message.test = 1;
}
if (object.foo !== undefined && object.foo !== null) {
message.foo = Number(object.foo);
}
return message;
},
fromPartial(object: DeepPartial<Issue56>): Issue56 {
Expand All @@ -81,11 +93,15 @@ export const Issue56 = {
} else {
message.test = 1;
}
if (object.foo !== undefined && object.foo !== null) {
message.foo = object.foo;
}
return message;
},
toJSON(message: Issue56): unknown {
const obj: any = {};
message.test !== undefined && (obj.test = enumWithoutZeroToJSON(message.test));
message.foo !== undefined && (obj.foo = message.foo);
return obj;
},
};
Expand Down
22 changes: 19 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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...

);
}

// Create the interface with properties
Expand Down Expand Up @@ -847,6 +851,11 @@ function generateEncode(
)
.addStatement('%L', writeSnippet(`message.${fieldName}`))
.endControlFlow();
} else if (field.label === FieldDescriptorProto.Label.LABEL_OPTIONAL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is proto3Optional handled in this if/elseif/.../else block? Should that not also be handled here?

func = func
.beginControlFlow('if (message.%L !== undefined)', fieldName)
.addStatement('%L', writeSnippet(`message.${fieldName}`))
.endControlFlow();
} else {
func = func.addStatement('%L', writeSnippet(`message.${fieldName}`));
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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`,
Expand Down
7 changes: 5 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Expand All @@ -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) ||
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
) {
Expand Down