Skip to content

Commit

Permalink
fix: fixed addGrpcMetadata option (#761)
Browse files Browse the repository at this point in the history
This PR should fix this issue
#188

I've removed `addNestjsRestParameter` parameter completely from
`src/generate-services.ts` since `NestJS` seems to be handled in
`generate-nestjs` file.

- [x] tests
  • Loading branch information
vojty authored Mar 13, 2024
1 parent a6d38ac commit cb2b573
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 11 deletions.
3 changes: 3 additions & 0 deletions integration/default-with-custom-metadata/custom-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Foo {
foo: "bar";
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package basic;

message GetBasicRequest {
string name = 1;
}

message GetBasicResponse {
string name = 1;
}

service BasicService {
rpc GetBasic (GetBasicRequest) returns (GetBasicResponse) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";
import { Foo } from "./custom-metadata";

export const protobufPackage = "basic";

export interface GetBasicRequest {
name: string;
}

export interface GetBasicResponse {
name: string;
}

function createBaseGetBasicRequest(): GetBasicRequest {
return { name: "" };
}

export const GetBasicRequest = {
encode(message: GetBasicRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.name !== "") {
writer.uint32(10).string(message.name);
}
return writer;
},

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

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

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

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

create<I extends Exact<DeepPartial<GetBasicRequest>, I>>(base?: I): GetBasicRequest {
return GetBasicRequest.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<GetBasicRequest>, I>>(object: I): GetBasicRequest {
const message = createBaseGetBasicRequest();
message.name = object.name ?? "";
return message;
},
};

function createBaseGetBasicResponse(): GetBasicResponse {
return { name: "" };
}

export const GetBasicResponse = {
encode(message: GetBasicResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.name !== "") {
writer.uint32(10).string(message.name);
}
return writer;
},

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

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

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

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

create<I extends Exact<DeepPartial<GetBasicResponse>, I>>(base?: I): GetBasicResponse {
return GetBasicResponse.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<GetBasicResponse>, I>>(object: I): GetBasicResponse {
const message = createBaseGetBasicResponse();
message.name = object.name ?? "";
return message;
},
};

export interface BasicService {
GetBasic(request: GetBasicRequest, metadata?: Foo): Promise<GetBasicResponse>;
}

export const BasicServiceServiceName = "basic.BasicService";
export class BasicServiceClientImpl implements BasicService {
private readonly rpc: Rpc;
private readonly service: string;
constructor(rpc: Rpc, opts?: { service?: string }) {
this.service = opts?.service || BasicServiceServiceName;
this.rpc = rpc;
this.GetBasic = this.GetBasic.bind(this);
}
GetBasic(request: GetBasicRequest, metadata?: Foo): Promise<GetBasicResponse> {
const data = GetBasicRequest.encode(request).finish();
const promise = this.rpc.request(this.service, "GetBasic", data, metadata);
return promise.then((data) => GetBasicResponse.decode(_m0.Reader.create(data)));
}
}

interface Rpc {
request(service: string, method: string, data: Uint8Array, metadata?: Foo): Promise<Uint8Array>;
}

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;
}
1 change: 1 addition & 0 deletions integration/default-with-custom-metadata/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
metadataType=Foo@./custom-metadata,outputServices=default,addGrpcMetadata=true
Binary file not shown.
14 changes: 14 additions & 0 deletions integration/default-with-metadata/default-with-metadata.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package basic;

message GetBasicRequest {
string name = 1;
}

message GetBasicResponse {
string name = 1;
}

service BasicService {
rpc GetBasic (GetBasicRequest) returns (GetBasicResponse) {}
}
167 changes: 167 additions & 0 deletions integration/default-with-metadata/default-with-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* eslint-disable */
import { Metadata } from "@grpc/grpc-js";
import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "basic";

export interface GetBasicRequest {
name: string;
}

export interface GetBasicResponse {
name: string;
}

function createBaseGetBasicRequest(): GetBasicRequest {
return { name: "" };
}

export const GetBasicRequest = {
encode(message: GetBasicRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.name !== "") {
writer.uint32(10).string(message.name);
}
return writer;
},

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

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

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

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

create<I extends Exact<DeepPartial<GetBasicRequest>, I>>(base?: I): GetBasicRequest {
return GetBasicRequest.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<GetBasicRequest>, I>>(object: I): GetBasicRequest {
const message = createBaseGetBasicRequest();
message.name = object.name ?? "";
return message;
},
};

function createBaseGetBasicResponse(): GetBasicResponse {
return { name: "" };
}

export const GetBasicResponse = {
encode(message: GetBasicResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.name !== "") {
writer.uint32(10).string(message.name);
}
return writer;
},

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

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

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

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

create<I extends Exact<DeepPartial<GetBasicResponse>, I>>(base?: I): GetBasicResponse {
return GetBasicResponse.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<GetBasicResponse>, I>>(object: I): GetBasicResponse {
const message = createBaseGetBasicResponse();
message.name = object.name ?? "";
return message;
},
};

export interface BasicService {
GetBasic(request: GetBasicRequest, metadata?: Metadata): Promise<GetBasicResponse>;
}

export const BasicServiceServiceName = "basic.BasicService";
export class BasicServiceClientImpl implements BasicService {
private readonly rpc: Rpc;
private readonly service: string;
constructor(rpc: Rpc, opts?: { service?: string }) {
this.service = opts?.service || BasicServiceServiceName;
this.rpc = rpc;
this.GetBasic = this.GetBasic.bind(this);
}
GetBasic(request: GetBasicRequest, metadata?: Metadata): Promise<GetBasicResponse> {
const data = GetBasicRequest.encode(request).finish();
const promise = this.rpc.request(this.service, "GetBasic", data, metadata);
return promise.then((data) => GetBasicResponse.decode(_m0.Reader.create(data)));
}
}

interface Rpc {
request(service: string, method: string, data: Uint8Array, metadata?: Metadata): Promise<Uint8Array>;
}

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;
}
1 change: 1 addition & 0 deletions integration/default-with-metadata/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputServices=default,addGrpcMetadata=true
Loading

0 comments on commit cb2b573

Please sign in to comment.