Skip to content

Commit

Permalink
option to skip deprecate field
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 committed Apr 22, 2024
1 parent bd56caf commit 87cffc1
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 50 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-proto",
"version": "1.172.0",
"name": "@sentio/ts-proto",
"version": "1.172.0-patch.1",
"description": "",
"main": "build/plugin.js",
"repository": "github:stephenh/ts-proto",
Expand Down
8 changes: 8 additions & 0 deletions src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function generateEnum(
const chunks: Code[] = [];
let unrecognizedEnum: UnrecognizedEnum = { present: false };

if (enumDesc.options?.deprecated && ctx.options.removeDeprecated) {
return code``;
}

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

if (options.enumsAsLiterals) {
Expand All @@ -30,6 +34,10 @@ export function generateEnum(

enumDesc.value.forEach((valueDesc, index) => {
const info = sourceInfo.lookup(Fields.enum.value, index);
if (valueDesc.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

const valueName = getValueName(ctx, fullName, valueDesc);
const memberName = getMemberName(ctx, enumDesc, valueDesc);
if (valueDesc.number === options.unrecognizedEnumValue) {
Expand Down
7 changes: 7 additions & 0 deletions src/generate-generic-service-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function generateGenericServiceDefinition(
serviceDesc: ServiceDescriptorProto,
) {
const chunks: Code[] = [];
if (serviceDesc.options?.deprecated && ctx.options.removeDeprecated) {
return code``;
}

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

Expand All @@ -45,6 +48,10 @@ export function generateGenericServiceDefinition(

for (const [index, methodDesc] of serviceDesc.method.entries()) {
const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
return code``;
}

maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

chunks.push(code`
Expand Down
15 changes: 15 additions & 0 deletions src/generate-grpc-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function generateServiceDefinition(
serviceDesc: ServiceDescriptorProto,
) {
const chunks: Code[] = [];
if (serviceDesc.options?.deprecated && ctx.options.removeDeprecated) {
return code``;
}

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

Expand All @@ -74,6 +77,10 @@ function generateServiceDefinition(
const outputType = messageToTypeName(ctx, methodDesc.outputType);

const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
continue;
}

maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

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

const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
continue;
}

maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

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

const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
continue;
}

maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

const responseCallback = code`(error: ${ServiceError} | null, response: ${outputType}) => void`;
Expand Down
11 changes: 11 additions & 0 deletions src/generate-nestjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function generateNestjsServiceController(
const chunks: Code[] = [];

const Metadata = imp("Metadata@@grpc/grpc-js");
if (serviceDesc.options?.deprecated && ctx.options.removeDeprecated) {
return code``;
}

maybeAddComment(options, sourceInfo, chunks, serviceDesc.options?.deprecated);
const t = options.context ? `<${contextTypeVar}>` : "";
Expand All @@ -35,6 +38,10 @@ export function generateNestjsServiceController(
serviceDesc.method.forEach((methodDesc, index) => {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const info = sourceInfo.lookup(Fields.service.method, index);
if (serviceDesc.options?.deprecated && ctx.options.removeDeprecated) {
return code``;
}

maybeAddComment(options, info, chunks, serviceDesc.options?.deprecated);

const params: Code[] = [];
Expand Down Expand Up @@ -125,6 +132,10 @@ export function generateNestjsServiceClient(
const returns = responseObservable(ctx, methodDesc);

const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

maybeAddComment(options, info, chunks, methodDesc.options?.deprecated);
chunks.push(code`
${methodDesc.formattedName}(
Expand Down
8 changes: 8 additions & 0 deletions src/generate-nice-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ function generateServerStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
const ServerStreamingMethodResult = ctx.utils.NiceGrpcServerStreamingMethodResult;

const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
continue;
}

maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

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

const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
continue;
}

maybeAddComment(ctx.options, info, chunks, methodDesc.options?.deprecated);

if (methodDesc.clientStreaming) {
Expand Down
7 changes: 7 additions & 0 deletions src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export function generateService(
): Code {
const { options } = ctx;
const chunks: Code[] = [];
if (serviceDesc.options?.deprecated && ctx.options.context) {
return code``;
}

maybeAddComment(options, sourceInfo, chunks, serviceDesc.options?.deprecated);
const maybeTypeVar = options.context ? `<${contextTypeVar}>` : "";
Expand All @@ -49,6 +52,10 @@ export function generateService(
serviceDesc.method.forEach((methodDesc, index) => {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const info = sourceInfo.lookup(Fields.service.method, index);
if (methodDesc.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

maybeAddComment(options, info, chunks, methodDesc.options?.deprecated);

const params: Code[] = [];
Expand Down
37 changes: 37 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [stri
// Syntax, unlike most fields, is not repeated and thus does not use an index
const sourceInfo = SourceInfo.fromDescriptor(fileDesc);
const headerComment = sourceInfo.lookup(Fields.file.syntax, undefined);
if (fileDesc.options?.deprecated && ctx.options.removeDeprecated) {
return [moduleName, code``];
}
maybeAddComment(options, headerComment, chunks, fileDesc.options?.deprecated);

// Apply formatting to methods here, so they propagate globally
Expand Down Expand Up @@ -199,6 +202,9 @@ export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [stri
fileDesc,
sourceInfo,
(fullName, message, _sInfo, fullProtoTypeName) => {
if (message.options?.deprecated && ctx.options.removeDeprecated) {
return;
}
const fullTypeName = maybePrefixPackage(fileDesc, fullProtoTypeName);
const outputWrapAndUnwrap = isWrapperType(fullTypeName);

Expand Down Expand Up @@ -956,6 +962,10 @@ function generateInterfaceDeclaration(
const { options, currentFile } = ctx;
const chunks: Code[] = [];

if (messageDesc.options?.deprecated && ctx.options.removeDeprecated) {
return code``;
}

maybeAddComment(options, sourceInfo, chunks, messageDesc.options?.deprecated);
// interface name should be defined to avoid import collisions
chunks.push(code`export interface ${def(fullName)} {`);
Expand All @@ -978,6 +988,10 @@ function generateInterfaceDeclaration(
}

const info = sourceInfo.lookup(Fields.message.field, index);
if (fieldDesc.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

maybeAddComment(options, info, chunks, fieldDesc.options?.deprecated);
const fieldKey = safeAccessor(getFieldName(fieldDesc, options));
const isOptional = isOptionalProperty(fieldDesc, messageDesc.options, options, currentFile.isProto3Syntax);
Expand Down Expand Up @@ -1050,6 +1064,9 @@ function generateBaseInstanceFactory(
const processedOneofs = new Set<number>();

for (const field of messageDesc.field) {
if (field.options?.deprecated && ctx.options.removeDeprecated) {
continue;
}
if (isWithinOneOfThatShouldBeUnion(ctx.options, field)) {
const { oneofIndex } = field;
if (!processedOneofs.has(oneofIndex)) {
Expand Down Expand Up @@ -1197,6 +1214,10 @@ function generateDecode(ctx: Context, fullName: string, messageDesc: DescriptorP

// add a case for each incoming field
messageDesc.field.forEach((field) => {
if (field.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

const fieldName = getFieldName(field, options);
const messageProperty = getPropertyAccessor("message", fieldName);
chunks.push(code`case ${field.number}:`);
Expand Down Expand Up @@ -1447,6 +1468,10 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP

// then add a case for each field
messageDesc.field.forEach((field) => {
if (field.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

const fieldName = getFieldName(field, options);
const messageProperty = getPropertyAccessor("message", fieldName);

Expand Down Expand Up @@ -1922,6 +1947,10 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string,

// add a check for each incoming field
messageDesc.field.forEach((field) => {
if (field.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

const fieldName = getFieldName(field, options);
const fieldKey = safeAccessor(fieldName);
const jsonName = getFieldJsonName(field, options);
Expand Down Expand Up @@ -2170,6 +2199,10 @@ function generateToJson(

// then add a case for each field
messageDesc.field.forEach((field) => {
if (field.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

const fieldName = getFieldName(field, options);
const jsonName = getFieldJsonName(field, options);
const jsonProperty = getPropertyAccessor("obj", jsonName);
Expand Down Expand Up @@ -2352,6 +2385,10 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri

// add a check for each incoming field
messageDesc.field.forEach((field) => {
if (field.options?.deprecated && ctx.options.removeDeprecated) {
return;
}

const fieldName = getFieldName(field, options);
const messageProperty = getPropertyAccessor("message", fieldName);
const objectProperty = getPropertyAccessor("object", fieldName);
Expand Down
4 changes: 4 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export type Options = {
useSnakeTypeName: boolean;
outputExtensions: boolean;
outputIndex: boolean;
removeDeprecated: boolean;
M: { [from: string]: string };
rpcBeforeRequest: boolean;
rpcAfterResponse: boolean;
Expand Down Expand Up @@ -155,6 +156,7 @@ export function defaultOptions(): Options {
useSnakeTypeName: true,
outputExtensions: false,
outputIndex: false,
removeDeprecated: false,
M: {},
rpcBeforeRequest: false,
rpcAfterResponse: false,
Expand Down Expand Up @@ -185,6 +187,8 @@ export function optionsFromParameter(parameter: string | undefined): Options {
Object.assign(options, parsed);
}

console.warn("remove deprecated: ", options.removeDeprecated);

// onlyTypes=true implies outputJsonMethods=false,outputEncodeMethods=false,outputClientImpl=false,nestJs=false
if (options.onlyTypes) {
options.outputJsonMethods = false;
Expand Down
Loading

0 comments on commit 87cffc1

Please sign in to comment.