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

fix: use correct imports for optional fields #904

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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
Binary file modified integration/avoid-import-conflicts/simple.bin
Binary file not shown.
5 changes: 5 additions & 0 deletions integration/avoid-import-conflicts/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ message Simple {
simple2.Simple otherSimple = 2;
}

message DifferentSimple {
string name = 1;
optional simple2.Simple otherOptionalSimple2 = 2;
}

message SimpleEnums {
SimpleEnum local_enum = 1;
simple2.SimpleEnum import_enum = 2;
Expand Down
83 changes: 83 additions & 0 deletions integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export interface Simple {
otherSimple: Simple3 | undefined;
}

export interface DifferentSimple {
name: string;
otherOptionalSimple2?: Simple3 | undefined;
}

export interface SimpleEnums {
localEnum: SimpleEnum;
importEnum: SimpleEnum1;
Expand Down Expand Up @@ -145,6 +150,84 @@ export const Simple = {
},
};

function createBaseDifferentSimple(): DifferentSimple {
return { name: "", otherOptionalSimple2: undefined };
}

export const DifferentSimple = {
encode(message: DifferentSimple, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.name !== "") {
writer.uint32(10).string(message.name);
}
if (message.otherOptionalSimple2 !== undefined) {
Simple3.encode(message.otherOptionalSimple2, writer.uint32(18).fork()).ldelim();
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): DifferentSimple {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseDifferentSimple();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}

message.name = reader.string();
continue;
case 2:
if (tag !== 18) {
break;
}

message.otherOptionalSimple2 = Simple3.decode(reader, reader.uint32());
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): DifferentSimple {
return {
name: isSet(object.name) ? String(object.name) : "",
otherOptionalSimple2: isSet(object.otherOptionalSimple2)
? Simple3.fromJSON(object.otherOptionalSimple2)
: undefined,
};
},

toJSON(message: DifferentSimple): unknown {
const obj: any = {};
if (message.name !== "") {
obj.name = message.name;
}
if (message.otherOptionalSimple2 !== undefined) {
obj.otherOptionalSimple2 = Simple3.toJSON(message.otherOptionalSimple2);
}
return obj;
},

create<I extends Exact<DeepPartial<DifferentSimple>, I>>(base?: I): DifferentSimple {
return DifferentSimple.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<DifferentSimple>, I>>(object: I): DifferentSimple {
const message = createBaseDifferentSimple();
message.name = object.name ?? "";
message.otherOptionalSimple2 = (object.otherOptionalSimple2 !== undefined && object.otherOptionalSimple2 !== null)
? Simple3.fromPartial(object.otherOptionalSimple2)
: undefined;
return message;
},
};

function createBaseSimpleEnums(): SimpleEnums {
return { localEnum: 0, importEnum: 0 };
}
Expand Down
3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,6 @@ function generateInterfaceDeclaration(
const name = maybeSnakeToCamel(fieldDesc.name, options);
const isOptional = isOptionalProperty(fieldDesc, messageDesc.options, options);
const type = toTypeName(ctx, messageDesc, fieldDesc, isOptional);
if (isOptional && !type.toString().includes("undefined")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The definitive fix probably lays within ts-poet, I believe the toString method shouldn't have any side-effects

Copy link
Owner

Choose a reason for hiding this comment

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

Wow, I agree that is very strange that toString has side-effects. Also kinda funny this console.warn was just hanging out here 🤷 . Thanks for finding! I'll see if I can poke around at the ts-poet root cause at some point...

console.warn(name, type);
}
chunks.push(code`${maybeReadonly(options)}${name}${isOptional ? "?" : ""}: ${type}, `);
});

Expand Down
Loading