diff --git a/integration/test-1110/parameters.txt b/integration/test-1110/parameters.txt new file mode 100644 index 000000000..97bb08ab5 --- /dev/null +++ b/integration/test-1110/parameters.txt @@ -0,0 +1 @@ +useExactTypes=false diff --git a/integration/test-1110/test-1110-test.ts b/integration/test-1110/test-1110-test.ts new file mode 100644 index 000000000..b1991f3f2 --- /dev/null +++ b/integration/test-1110/test-1110-test.ts @@ -0,0 +1,8 @@ +import { UserRule } 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(); + }); +}); diff --git a/integration/test-1110/test-1110.proto b/integration/test-1110/test-1110.proto new file mode 100644 index 000000000..ee5b6ae82 --- /dev/null +++ b/integration/test-1110/test-1110.proto @@ -0,0 +1,29 @@ +syntax = "proto3"; + +message UserRule { + // just some fake rule for testing + string UUID = 1 [json_name = "uuid"]; +} + +message Object { + string namespace = 1; + string name = 2; +} + +message Boolean { + string namespace = 1; + string name = 2; +} + +message Symbol { + string namespace = 1; + string name = 2; +} + +message Function { + string namespace = 1; + string name = 2; +} + +service APIService { +} diff --git a/integration/test-1110/test-1110.ts b/integration/test-1110/test-1110.ts new file mode 100644 index 000000000..51c2baa1e --- /dev/null +++ b/integration/test-1110/test-1110.ts @@ -0,0 +1,432 @@ +// Code generated by protoc-gen-ts_proto. DO NOT EDIT. +// source: test-1110.proto + +/* eslint-disable */ +import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire"; + +export const protobufPackage = ""; + +export interface UserRule { + /** just some fake rule for testing */ + UUID: string; +} + +export interface Object { + namespace: string; + name: string; +} + +export interface Boolean { + namespace: string; + name: string; +} + +export interface Symbol { + namespace: string; + name: string; +} + +export interface FunctionMessage { + namespace: string; + name: string; +} + +function createBaseUserRule(): UserRule { + return { UUID: "" }; +} + +export const UserRule: MessageFns = { + encode(message: UserRule, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.UUID !== "") { + writer.uint32(10).string(message.UUID); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): UserRule { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseUserRule(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 10) { + break; + } + + message.UUID = reader.string(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): UserRule { + return { UUID: isSet(object.uuid) ? globalThis.String(object.uuid) : "" }; + }, + + toJSON(message: UserRule): unknown { + const obj: any = {}; + if (message.UUID !== "") { + obj.uuid = message.UUID; + } + return obj; + }, + + create(base?: DeepPartial): UserRule { + return UserRule.fromPartial(base ?? {}); + }, + fromPartial(object: DeepPartial): UserRule { + const message = createBaseUserRule(); + message.UUID = object.UUID ?? ""; + return message; + }, +}; + +function createBaseObject(): Object { + return { namespace: "", name: "" }; +} + +export const Object: MessageFns = { + encode(message: Object, 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): Object { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseObject(); + 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): Object { + return { + namespace: isSet(object.namespace) ? globalThis.String(object.namespace) : "", + name: isSet(object.name) ? globalThis.String(object.name) : "", + }; + }, + + toJSON(message: Object): unknown { + const obj: any = {}; + if (message.namespace !== "") { + obj.namespace = message.namespace; + } + if (message.name !== "") { + obj.name = message.name; + } + return obj; + }, + + create(base?: DeepPartial): Object { + return Object.fromPartial(base ?? {}); + }, + fromPartial(object: DeepPartial): Object { + const message = createBaseObject(); + message.namespace = object.namespace ?? ""; + message.name = object.name ?? ""; + return message; + }, +}; + +function createBaseBoolean(): Boolean { + return { namespace: "", name: "" }; +} + +export const Boolean: MessageFns = { + encode(message: Boolean, 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): Boolean { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseBoolean(); + 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): Boolean { + return { + namespace: isSet(object.namespace) ? globalThis.String(object.namespace) : "", + name: isSet(object.name) ? globalThis.String(object.name) : "", + }; + }, + + toJSON(message: Boolean): unknown { + const obj: any = {}; + if (message.namespace !== "") { + obj.namespace = message.namespace; + } + if (message.name !== "") { + obj.name = message.name; + } + return obj; + }, + + create(base?: DeepPartial): Boolean { + return Boolean.fromPartial(base ?? {}); + }, + fromPartial(object: DeepPartial): Boolean { + const message = createBaseBoolean(); + message.namespace = object.namespace ?? ""; + message.name = object.name ?? ""; + return message; + }, +}; + +function createBaseSymbol(): Symbol { + return { namespace: "", name: "" }; +} + +export const Symbol: MessageFns = { + encode(message: Symbol, 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): Symbol { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseSymbol(); + 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): Symbol { + return { + namespace: isSet(object.namespace) ? globalThis.String(object.namespace) : "", + name: isSet(object.name) ? globalThis.String(object.name) : "", + }; + }, + + toJSON(message: Symbol): unknown { + const obj: any = {}; + if (message.namespace !== "") { + obj.namespace = message.namespace; + } + if (message.name !== "") { + obj.name = message.name; + } + return obj; + }, + + create(base?: DeepPartial): Symbol { + return Symbol.fromPartial(base ?? {}); + }, + fromPartial(object: DeepPartial): Symbol { + const message = createBaseSymbol(); + message.namespace = object.namespace ?? ""; + message.name = object.name ?? ""; + return message; + }, +}; + +function createBaseFunctionMessage(): FunctionMessage { + return { namespace: "", name: "" }; +} + +export const FunctionMessage: MessageFns = { + encode(message: FunctionMessage, 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): FunctionMessage { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseFunctionMessage(); + 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): FunctionMessage { + return { + namespace: isSet(object.namespace) ? globalThis.String(object.namespace) : "", + name: isSet(object.name) ? globalThis.String(object.name) : "", + }; + }, + + toJSON(message: FunctionMessage): unknown { + const obj: any = {}; + if (message.namespace !== "") { + obj.namespace = message.namespace; + } + if (message.name !== "") { + obj.name = message.name; + } + return obj; + }, + + create(base?: DeepPartial): FunctionMessage { + return FunctionMessage.fromPartial(base ?? {}); + }, + fromPartial(object: DeepPartial): FunctionMessage { + const message = createBaseFunctionMessage(); + message.namespace = object.namespace ?? ""; + message.name = object.name ?? ""; + return message; + }, +}; + +export interface APIService { +} + +export const APIServiceServiceName = "APIService"; +export class APIServiceClientImpl implements APIService { + private readonly rpc: Rpc; + private readonly service: string; + constructor(rpc: Rpc, opts?: { service?: string }) { + this.service = opts?.service || APIServiceServiceName; + this.rpc = rpc; + } +} + +interface Rpc { + request(service: string, method: string, data: Uint8Array): Promise; +} + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin ? T + : T extends globalThis.Array ? globalThis.Array> + : T extends ReadonlyArray ? ReadonlyArray> + : T extends {} ? { [K in keyof T]?: DeepPartial } + : Partial; + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} + +export interface MessageFns { + encode(message: T, writer?: BinaryWriter): BinaryWriter; + decode(input: BinaryReader | Uint8Array, length?: number): T; + fromJSON(object: any): T; + toJSON(message: T): unknown; + create(base?: DeepPartial): T; + fromPartial(object: DeepPartial): T; +} diff --git a/src/visit.ts b/src/visit.ts index 8ae055be6..a4f3f9730 100644 --- a/src/visit.ts +++ b/src/visit.ts @@ -60,7 +60,7 @@ export function visit( }); } -const builtInNames = ["Date"]; +const builtInNames = ["Date", "Function"]; /** Potentially suffixes `Message` to names to avoid conflicts, i.e. with `Date`. */ function messageName(message: DescriptorProto): string {