From ad73ff9341d0d593156d10b5d96c4a47afdc802d Mon Sep 17 00:00:00 2001 From: Jesse Chrestler Date: Tue, 25 Oct 2022 17:16:54 -0400 Subject: [PATCH] feat: option useSnakeTypeName (#694) * feat: option useSnakeTypeName * chore: update readme to include option --- README.markdown | 18 ++++++++++++++++++ src/options.ts | 2 ++ src/visit.ts | 3 ++- tests/options-test.ts | 8 ++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index ef3f3b278..ae1b8641e 100644 --- a/README.markdown +++ b/README.markdown @@ -464,6 +464,24 @@ Generated code will be placed in the Gradle build directory. - With `--ts_proto_opt=useReadonlyTypes=true`, the generated types will be declared as immutable using typescript's `readonly` modifer. +- With `--ts_proto_opt=useSnakeTypeName=false` will remove snake casing from types. + + Example Protobuf + ```protobuf + message Box { + message Element { + message Image { + enum Alignment { + LEFT = 1; + CENTER = 2; + RIGHT = 3; + } + } + } + } + ``` + by default this is enabled which would generate a type of `Box_Element_Image_Alignment`. By disabling this option the type that is generated would be `BoxElementImageAlignment`. + ### NestJS Support We have a great way of working together with [nestjs](https://docs.nestjs.com/microservices/grpc). `ts-proto` generates `interfaces` and `decorators` for you controller, client. For more information see the [nestjs readme](NESTJS.markdown). diff --git a/src/options.ts b/src/options.ts index 091877aed..20c48ef01 100644 --- a/src/options.ts +++ b/src/options.ts @@ -74,6 +74,7 @@ export type Options = { initializeFieldsAsUndefined: boolean; useMapType: boolean; useReadonlyTypes: boolean; + useSnakeTypeName: boolean; M: { [from: string]: string }; }; @@ -119,6 +120,7 @@ export function defaultOptions(): Options { initializeFieldsAsUndefined: true, useMapType: false, useReadonlyTypes: false, + useSnakeTypeName: true, M: {}, }; } diff --git a/src/visit.ts b/src/visit.ts index 7bcd96648..54a11093d 100644 --- a/src/visit.ts +++ b/src/visit.ts @@ -53,7 +53,8 @@ export function visit( const tsFullName = tsPrefix + maybeSnakeToCamel(messageName(message), options); const nestedSourceInfo = sourceInfo.open(childType, index); messageFn(tsFullName, message, nestedSourceInfo, protoFullName); - visit(message, nestedSourceInfo, messageFn, options, enumFn, tsFullName + "_", protoFullName + "."); + const delim = options.useSnakeTypeName ? "_" : ""; + visit(message, nestedSourceInfo, messageFn, options, enumFn, tsFullName + delim, protoFullName + "."); }); } diff --git a/tests/options-test.ts b/tests/options-test.ts index 5e7954f1e..6625b6c55 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -50,6 +50,7 @@ describe("options", () => { "useOptionals": "none", "usePrototypeForDefaults": false, "useReadonlyTypes": false, + "useSnakeTypeName": true, } `); }); @@ -136,6 +137,13 @@ describe("options", () => { }); }); + it("useSnakeTypeName", () => { + const options = optionsFromParameter("useSnakeTypeName=false"); + expect(options).toMatchObject({ + useSnakeTypeName: false, + }); + }); + it("useJsonWireFormat implies useDate=string and stringEnums=true", () => { const options = optionsFromParameter("useJsonWireFormat=true,onlyTypes=true"); expect(options).toMatchObject({