Skip to content

Commit

Permalink
feat: Allow comments to be omitted
Browse files Browse the repository at this point in the history
  • Loading branch information
haines committed Jan 22, 2024
1 parent 3cd9903 commit 1b0f802
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 30 deletions.
4 changes: 3 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,9 @@ Generated code will be placed in the Gradle build directory.

This will disable `exportCommonSymbols` to avoid name collisions on the common symbols.

- With `--emitDefaultValues=json-methods`, the generated toJSON method will emit scalars like `0` and `""` as json fields.
- With `--ts_proto_opt=emitDefaultValues=json-methods`, the generated toJSON method will emit scalars like `0` and `""` as json fields.

- With `--ts_proto_opt=comments=false`, comments won't be copied from the proto files to the generated code.

### NestJS Support

Expand Down
1 change: 1 addition & 0 deletions integration/no-comments/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comments=false
Binary file added integration/no-comments/simple.bin
Binary file not shown.
23 changes: 23 additions & 0 deletions integration/no-comments/simple.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Adding a comment to the syntax will become the first
// comment in the output source file.
syntax = "proto3";

package simple;

// This comment is separated by a blank non-comment line, and will detach from
// the following comment on the message Simple.

/** Example comment on the Simple message */
message Simple {
// Name field
string name = 1 [deprecated = true];
/** Age field */
int32 age = 2 [deprecated = true];
Child child = 3 [deprecated = true]; // This comment will also attach;
string test_field = 4 [deprecated = true];
string test_not_deprecated = 5 [deprecated = false];
}

message Child {
string name = 1;
}
208 changes: 208 additions & 0 deletions integration/no-comments/simple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "simple";

export interface Simple {
name: string;
age: number;
child: Child | undefined;
testField: string;
testNotDeprecated: string;
}

export interface Child {
name: string;
}

function createBaseSimple(): Simple {
return { name: "", age: 0, child: undefined, testField: "", testNotDeprecated: "" };
}

export const Simple = {
encode(message: Simple, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.name !== "") {
writer.uint32(10).string(message.name);
}
if (message.age !== 0) {
writer.uint32(16).int32(message.age);
}
if (message.child !== undefined) {
Child.encode(message.child, writer.uint32(26).fork()).ldelim();
}
if (message.testField !== "") {
writer.uint32(34).string(message.testField);
}
if (message.testNotDeprecated !== "") {
writer.uint32(42).string(message.testNotDeprecated);
}
return writer;
},

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

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

message.age = reader.int32();
continue;
case 3:
if (tag !== 26) {
break;
}

message.child = Child.decode(reader, reader.uint32());
continue;
case 4:
if (tag !== 34) {
break;
}

message.testField = reader.string();
continue;
case 5:
if (tag !== 42) {
break;
}

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

fromJSON(object: any): Simple {
return {
name: isSet(object.name) ? globalThis.String(object.name) : "",
age: isSet(object.age) ? globalThis.Number(object.age) : 0,
child: isSet(object.child) ? Child.fromJSON(object.child) : undefined,
testField: isSet(object.testField) ? globalThis.String(object.testField) : "",
testNotDeprecated: isSet(object.testNotDeprecated) ? globalThis.String(object.testNotDeprecated) : "",
};
},

toJSON(message: Simple): unknown {
const obj: any = {};
if (message.name !== "") {
obj.name = message.name;
}
if (message.age !== 0) {
obj.age = Math.round(message.age);
}
if (message.child !== undefined) {
obj.child = Child.toJSON(message.child);
}
if (message.testField !== "") {
obj.testField = message.testField;
}
if (message.testNotDeprecated !== "") {
obj.testNotDeprecated = message.testNotDeprecated;
}
return obj;
},

create<I extends Exact<DeepPartial<Simple>, I>>(base?: I): Simple {
return Simple.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Simple>, I>>(object: I): Simple {
const message = createBaseSimple();
message.name = object.name ?? "";
message.age = object.age ?? 0;
message.child = (object.child !== undefined && object.child !== null) ? Child.fromPartial(object.child) : undefined;
message.testField = object.testField ?? "";
message.testNotDeprecated = object.testNotDeprecated ?? "";
return message;
},
};

function createBaseChild(): Child {
return { name: "" };
}

