Skip to content

Commit

Permalink
feat: added the "typePrefix" and "typeSuffix" options. (#1069)
Browse files Browse the repository at this point in the history
Fixes #1033
  • Loading branch information
oliveryasuna authored Jul 1, 2024
1 parent 14e7e0f commit ab515cd
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=useNullAsOptional=true`, `undefined` values will be converted to `null`, and if you use `optional` label in your `.proto` file, the field will have `undefined` type as well. for example:

- With `--ts_proto_opt=typePrefix=MyPrefix`, the generated interfaces, enums, and factories will have a prefix of `MyPrefix` in their names.

- With `--ts_proto_opt=typeSuffix=MySuffix`, the generated interfaces, enums, and factories will have a suffix of `MySuffix` in their names.

```protobuf
message ProfileInfo {
int32 id = 1;
Expand Down
17 changes: 17 additions & 0 deletions integration/affixes/affixes.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package affixes;

message AwesomeMessage {
message Inner {
}
}

enum AwesomeEnum {
AWESOME_ENUM_AWESOME = 0;
AWESOME_ENUM_COOL = 1;
AWESOME_ENUM_JUST_OKAY = 2;
}

service AwesomeService {
}
171 changes: 171 additions & 0 deletions integration/affixes/affixes.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integration/affixes/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typePrefix=Prefix,typeSuffix=Suffix
4 changes: 4 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export type Options = {
annotateFilesWithVersion: boolean;
noDefaultsForOptionals: boolean;
bigIntLiteral: boolean;
typePrefix: string;
typeSuffix: string;
};

export function defaultOptions(): Options {
Expand Down Expand Up @@ -172,6 +174,8 @@ export function defaultOptions(): Options {
annotateFilesWithVersion: true,
noDefaultsForOptionals: false,
bigIntLiteral: true,
typePrefix: "",
typeSuffix: "",
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export function visit(
const protoFullName = protoPrefix + enumDesc.name;
// I.e. FooBar_ZazInner
const tsFullName = tsPrefix + maybeSnakeToCamel(enumDesc.name, options);
const tsFullNameWithAffixes = `${options.typePrefix}${tsFullName}${options.typeSuffix}`;
const nestedSourceInfo = sourceInfo.open(childEnumType, index);
enumFn(tsFullName, enumDesc, nestedSourceInfo, protoFullName);
enumFn(tsFullNameWithAffixes, enumDesc, nestedSourceInfo, protoFullName);
});

const messages = "messageType" in proto ? proto.messageType : proto.nestedType;
Expand All @@ -51,8 +52,9 @@ export function visit(
const protoFullName = protoPrefix + message.name;
// I.e. FooBar_ZazInner
const tsFullName = tsPrefix + maybeSnakeToCamel(messageName(message), options);
const tsFullNameWithAffixes = `${options.typePrefix}${tsFullName}${options.typeSuffix}`;
const nestedSourceInfo = sourceInfo.open(childType, index);
messageFn(tsFullName, message, nestedSourceInfo, protoFullName);
messageFn(tsFullNameWithAffixes, message, nestedSourceInfo, protoFullName);
const delim = options.useSnakeTypeName ? "_" : "";
visit(message, nestedSourceInfo, messageFn, options, enumFn, tsFullName + delim, protoFullName + ".");
});
Expand Down
2 changes: 2 additions & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe("options", () => {
"keys",
],
"stringEnums": false,
"typePrefix": "",
"typeSuffix": "",
"unknownFields": false,
"unrecognizedEnum": true,
"unrecognizedEnumName": "UNRECOGNIZED",
Expand Down

0 comments on commit ab515cd

Please sign in to comment.