Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add options to limit generation of encode and decode methods to only specific message types #1085

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ Generated code will be placed in the Gradle build directory.

This is useful if you want "only types".

- With `--ts_proto_opt=outputEncodeIncludeTypes=regex`, the `Message.encode` method will only be output for types whose name matches the provided regular expression.

This is useful if you want to limit `encode` methods to only select types.

- With `--ts_proto_opt=outputDecodeIncludeTypes=regex`, the `Message.decode` method will only be output for types whose name matches the provided regular expression.

This is useful if you want to limit `decode` methods to only select types.

- With `--ts_proto_opt=outputJsonMethods=false`, the `Message.fromJSON` and `Message.toJSON` methods for working with JSON-coded data will not be output.

This is also useful if you want "only types".
Expand Down
11 changes: 11 additions & 0 deletions integration/output-decode-include-types/encode.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

import "google/protobuf/wrappers.proto";

message EncodeWithDecodeMethod {
string encode = 1;
}

message EncodeDontGenerateDecodeMethod {
string encode_dont_generate_decode_method = 1;
}
48 changes: 48 additions & 0 deletions integration/output-decode-include-types/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// source: encode.proto

/* eslint-disable */
import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "";

export interface EncodeWithDecodeMethod {
encode: string;
}

export interface EncodeDontGenerateDecodeMethod {
encodeDontGenerateDecodeMethod: string;
}

function createBaseEncodeWithDecodeMethod(): EncodeWithDecodeMethod {
return { encode: "" };
}

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

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

function createBaseEncodeDontGenerateDecodeMethod(): EncodeDontGenerateDecodeMethod {
return { encodeDontGenerateDecodeMethod: "" };
}
1 change: 1 addition & 0 deletions integration/output-decode-include-types/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputEncodeMethods=decode-only,outputPartialMethods=false,outputJsonMethods=false,nestJs=false,outputDecodeIncludeTypes=^EncodeWithDecodeMethod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputEncodeMethods=true,outputPartialMethods=false,outputJsonMethods=false,nestJs=false,outputEncodeIncludeTypes=^Request,outputDecodeIncludeTypes=^Response$
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

import "google/protobuf/wrappers.proto";

message Request {
string request_id = 1;

message Nested {
string more_data = 1;
}

Nested nested = 2;
}

message Response {
string response_id = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// source: request_response.proto

/* eslint-disable */
import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "";

export interface Request {
requestId: string;
nested: Request_Nested | undefined;
}

export interface Request_Nested {
moreData: string;
}

export interface Response {
responseId: string;
}

function createBaseRequest(): Request {
return { requestId: "", nested: undefined };
}

export const Request = {
encode(message: Request, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.requestId !== "") {
writer.uint32(10).string(message.requestId);
}
if (message.nested !== undefined) {
Request_Nested.encode(message.nested, writer.uint32(18).fork()).ldelim();
}
return writer;
},
};

function createBaseRequest_Nested(): Request_Nested {
return { moreData: "" };
}

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

function createBaseResponse(): Response {
return { responseId: "" };
}

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

message.responseId = reader.string();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
};
11 changes: 11 additions & 0 deletions integration/output-encode-include-types/encode.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

import "google/protobuf/wrappers.proto";

message Encode {
string encode = 1;
}

message DontGenerateEncode {
string dont_generate_encode = 1;
}
32 changes: 32 additions & 0 deletions integration/output-encode-include-types/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// source: encode.proto

/* eslint-disable */
import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "";

export interface Encode {
encode: string;
}

export interface DontGenerateEncode {
dontGenerateEncode: string;
}

function createBaseEncode(): Encode {
return { encode: "" };
}

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

function createBaseDontGenerateEncode(): DontGenerateEncode {
return { dontGenerateEncode: "" };
}
1 change: 1 addition & 0 deletions integration/output-encode-include-types/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputEncodeMethods=encode-only,outputPartialMethods=false,outputJsonMethods=false,nestJs=false,outputEncodeIncludeTypes=^Encode$
12 changes: 8 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,21 @@ export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [stri

if (options.outputEncodeMethods) {
if (
options.outputEncodeMethods === true ||
options.outputEncodeMethods === "encode-only" ||
options.outputEncodeMethods === "encode-no-creation"
(options.outputEncodeMethods === true ||
options.outputEncodeMethods === "encode-only" ||
options.outputEncodeMethods === "encode-no-creation") &&
(options.outputEncodeIncludeTypes === "" || new RegExp(options.outputEncodeIncludeTypes).test(fullTypeName))
) {
staticMembers.push(generateEncode(ctx, fullName, message));

if (options.outputExtensions && options.unknownFields && message.extensionRange.length) {
staticMembers.push(generateSetExtension(ctx, fullName));
}
}
if (options.outputEncodeMethods === true || options.outputEncodeMethods === "decode-only") {
if (
(options.outputEncodeMethods === true || options.outputEncodeMethods === "decode-only") &&
(options.outputDecodeIncludeTypes === "" || new RegExp(options.outputDecodeIncludeTypes).test(fullTypeName))
) {
staticMembers.push(generateDecode(ctx, fullName, message));

if (options.outputExtensions && options.unknownFields && message.extensionRange.length) {
Expand Down
4 changes: 4 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export type Options = {
fileSuffix: string;
importSuffix: string;
outputEncodeMethods: true | false | "encode-only" | "decode-only" | "encode-no-creation";
outputEncodeIncludeTypes: string;
outputDecodeIncludeTypes: string;
outputJsonMethods: true | false | "to-only" | "from-only";
outputPartialMethods: boolean;
outputTypeAnnotations: boolean | "static-only" | "optional";
Expand Down Expand Up @@ -126,6 +128,8 @@ export function defaultOptions(): Options {
importSuffix: "",
lowerCaseServiceMethods: false,
outputEncodeMethods: true,
outputEncodeIncludeTypes: "",
outputDecodeIncludeTypes: "",
outputJsonMethods: true,
outputPartialMethods: true,
outputTypeAnnotations: false,
Expand Down
2 changes: 2 additions & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe("options", () => {
"oneof": "properties",
"onlyTypes": false,
"outputClientImpl": false,
"outputDecodeIncludeTypes": "",
"outputEncodeIncludeTypes": "",
"outputEncodeMethods": false,
"outputExtensions": false,
"outputIndex": false,
Expand Down