export const Child = {
encode(message: Child, 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): Child {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseChild();
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): Child {
return { name: isSet(object.name) ? globalThis.String(object.name) : "" };
},

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

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

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;
}
4 changes: 2 additions & 2 deletions src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function generateEnum(
const chunks: Code[] = [];
let unrecognizedEnum: UnrecognizedEnum = { present: false };

maybeAddComment(sourceInfo, chunks, enumDesc.options?.deprecated);
maybeAddComment(options, sourceInfo, chunks, enumDesc.options?.deprecated);

if (options.enumsAsLiterals) {
chunks.push(code`export const ${def(fullName)} = {`);
Expand All @@ -35,7 +35,7 @@ export function generateEnum(
if (valueDesc.number === options.unrecognizedEnumValue) {
unrecognizedEnum = { present: true, name: memberName };
}
maybeAddComment(info, chunks, valueDesc.options?.deprecated, `${memberName} - `);
maybeAddComment(options, info, chunks, valueDesc.options?.deprecated, `${memberName} - `);
chunks.push(
code`${memberName} ${delimiter} ${options.stringEnums ? `"${valueName}"` : valueDesc.number.toString()},`,
);
Expand Down
4 changes: 2 additions & 2 deletions src/generate-generic-service-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function generateGenericServiceDefinition(
) {
const chunks: Code[] = [];

maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);
maybeAddComment(ctx.options, sourceInfo, chunks, serviceDesc.options?.deprecated);

// Service definition type
const name = def(`${serviceDesc.name}Definition`);
Expand All @@ -45,7 +45,7 @@ export function generateGenericServiceDefinition(

for (const [index, methodDesc] of serviceDesc.method.entries()) {
const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

chunks.push(code`
${uncapitalize(methodDesc.name)}: ${generateMethodDefinition(ctx, methodDesc)},
Expand Down
8 changes: 4 additions & 4 deletions src/generate-grpc-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function generateServiceDefinition(
) {
const chunks: Code[] = [];

maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);
maybeAddComment(ctx.options, sourceInfo, chunks, serviceDesc.options?.deprecated);

// Service definition type
const name = def(`${serviceDesc.name}Service`);
Expand All @@ -74,7 +74,7 @@ function generateServiceDefinition(
const outputType = messageToTypeName(ctx, methodDesc.outputType);

const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

const inputEncoder = generateEncoder(ctx, methodDesc.inputType);
const outputEncoder = generateEncoder(ctx, methodDesc.outputType);
Expand Down Expand Up @@ -114,7 +114,7 @@ function generateServerStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
const outputType = messageToTypeName(ctx, methodDesc.outputType);

const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

const callType = methodDesc.clientStreaming
? methodDesc.serverStreaming
Expand Down Expand Up @@ -146,7 +146,7 @@ function generateClientStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
const outputType = messageToTypeName(ctx, methodDesc.outputType);

const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

const responseCallback = code`(error: ${ServiceError} | null, response: ${outputType}) => void`;

Expand Down
8 changes: 4 additions & 4 deletions src/generate-nestjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function generateNestjsServiceController(

const Metadata = imp("Metadata@@grpc/grpc-js");

maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);
maybeAddComment(options, sourceInfo, chunks, serviceDesc.options?.deprecated);
const t = options.context ? `<${contextTypeVar}>` : "";
chunks.push(code`
export interface ${serviceDesc.name}Controller${t} {
Expand All @@ -35,7 +35,7 @@ export function generateNestjsServiceController(
serviceDesc.method.forEach((methodDesc, index) => {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, serviceDesc.options?.deprecated);
maybeAddComment(options, info, chunks, serviceDesc.options?.deprecated);

const params: Code[] = [];
if (options.context) {
Expand Down Expand Up @@ -99,7 +99,7 @@ export function generateNestjsServiceClient(

const Metadata = imp("Metadata@@grpc/grpc-js");

maybeAddComment(sourceInfo, chunks);
maybeAddComment(options, sourceInfo, chunks);
const t = options.context ? `<${contextTypeVar}>` : ``;
chunks.push(code`
export interface ${serviceDesc.name}Client${t} {
Expand All @@ -125,7 +125,7 @@ export function generateNestjsServiceClient(
const returns = responseObservable(ctx, methodDesc);

const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(options, info, chunks, methodDesc.options?.deprecated);
chunks.push(code`
${methodDesc.formattedName}(
${joinCode(params, { on: "," })}
Expand Down
4 changes: 2 additions & 2 deletions src/generate-nice-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function generateServerStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
const ServerStreamingMethodResult = ctx.utils.NiceGrpcServerStreamingMethodResult;

const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

if (methodDesc.clientStreaming) {
if (methodDesc.serverStreaming) {
Expand Down Expand Up @@ -107,7 +107,7 @@ function generateClientStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
const outputType = messageToTypeName(ctx, methodDesc.outputType, { keepValueType: true });

const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

if (methodDesc.clientStreaming) {
if (methodDesc.serverStreaming) {
Expand Down
4 changes: 2 additions & 2 deletions src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ export function generateService(
const { options } = ctx;
const chunks: Code[] = [];

maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);
maybeAddComment(options, sourceInfo, chunks, serviceDesc.options?.deprecated);
const maybeTypeVar = options.context ? `<${contextTypeVar}>` : "";
chunks.push(code`export interface ${def(serviceDesc.name)}${maybeTypeVar} {`);

serviceDesc.method.forEach((methodDesc, index) => {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
maybeAddComment(options, info, chunks, methodDesc.options?.deprecated);

const params: Code[] = [];
if (options.context) {
Expand Down
Loading

0 comments on commit 1b0f802

Please sign in to comment.