Skip to content

Commit

Permalink
fix: Use underscore separator in snakeToCamel. (#648)
Browse files Browse the repository at this point in the history
* fix: Use underscore separator in snakeToCamel.

* Add default value.
  • Loading branch information
stephenh committed Aug 6, 2022
1 parent 83bbd61 commit b374910
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });
Expand Down

0 comments on commit b374910

Please sign in to comment.