Skip to content

Commit

Permalink
fix: Add functionality for grpc camel case to respect splitting by wo…
Browse files Browse the repository at this point in the history
…rd (#721)

* Add functionality for grpc camel case to respect splitting by word

* Add lodash as a direct dependency

* chore: Bump to yarn v3. (#722)

* chore: Bump to yarn v3.

* Bump angular example.

* Rebase from upstream

* Update dependencies

* Depend only on lodash.camelcase to reduce import size

* Fix an issue with esModuleInterop

* Fix an issue with esModuleInterop

* Add full version of lodash back

* Try fixing esModuleInterop

* Remove esModuleInterop

* Replace library for using camel casing by word

* Force rebuild

* Update comment

Co-authored-by: Stephen Haberman <stephen.haberman@gmail.com>
  • Loading branch information
Ian Gregson and stephenh committed Dec 9, 2022
1 parent 7143103 commit 4af040c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
},
"dependencies": {
"@types/object-hash": "^1.3.0",
"case-anything": "^2.1.10",
"dataloader": "^1.4.0",
"object-hash": "^1.3.1",
"protobufjs": "^6.11.3",
Expand Down
2 changes: 0 additions & 2 deletions protos/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ protoc \

./node_modules/.bin/tsc -p tsconfig.json



12 changes: 12 additions & 0 deletions src/case.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { camelCase as camelCaseAnything } from "case-anything";

import { Options } from "./options";

/** Converts `key` to TS/JS camel-case idiom, unless overridden not to. */
Expand Down Expand Up @@ -32,3 +34,13 @@ export function capitalize(s: string): string {
export function camelCase(s: string): string {
return s.substring(0, 1).toLowerCase() + s.substring(1);
}

export function camelCaseGrpc(s: string): string {
/* This function uses the exact same semantics found inside the grpc
* nodejs library. Camel case splitting must be done by word i.e
* GetAPIValue must become getApiValue (notice the API becomes Api).
* This needs to be followed otherwise it will not succeed in the grpc nodejs module.
*/

return camelCaseAnything(s);
}
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import ReadStream = NodeJS.ReadStream;
import { SourceDescription } from "./sourceInfo";
import { Options, ServiceOption } from "./options";
import { camelCase, snakeToCamel } from "./case";
import { camelCaseGrpc, snakeToCamel } from "./case";

export function protoFilesToGenerate(request: CodeGeneratorRequest): FileDescriptorProto[] {
return request.protoFile.filter((f) => request.fileToGenerate.includes(f.name));
Expand Down Expand Up @@ -167,7 +167,7 @@ export class FormattedMethodDescriptor implements MethodDescriptorProto {
let result = methodName;

if (options.lowerCaseServiceMethods || options.outputServices.includes(ServiceOption.GRPC)) {
result = camelCase(result);
result = camelCaseGrpc(result);
}

return result;
Expand Down
6 changes: 5 additions & 1 deletion tests/case-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maybeSnakeToCamel } from "../src/case";
import { maybeSnakeToCamel, camelCaseGrpc } from "../src/case";
import { Options, optionsFromParameter } from "../src/options";
import { getFieldJsonName } from "../src/utils";

Expand Down Expand Up @@ -47,6 +47,10 @@ describe("case", () => {
expect(maybeSnakeToCamel("_uuid_foo", { snakeToCamel: ["keys"] })).toEqual("UuidFoo");
});

it("converts string to camel case respecting word separation, getAPIValue === getApiValue", () => {
expect(camelCaseGrpc("GetAPIValue")).toEqual("getApiValue");
});

describe("getFieldJsonName", () => {
it("keeps snake case when jsonName is probably not set", () => {
expect(getFieldJsonName({ name: "foo_bar", jsonName: "fooBar" }, { snakeToCamel: [] })).toBe("foo_bar");
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,13 @@ __metadata:
languageName: node
linkType: hard

"case-anything@npm:^2.1.10":
version: 2.1.10
resolution: "case-anything@npm:2.1.10"
checksum: eff2769d7da178115be8ac2617ccbb850664ffd36f0a897e000d9dd5a64300141128f6a8fe024bd98ec63cc9173a72c1426bbcaac328c8a44109ce15f7c14a5e
languageName: node
linkType: hard

"caseless@npm:~0.12.0":
version: 0.12.0
resolution: "caseless@npm:0.12.0"
Expand Down Expand Up @@ -8957,6 +8964,7 @@ __metadata:
"@types/jest": ^26.0.22
"@types/node": ^14.14.37
"@types/object-hash": ^1.3.0
case-anything: ^2.1.10
chokidar: ^3.5.2
dataloader: ^1.4.0
jest: ^28.1.2
Expand Down

0 comments on commit 4af040c

Please sign in to comment.