diff --git a/integration/avoid-import-conflicts/simple.bin b/integration/avoid-import-conflicts/simple.bin index 57bd5482b..c2bbee56f 100644 Binary files a/integration/avoid-import-conflicts/simple.bin and b/integration/avoid-import-conflicts/simple.bin differ diff --git a/integration/avoid-import-conflicts/simple.proto b/integration/avoid-import-conflicts/simple.proto index 5f6778ff4..bd0886204 100644 --- a/integration/avoid-import-conflicts/simple.proto +++ b/integration/avoid-import-conflicts/simple.proto @@ -18,4 +18,15 @@ message SimpleEnums { simple2.SimpleEnum import_enum = 2; } +message FooServiceCreateRequest { + simple2.FooService kind = 1; +} + +message FooServiceCreateResponse { + simple2.FooService kind = 1; +} + +service FooService { + rpc Create (FooServiceCreateRequest) returns (FooServiceCreateResponse); +} diff --git a/integration/avoid-import-conflicts/simple.ts b/integration/avoid-import-conflicts/simple.ts index 453ccd807..f55c88f31 100644 --- a/integration/avoid-import-conflicts/simple.ts +++ b/integration/avoid-import-conflicts/simple.ts @@ -1,9 +1,12 @@ /* eslint-disable */ import { SimpleEnum as SimpleEnum1, - Simple as Simple2, - simpleEnumFromJSON as simpleEnumFromJSON3, - simpleEnumToJSON as simpleEnumToJSON4, + FooService as FooService2, + Simple as Simple3, + simpleEnumFromJSON as simpleEnumFromJSON4, + simpleEnumToJSON as simpleEnumToJSON5, + fooServiceFromJSON, + fooServiceToJSON, } from './simple2'; import * as _m0 from 'protobufjs/minimal'; @@ -50,7 +53,7 @@ export function simpleEnumToJSON(object: SimpleEnum): string { export interface Simple { name: string; - otherSimple: Simple2 | undefined; + otherSimple: Simple3 | undefined; } export interface SimpleEnums { @@ -58,6 +61,14 @@ export interface SimpleEnums { importEnum: SimpleEnum1; } +export interface FooServiceCreateRequest { + kind: FooService2; +} + +export interface FooServiceCreateResponse { + kind: FooService2; +} + function createBaseSimple(): Simple { return { name: '', otherSimple: undefined }; } @@ -68,7 +79,7 @@ export const Simple = { writer.uint32(10).string(message.name); } if (message.otherSimple !== undefined) { - Simple2.encode(message.otherSimple, writer.uint32(18).fork()).ldelim(); + Simple3.encode(message.otherSimple, writer.uint32(18).fork()).ldelim(); } return writer; }, @@ -84,7 +95,7 @@ export const Simple = { message.name = reader.string(); break; case 2: - message.otherSimple = Simple2.decode(reader, reader.uint32()); + message.otherSimple = Simple3.decode(reader, reader.uint32()); break; default: reader.skipType(tag & 7); @@ -97,7 +108,7 @@ export const Simple = { fromJSON(object: any): Simple { return { name: isSet(object.name) ? String(object.name) : '', - otherSimple: isSet(object.otherSimple) ? Simple2.fromJSON(object.otherSimple) : undefined, + otherSimple: isSet(object.otherSimple) ? Simple3.fromJSON(object.otherSimple) : undefined, }; }, @@ -105,7 +116,7 @@ export const Simple = { const obj: any = {}; message.name !== undefined && (obj.name = message.name); message.otherSimple !== undefined && - (obj.otherSimple = message.otherSimple ? Simple2.toJSON(message.otherSimple) : undefined); + (obj.otherSimple = message.otherSimple ? Simple3.toJSON(message.otherSimple) : undefined); return obj; }, @@ -114,7 +125,7 @@ export const Simple = { message.name = object.name ?? ''; message.otherSimple = object.otherSimple !== undefined && object.otherSimple !== null - ? Simple2.fromPartial(object.otherSimple) + ? Simple3.fromPartial(object.otherSimple) : undefined; return message; }, @@ -159,14 +170,14 @@ export const SimpleEnums = { fromJSON(object: any): SimpleEnums { return { localEnum: isSet(object.localEnum) ? simpleEnumFromJSON(object.localEnum) : 0, - importEnum: isSet(object.importEnum) ? simpleEnumFromJSON3(object.importEnum) : 0, + importEnum: isSet(object.importEnum) ? simpleEnumFromJSON4(object.importEnum) : 0, }; }, toJSON(message: SimpleEnums): unknown { const obj: any = {}; message.localEnum !== undefined && (obj.localEnum = simpleEnumToJSON(message.localEnum)); - message.importEnum !== undefined && (obj.importEnum = simpleEnumToJSON4(message.importEnum)); + message.importEnum !== undefined && (obj.importEnum = simpleEnumToJSON5(message.importEnum)); return obj; }, @@ -178,6 +189,125 @@ export const SimpleEnums = { }, }; +function createBaseFooServiceCreateRequest(): FooServiceCreateRequest { + return { kind: 0 }; +} + +export const FooServiceCreateRequest = { + encode(message: FooServiceCreateRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { + if (message.kind !== 0) { + writer.uint32(8).int32(message.kind); + } + return writer; + }, + + decode(input: _m0.Reader | Uint8Array, length?: number): FooServiceCreateRequest { + const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseFooServiceCreateRequest(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.kind = reader.int32() as any; + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): FooServiceCreateRequest { + return { + kind: isSet(object.kind) ? fooServiceFromJSON(object.kind) : 0, + }; + }, + + toJSON(message: FooServiceCreateRequest): unknown { + const obj: any = {}; + message.kind !== undefined && (obj.kind = fooServiceToJSON(message.kind)); + return obj; + }, + + fromPartial, I>>(object: I): FooServiceCreateRequest { + const message = createBaseFooServiceCreateRequest(); + message.kind = object.kind ?? 0; + return message; + }, +}; + +function createBaseFooServiceCreateResponse(): FooServiceCreateResponse { + return { kind: 0 }; +} + +export const FooServiceCreateResponse = { + encode(message: FooServiceCreateResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { + if (message.kind !== 0) { + writer.uint32(8).int32(message.kind); + } + return writer; + }, + + decode(input: _m0.Reader | Uint8Array, length?: number): FooServiceCreateResponse { + const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseFooServiceCreateResponse(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.kind = reader.int32() as any; + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): FooServiceCreateResponse { + return { + kind: isSet(object.kind) ? fooServiceFromJSON(object.kind) : 0, + }; + }, + + toJSON(message: FooServiceCreateResponse): unknown { + const obj: any = {}; + message.kind !== undefined && (obj.kind = fooServiceToJSON(message.kind)); + return obj; + }, + + fromPartial, I>>(object: I): FooServiceCreateResponse { + const message = createBaseFooServiceCreateResponse(); + message.kind = object.kind ?? 0; + return message; + }, +}; + +export interface FooService { + Create(request: FooServiceCreateRequest): Promise; +} + +export class FooServiceClientImpl implements FooService { + private readonly rpc: Rpc; + constructor(rpc: Rpc) { + this.rpc = rpc; + this.Create = this.Create.bind(this); + } + Create(request: FooServiceCreateRequest): Promise { + const data = FooServiceCreateRequest.encode(request).finish(); + const promise = this.rpc.request('simple.FooService', 'Create', data); + return promise.then((data) => FooServiceCreateResponse.decode(new _m0.Reader(data))); + } +} + +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 diff --git a/integration/avoid-import-conflicts/simple2.bin b/integration/avoid-import-conflicts/simple2.bin index 7b35fb39e..f0d78273c 100644 Binary files a/integration/avoid-import-conflicts/simple2.bin and b/integration/avoid-import-conflicts/simple2.bin differ diff --git a/integration/avoid-import-conflicts/simple2.proto b/integration/avoid-import-conflicts/simple2.proto index 2072c0f56..4d087f923 100644 --- a/integration/avoid-import-conflicts/simple2.proto +++ b/integration/avoid-import-conflicts/simple2.proto @@ -7,6 +7,12 @@ enum SimpleEnum { IMPORT_BAR = 11; } +enum FooService { + FOO_SERVICE_DEFAULT = 0; + FOO_SERVICE_FOO = 1; + FOO_SERVICE_BAR = 2; +} + message Simple { string name = 1; int32 age = 2; diff --git a/integration/avoid-import-conflicts/simple2.ts b/integration/avoid-import-conflicts/simple2.ts index 1c75e2fca..6fb2b40f6 100644 --- a/integration/avoid-import-conflicts/simple2.ts +++ b/integration/avoid-import-conflicts/simple2.ts @@ -42,6 +42,45 @@ export function simpleEnumToJSON(object: SimpleEnum): string { } } +export enum FooService { + FOO_SERVICE_DEFAULT = 0, + FOO_SERVICE_FOO = 1, + FOO_SERVICE_BAR = 2, + UNRECOGNIZED = -1, +} + +export function fooServiceFromJSON(object: any): FooService { + switch (object) { + case 0: + case 'FOO_SERVICE_DEFAULT': + return FooService.FOO_SERVICE_DEFAULT; + case 1: + case 'FOO_SERVICE_FOO': + return FooService.FOO_SERVICE_FOO; + case 2: + case 'FOO_SERVICE_BAR': + return FooService.FOO_SERVICE_BAR; + case -1: + case 'UNRECOGNIZED': + default: + return FooService.UNRECOGNIZED; + } +} + +export function fooServiceToJSON(object: FooService): string { + switch (object) { + case FooService.FOO_SERVICE_DEFAULT: + return 'FOO_SERVICE_DEFAULT'; + case FooService.FOO_SERVICE_FOO: + return 'FOO_SERVICE_FOO'; + case FooService.FOO_SERVICE_BAR: + return 'FOO_SERVICE_BAR'; + case FooService.UNRECOGNIZED: + default: + return 'UNRECOGNIZED'; + } +} + export interface Simple { name: string; age: number; diff --git a/src/generate-services.ts b/src/generate-services.ts index 1f4281150..42c2b04ea 100644 --- a/src/generate-services.ts +++ b/src/generate-services.ts @@ -1,5 +1,5 @@ import { MethodDescriptorProto, FileDescriptorProto, ServiceDescriptorProto } from 'ts-proto-descriptors'; -import { Code, code, imp, joinCode } from 'ts-poet'; +import { Code, code, def, imp, joinCode } from 'ts-poet'; import { BatchMethod, detectBatchMethod, @@ -45,7 +45,7 @@ export function generateService( maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated); const maybeTypeVar = options.context ? `<${contextTypeVar}>` : ''; - chunks.push(code`export interface ${serviceDesc.name}${maybeTypeVar} {`); + chunks.push(code`export interface ${def(serviceDesc.name)}${maybeTypeVar} {`); serviceDesc.method.forEach((methodDesc, index) => { assertInstanceOf(methodDesc, FormattedMethodDescriptor); @@ -185,7 +185,7 @@ export function generateServiceClientImpl( const { name } = serviceDesc; const i = options.context ? `${name}` : name; const t = options.context ? `<${contextTypeVar}>` : ''; - chunks.push(code`export class ${name}ClientImpl${t} implements ${i} {`); + chunks.push(code`export class ${name}ClientImpl${t} implements ${def(i)} {`); // Create the constructor(rpc: Rpc) const rpcType = options.context ? 'Rpc' : 'Rpc';