Skip to content

Commit

Permalink
fix: resolve import collisions for services (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
doochik authored Aug 8, 2022
1 parent ec6da79 commit ee0296f
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 14 deletions.
Binary file modified integration/avoid-import-conflicts/simple.bin
Binary file not shown.
11 changes: 11 additions & 0 deletions integration/avoid-import-conflicts/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

152 changes: 141 additions & 11 deletions integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -50,14 +53,22 @@ export function simpleEnumToJSON(object: SimpleEnum): string {

export interface Simple {
name: string;
otherSimple: Simple2 | undefined;
otherSimple: Simple3 | undefined;
}

export interface SimpleEnums {
localEnum: SimpleEnum;
importEnum: SimpleEnum1;
}

export interface FooServiceCreateRequest {
kind: FooService2;
}

export interface FooServiceCreateResponse {
kind: FooService2;
}

function createBaseSimple(): Simple {
return { name: '', otherSimple: undefined };
}
Expand All @@ -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;
},
Expand All @@ -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);
Expand All @@ -97,15 +108,15 @@ 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,
};
},

toJSON(message: Simple): unknown {
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;
},

Expand All @@ -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;
},
Expand Down Expand Up @@ -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;
},

Expand All @@ -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 extends Exact<DeepPartial<FooServiceCreateRequest>, 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 extends Exact<DeepPartial<FooServiceCreateResponse>, I>>(object: I): FooServiceCreateResponse {
const message = createBaseFooServiceCreateResponse();
message.kind = object.kind ?? 0;
return message;
},
};

export interface FooService {
Create(request: FooServiceCreateRequest): Promise<FooServiceCreateResponse>;
}

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<FooServiceCreateResponse> {
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<Uint8Array>;
}

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

export type DeepPartial<T> = T extends Builtin
Expand Down
Binary file modified integration/avoid-import-conflicts/simple2.bin
Binary file not shown.
6 changes: 6 additions & 0 deletions integration/avoid-import-conflicts/simple2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions integration/avoid-import-conflicts/simple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/generate-services.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -185,7 +185,7 @@ export function generateServiceClientImpl(
const { name } = serviceDesc;
const i = options.context ? `${name}<Context>` : 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<Context>' : 'Rpc';
Expand Down

0 comments on commit ee0296f

Please sign in to comment.