Skip to content

Commit

Permalink
fix: import fails when folder name overlaps with file name (#1000)
Browse files Browse the repository at this point in the history
* reproduce error

* correct the output using linked version of ts-poet

* update to latest version of ts-poet

* running bin2ts with latest ts-poet removes braces
  • Loading branch information
lukealvoeiro committed Feb 15, 2024
1 parent 14f4635 commit 1e68e6f
Show file tree
Hide file tree
Showing 18 changed files with 432 additions and 26 deletions.
Binary file not shown.
14 changes: 14 additions & 0 deletions integration/avoid-import-conflicts-folder-name/ui.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package simple2;

enum SimpleEnum {
IMPORT_DEFAULT = 0;
IMPORT_FOO = 10;
IMPORT_BAR = 11;
}

message Simple {
string simple2Name = 1;
int32 simple2Age = 2;
}

138 changes: 138 additions & 0 deletions integration/avoid-import-conflicts-folder-name/ui.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "simple2";

export enum SimpleEnum {
IMPORT_DEFAULT = 0,
IMPORT_FOO = 10,
IMPORT_BAR = 11,
UNRECOGNIZED = -1,
}

export function simpleEnumFromJSON(object: any): SimpleEnum {
switch (object) {
case 0:
case "IMPORT_DEFAULT":
return SimpleEnum.IMPORT_DEFAULT;
case 10:
case "IMPORT_FOO":
return SimpleEnum.IMPORT_FOO;
case 11:
case "IMPORT_BAR":
return SimpleEnum.IMPORT_BAR;
case -1:
case "UNRECOGNIZED":
default:
return SimpleEnum.UNRECOGNIZED;
}
}

export function simpleEnumToJSON(object: SimpleEnum): string {
switch (object) {
case SimpleEnum.IMPORT_DEFAULT:
return "IMPORT_DEFAULT";
case SimpleEnum.IMPORT_FOO:
return "IMPORT_FOO";
case SimpleEnum.IMPORT_BAR:
return "IMPORT_BAR";
case SimpleEnum.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}

export interface Simple {
simple2Name: string;
simple2Age: number;
}

function createBaseSimple(): Simple {
return { simple2Name: "", simple2Age: 0 };
}

export const Simple = {
encode(message: Simple, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.simple2Name !== "") {
writer.uint32(10).string(message.simple2Name);
}
if (message.simple2Age !== 0) {
writer.uint32(16).int32(message.simple2Age);
}
return writer;
},

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

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

message.simple2Age = reader.int32();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): Simple {
return {
simple2Name: isSet(object.simple2Name) ? globalThis.String(object.simple2Name) : "",
simple2Age: isSet(object.simple2Age) ? globalThis.Number(object.simple2Age) : 0,
};
},

toJSON(message: Simple): unknown {
const obj: any = {};
if (message.simple2Name !== "") {
obj.simple2Name = message.simple2Name;
}
if (message.simple2Age !== 0) {
obj.simple2Age = Math.round(message.simple2Age);
}
return obj;
},

create<I extends Exact<DeepPartial<Simple>, I>>(base?: I): Simple {
return Simple.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Simple>, I>>(object: I): Simple {
const message = createBaseSimple();
message.simple2Name = object.simple2Name ?? "";
message.simple2Age = object.simple2Age ?? 0;
return message;
},
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SimpleEnums } from "./simple";

describe("avoid-import-conflicts-folder-name", () => {
it("compiles", () => {
expect(SimpleEnums).not.toBeUndefined();
});
});
Binary file not shown.
22 changes: 22 additions & 0 deletions integration/avoid-import-conflicts-folder-name/ui/simple.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";
package simple;

import "ui.proto";

enum SimpleEnum {
LOCAL_DEFAULT = 0;
LOCAL_FOO = 1;
LOCAL_BAR = 2;
}

message Simple {
string name = 1;
simple2.Simple otherSimple = 2;
}

message SimpleEnums {
SimpleEnum local_enum = 1;
simple2.SimpleEnum import_enum = 2;
}


Loading

0 comments on commit 1e68e6f

Please sign in to comment.