Skip to content

Commit

Permalink
feat: removeEnumPrefix option (#779)
Browse files Browse the repository at this point in the history
* Remove enum prefix feature

* Update readme

* Fix prettier

* Make a boolean parameter for removeEnumPrefix option

---------

Co-authored-by: maxkiner <kirpichnikov@qtim.ru>
  • Loading branch information
superclaw and maxkiner committed Feb 22, 2023
1 parent 7a3d429 commit 53733e6
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=unrecognizedEnum=false` enums will not contain an `UNRECOGNIZED` key with value of -1.

- With `--ts_proto_opt=removeEnumPrefix=true` generated enums will have the enum name removed from members.

`FooBar.FOO_BAR_BAZ = "FOO_BAR_BAZ"` will generate `FooBar.BAZ = "FOO_BAR_BAZ"`

- With `--ts_proto_opt=lowerCaseServiceMethods=true`, the method names of service methods will be lowered/camel-case, i.e. `service.findFoo` instead of `service.FindFoo`.

- With `--ts_proto_opt=snakeToCamel=false`, fields will be kept snake case in both the message keys and the `toJSON` / `fromJSON` methods.
Expand Down
1 change: 1 addition & 0 deletions integration/remove-enum-prefix/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
removeEnumPrefix=true,unrecognizedEnum=false
Binary file not shown.
13 changes: 13 additions & 0 deletions integration/remove-enum-prefix/remove-enum-prefix.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

enum Foo {
FOO_UNSPECIFIED = 0;
FOO_BAR = 1;
FOO_BAZ = 2;
}

enum Bar {
BAR_UNSPECIFIED = 0;
BAZ = 1;
QUX = 2;
}
92 changes: 92 additions & 0 deletions integration/remove-enum-prefix/remove-enum-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-disable */

export const protobufPackage = "";

export enum Foo {
UNSPECIFIED = 0,
BAR = 1,
BAZ = 2,
}

export function fooFromJSON(object: any): Foo {
switch (object) {
case 0:
case "FOO_UNSPECIFIED":
return Foo.UNSPECIFIED;
case 1:
case "FOO_BAR":
return Foo.BAR;
case 2:
case "FOO_BAZ":
return Foo.BAZ;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Foo");
}
}

export function fooToJSON(object: Foo): string {
switch (object) {
case Foo.UNSPECIFIED:
return "FOO_UNSPECIFIED";
case Foo.BAR:
return "FOO_BAR";
case Foo.BAZ:
return "FOO_BAZ";
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Foo");
}
}

export enum Bar {
UNSPECIFIED = 0,
BAZ = 1,
QUX = 2,
}

export function barFromJSON(object: any): Bar {
switch (object) {
case 0:
case "BAR_UNSPECIFIED":
return Bar.UNSPECIFIED;
case 1:
case "BAZ":
return Bar.BAZ;
case 2:
case "QUX":
return Bar.QUX;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Bar");
}
}

export function barToJSON(object: Bar): string {
switch (object) {
case Bar.UNSPECIFIED:
return "BAR_UNSPECIFIED";
case Bar.BAZ:
return "BAZ";
case Bar.QUX:
return "QUX";
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Bar");
}
}

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
if (typeof self !== "undefined") {
return self;
}
if (typeof window !== "undefined") {
return window;
}
if (typeof global !== "undefined") {
return global;
}
throw "Unable to locate global object";
})();
36 changes: 27 additions & 9 deletions src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { code, def, Code, joinCode } from "ts-poet";
import { EnumDescriptorProto } from "ts-proto-descriptors";
import { EnumDescriptorProto, EnumValueDescriptorProto } from "ts-proto-descriptors";
import { maybeAddComment } from "./utils";
import { camelCase } from "./case";
import { camelCase, camelToSnake } from "./case";
import SourceInfo, { Fields } from "./sourceInfo";
import { Context } from "./context";

Expand Down Expand Up @@ -30,9 +30,11 @@ export function generateEnum(

enumDesc.value.forEach((valueDesc, index) => {
const info = sourceInfo.lookup(Fields.enum.value, index);
maybeAddComment(info, chunks, valueDesc.options?.deprecated, `${valueDesc.name} - `);
const valueName = getValueName(ctx, fullName, valueDesc);
const memberName = getMemberName(ctx, fullName, valueDesc);
maybeAddComment(info, chunks, valueDesc.options?.deprecated, `${memberName} - `);
chunks.push(
code`${valueDesc.name} ${delimiter} ${options.stringEnums ? `"${valueDesc.name}"` : valueDesc.number.toString()},`
code`${memberName} ${delimiter} ${options.stringEnums ? `"${valueName}"` : valueDesc.number.toString()},`
);
});

Expand Down Expand Up @@ -76,10 +78,12 @@ export function generateEnumFromJson(ctx: Context, fullName: string, enumDesc: E
chunks.push(code`switch (object) {`);

for (const valueDesc of enumDesc.value) {
const memberName = getMemberName(ctx, fullName, valueDesc);
const valueName = getValueName(ctx, fullName, valueDesc);
chunks.push(code`
case ${valueDesc.number}:
case "${valueDesc.name}":
return ${fullName}.${valueDesc.name};
case "${valueName}":
return ${fullName}.${memberName};
`);
}

Expand Down Expand Up @@ -119,9 +123,12 @@ export function generateEnumToJson(ctx: Context, fullName: string, enumDesc: Enu

for (const valueDesc of enumDesc.value) {
if (ctx.options.useNumericEnumForJson) {
chunks.push(code`case ${fullName}.${valueDesc.name}: return ${valueDesc.number};`);
const memberName = getMemberName(ctx, fullName, valueDesc);
chunks.push(code`case ${fullName}.${memberName}: return ${valueDesc.number};`);
} else {
chunks.push(code`case ${fullName}.${valueDesc.name}: return "${valueDesc.name}";`);
const memberName = getMemberName(ctx, fullName, valueDesc);
const valueName = getValueName(ctx, fullName, valueDesc);
chunks.push(code`case ${fullName}.${memberName}: return "${valueName}";`);
}
}

Expand Down Expand Up @@ -163,7 +170,7 @@ export function generateEnumToNumber(ctx: Context, fullName: string, enumDesc: E
chunks.push(code`export function ${def(functionName)}(object: ${fullName}): number {`);
chunks.push(code`switch (object) {`);
for (const valueDesc of enumDesc.value) {
chunks.push(code`case ${fullName}.${valueDesc.name}: return ${valueDesc.number};`);
chunks.push(code`case ${fullName}.${getMemberName(ctx, fullName, valueDesc)}: return ${valueDesc.number};`);
}

if (options.unrecognizedEnum) {
Expand All @@ -184,3 +191,14 @@ export function generateEnumToNumber(ctx: Context, fullName: string, enumDesc: E
chunks.push(code`}`);
return joinCode(chunks, { on: "\n" });
}

function getMemberName(ctx: Context, fullName: string, valueDesc: EnumValueDescriptorProto): string {
if (ctx.options.removeEnumPrefix) {
return valueDesc.name.replace(`${camelToSnake(fullName)}_`, "");
}
return valueDesc.name;
}

function getValueName(ctx: Context, fullName: string, valueDesc: EnumValueDescriptorProto): string {
return valueDesc.name;
}
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type Options = {
outputTypeRegistry: boolean;
stringEnums: boolean;
constEnums: boolean;
removeEnumPrefix: boolean;
enumsAsLiterals: boolean;
outputClientImpl: boolean | "grpc-web";
outputServices: ServiceOption[];
Expand Down Expand Up @@ -99,6 +100,7 @@ export function defaultOptions(): Options {
outputTypeRegistry: false,
stringEnums: false,
constEnums: false,
removeEnumPrefix: false,
enumsAsLiterals: false,
outputClientImpl: true,
outputServices: [],
Expand Down
1 change: 1 addition & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("options", () => {
"default",
],
"outputTypeRegistry": false,
"removeEnumPrefix": false,
"returnObservable": false,
"snakeToCamel": Array [
"json",
Expand Down

0 comments on commit 53733e6

Please sign in to comment.