Skip to content

Commit

Permalink
feat: option useSnakeTypeName (#694)
Browse files Browse the repository at this point in the history
* feat: option useSnakeTypeName

* chore: update readme to include option
  • Loading branch information
JesseChrestler authored Oct 25, 2022
1 parent eb728ad commit ad73ff9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type Options = {
initializeFieldsAsUndefined: boolean;
useMapType: boolean;
useReadonlyTypes: boolean;
useSnakeTypeName: boolean;
M: { [from: string]: string };
};

Expand Down Expand Up @@ -119,6 +120,7 @@ export function defaultOptions(): Options {
initializeFieldsAsUndefined: true,
useMapType: false,
useReadonlyTypes: false,
useSnakeTypeName: true,
M: {},
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ".");
});
}

Expand Down
8 changes: 8 additions & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe("options", () => {
"useOptionals": "none",
"usePrototypeForDefaults": false,
"useReadonlyTypes": false,
"useSnakeTypeName": true,
}
`);
});
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit ad73ff9

Please sign in to comment.