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: Unbreak a use case for #1110 / fix for #1121 #1123

Merged
merged 1 commit into from
Oct 15, 2024
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
7 changes: 6 additions & 1 deletion integration/test-1110/test-1110-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { UserRule } from "./test-1110";
import { UserRule, Nested_Function } from "./test-1110";

describe("test-1110", () => {
it("Able to create a partial user rule with reserved word messages", () => {
const simple: UserRule = UserRule.fromPartial({ UUID: "foo" });
expect(simple).toBeTruthy();
});

it("built in handling should only be done to top level", () => {
const nestedFunction: Nested_Function = Nested_Function.fromPartial({});
expect(nestedFunction).toBeTruthy();
});
});
7 changes: 7 additions & 0 deletions integration/test-1110/test-1110.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@ message Function {
string name = 2;
}

message Nested {
message Function {
string namespace = 1;
string name = 2;
}
}

service APIService {
}
127 changes: 127 additions & 0 deletions integration/test-1110/test-1110.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export interface FunctionMessage {
name: string;
}

export interface Nested {
}

export interface Nested_Function {
namespace: string;
name: string;
}

function createBaseUserRule(): UserRule {
return { UUID: "" };
}
Expand Down Expand Up @@ -393,6 +401,125 @@ export const FunctionMessage: MessageFns<FunctionMessage> = {
},
};

function createBaseNested(): Nested {
return {};
}

export const Nested: MessageFns<Nested> = {
encode(_: Nested, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
return writer;
},

decode(input: BinaryReader | Uint8Array, length?: number): Nested {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseNested();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skip(tag & 7);
}
return message;
},

fromJSON(_: any): Nested {
return {};
},

toJSON(_: Nested): unknown {
const obj: any = {};
return obj;
},

create(base?: DeepPartial<Nested>): Nested {
return Nested.fromPartial(base ?? {});
},
fromPartial(_: DeepPartial<Nested>): Nested {
const message = createBaseNested();
return message;
},
};

function createBaseNested_Function(): Nested_Function {
return { namespace: "", name: "" };
}

export const Nested_Function: MessageFns<Nested_Function> = {
encode(message: Nested_Function, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
if (message.namespace !== "") {
writer.uint32(10).string(message.namespace);
}
if (message.name !== "") {
writer.uint32(18).string(message.name);
}
return writer;
},

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

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

message.name = reader.string();
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skip(tag & 7);
}
return message;
},

fromJSON(object: any): Nested_Function {
return {
namespace: isSet(object.namespace) ? globalThis.String(object.namespace) : "",
name: isSet(object.name) ? globalThis.String(object.name) : "",
};
},

toJSON(message: Nested_Function): unknown {
const obj: any = {};
if (message.namespace !== "") {
obj.namespace = message.namespace;
}
if (message.name !== "") {
obj.name = message.name;
}
return obj;
},

create(base?: DeepPartial<Nested_Function>): Nested_Function {
return Nested_Function.fromPartial(base ?? {});
},
fromPartial(object: DeepPartial<Nested_Function>): Nested_Function {
const message = createBaseNested_Function();
message.namespace = object.namespace ?? "";
message.name = object.name ?? "";
return message;
},
};

export interface APIService {
}

Expand Down
9 changes: 4 additions & 5 deletions src/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function visit(
const protoFullName = protoPrefix + enumDesc.name;
// I.e. FooBar_ZazInner
const tsFullName = tsPrefix + maybeSnakeToCamel(enumDesc.name, options);
const tsFullNameWithAffixes = `${options.typePrefix}${tsFullName}${options.typeSuffix}`;
const tsFullNameWithAffixes = messageName(`${options.typePrefix}${tsFullName}${options.typeSuffix}`);
const nestedSourceInfo = sourceInfo.open(childEnumType, index);
enumFn(tsFullNameWithAffixes, enumDesc, nestedSourceInfo, protoFullName);
});
Expand All @@ -51,8 +51,8 @@ export function visit(
// I.e. Foo_Bar.Zaz_Inner
const protoFullName = protoPrefix + message.name;
// I.e. FooBar_ZazInner
const tsFullName = tsPrefix + maybeSnakeToCamel(messageName(message), options);
const tsFullNameWithAffixes = `${options.typePrefix}${tsFullName}${options.typeSuffix}`;
const tsFullName = tsPrefix + maybeSnakeToCamel(message.name, options);
const tsFullNameWithAffixes = messageName(`${options.typePrefix}${tsFullName}${options.typeSuffix}`);
Copy link
Owner

Choose a reason for hiding this comment

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

Nice! Great fix to just move where we check the builtInNames

const nestedSourceInfo = sourceInfo.open(childType, index);
messageFn(tsFullNameWithAffixes, message, nestedSourceInfo, protoFullName);
const delim = options.useSnakeTypeName ? "_" : "";
Expand All @@ -63,8 +63,7 @@ export function visit(
const builtInNames = ["Date", "Function"];

/** Potentially suffixes `Message` to names to avoid conflicts, i.e. with `Date`. */
function messageName(message: DescriptorProto): string {
const { name } = message;
function messageName(name: string): string {
return builtInNames.includes(name) ? `${name}Message` : name;
}

Expand Down
Loading