From b374910a8fb1d0efe6c5c5322f8788cc3cc1ca6c Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Sat, 6 Aug 2022 13:36:32 -0500 Subject: [PATCH] fix: Use underscore separator in snakeToCamel. (#648) * fix: Use underscore separator in snakeToCamel. * Add default value. --- README.markdown | 10 +++++++++- src/options.ts | 2 +- tests/options-test.ts | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 43c5dce74..c4813e384 100644 --- a/README.markdown +++ b/README.markdown @@ -339,7 +339,15 @@ Generated code will be placed in the Gradle build directory. - 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. `snakeToCamel` can also be set as string with `--ts_proto_opt=snakeToCamel=keys,json`. `keys` will keep field names as camelCase and `json` will keep json field names as camelCase. Empty string will keep field names as snake_case. +- With `--ts_proto_opt=snakeToCamel=false`, fields will be kept snake case in both the message keys and the `toJSON` / `fromJSON` methods. + + `snakeToCamel` can also be set as a `_`-delimited list of strings (comma is reserved as the flag delimited), i.e. `--ts_proto_opt=snakeToCamel=keys_json`, where including `keys` will make message keys be camel case and including `json` will make JSON keys be camel case. + + Empty string, i.e. `snakeToCamel=`, will keep both messages keys and `JSON` keys as snake case (it is the same as `snakeToCamel=false`). + + Note that to use the `json_name` attribute, you'll have to use the `json`. + + The default behavior is `keys_json`, i.e. both will be camel cased, and `json_name` will be used if set. - With `--ts_proto_opt=outputEncodeMethods=false`, the `Message.encode` and `Message.decode` methods for working with protobuf-encoded/binary data will not be output. diff --git a/src/options.ts b/src/options.ts index f19acab94..549ef4f50 100644 --- a/src/options.ts +++ b/src/options.ts @@ -176,7 +176,7 @@ export function optionsFromParameter(parameter: string | undefined): Options { } else if ((options.snakeToCamel as any) === true) { options.snakeToCamel = ['keys', 'json']; } else if (typeof options.snakeToCamel === 'string') { - options.snakeToCamel = [options.snakeToCamel]; + options.snakeToCamel = (options.snakeToCamel as string).split('_') as any; } if (options.useJsonWireFormat) { diff --git a/tests/options-test.ts b/tests/options-test.ts index efb733c61..ccbbc6b55 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -98,6 +98,11 @@ describe('options', () => { expect(options).toMatchObject({ snakeToCamel: ['keys'] }); }); + it('can set snakeToCamel as two values', () => { + const options = optionsFromParameter('snakeToCamel=keys_json'); + expect(options).toMatchObject({ snakeToCamel: ['keys', 'json'] }); + }); + it('can set multiple values as an array', () => { const options = optionsFromParameter('foo=one,foo=two'); expect(options).toMatchObject({ foo: ['one', 'two'] });