Skip to content

Commit

Permalink
fix(diregapic): support field name IPProtocol gRPC transcoding (#1103)
Browse files Browse the repository at this point in the history
* fix: support field name IPProtocol gRPC transcoding

* keep capitalization

* add test case for iPProtocol
  • Loading branch information
summer-ji-eng authored Sep 10, 2021
1 parent f252a85 commit d9c2f21
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
* to snake_case (normally used in proto definitions).
*/
export function camelToSnakeCase(str: string) {
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
// Keep the first position capitalization, otherwise decapitalize with underscore.
return str.replace(/(?!^)[A-Z]/g, letter => `_${letter.toLowerCase()}`);
}

/**
Expand All @@ -41,7 +42,8 @@ export function snakeToCamelCase(str: string) {
const splitted = str
.split(/(?=[A-Z])|[\s\W_]+/)
.filter(w => w.length > 0)
.map(word => word.toLowerCase());
// Keep the capitalization for the first split.
.map((word, index) => (index === 0 ? word : word.toLowerCase()));
if (splitted.length === 0) {
return str;
}
Expand Down
53 changes: 53 additions & 0 deletions test/unit/transcoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,59 @@ describe('gRPC to HTTP transcoding', () => {
);
});

it('transcode should not decapitalize the first capital letter', () => {
assert.deepStrictEqual(
transcode(
{
parent: 'post1/project',
IPProtocol: 'tcp',
},
parsedOptions
),
{
httpMethod: 'post',
queryString: '',
url: '/v3/post1/project/supportedLanguages',
data: {
IPProtocol: 'tcp',
},
}
);
assert.deepStrictEqual(
transcode(
{
parent: 'post2/project',
IPProtocol: 'tcp',
field: 'value',
},
parsedOptions
),
{
httpMethod: 'post',
queryString: 'IPProtocol=tcp',
url: '/v3/post2/project/supportedLanguages',
data: 'value',
}
);
assert.deepStrictEqual(
transcode(
{
parent: 'post1/project',
iPProtocol: 'tcp',
},
parsedOptions
),
{
httpMethod: 'post',
queryString: '',
url: '/v3/post1/project/supportedLanguages',
data: {
iPProtocol: 'tcp',
},
}
);
});

it('transcode should ignore inherited properties', () => {
// In this test we emulate protobuf object that has inherited circular
// references in the prototype. This is supposed to be a pure JS code
Expand Down
3 changes: 3 additions & 0 deletions test/unit/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ describe('util.ts', () => {
assert.strictEqual(camelToSnakeCase('test123'), 'test123');
assert.strictEqual(camelToSnakeCase('testAbc'), 'test_abc');
assert.strictEqual(camelToSnakeCase('testAbcDef'), 'test_abc_def');
assert.strictEqual(camelToSnakeCase('IPProtocol'), 'I_p_protocol');
assert.strictEqual(camelToSnakeCase('iPProtocol'), 'i_p_protocol');
});

it('snakeToCamelCase', () => {
assert.strictEqual(snakeToCamelCase('test'), 'test');
assert.strictEqual(snakeToCamelCase('test123'), 'test123');
assert.strictEqual(snakeToCamelCase('test_abc'), 'testAbc');
assert.strictEqual(snakeToCamelCase('test_abc_def'), 'testAbcDef');
assert.strictEqual(snakeToCamelCase('I_p_protocol'), 'IPProtocol');
});
});

0 comments on commit d9c2f21

Please sign in to comment.