Skip to content

Commit

Permalink
fix: camelCase more proto identifiers (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster committed Jan 9, 2020
1 parent 0ee4a6c commit ad3c1e9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 50 deletions.
2 changes: 1 addition & 1 deletion templates/typescript_gapic/_util.njk
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ request.{{ oneComment.paramName.toCamelCase() }}
{{ chain }}.{{ field.toCamelCase() }} = {};
{%- set chain = chain + "." + field.toCamelCase() -%}
{%- endfor %}
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0] }} = '';
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0].toCamelCase() }} = '';
{%- endif %}
{%- endmacro -%}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export class {{ service.name }}Client {
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'{{ method.headerRequestParams.toString().toSnakeCase() }}': request.{{ method.headerRequestParams.camelCaseBeforeDot("!.") }} || '',
'{{ method.headerRequestParams.toSnakeCaseString(".") }}': request.{{ method.headerRequestParams.toCamelCaseString("!.") }} || '',
});
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
Expand Down Expand Up @@ -487,7 +487,7 @@ export class {{ service.name }}Client {
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'{{ method.headerRequestParams.toString().toSnakeCase() }}': request.{{ method.headerRequestParams.camelCaseBeforeDot("!.") }} || '',
'{{ method.headerRequestParams.toString().toSnakeCase() }}': request.{{ method.headerRequestParams.toCamelCaseString("!.") }} || '',
});
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
Expand Down Expand Up @@ -543,7 +543,7 @@ export class {{ service.name }}Client {
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'{{ method.headerRequestParams.toString().toSnakeCase() }}': request.{{ method.headerRequestParams.camelCaseBeforeDot("!.") }} || '',
'{{ method.headerRequestParams.toString().toSnakeCase() }}': request.{{ method.headerRequestParams.toCamelCaseString("!.") }} || '',
});
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
Expand Down
3 changes: 2 additions & 1 deletion typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ interface String {
}

interface Array<T> {
camelCaseBeforeDot(this: string[], joiner: string): string;
toCamelCaseString(this: string[], joiner: string): string;
toSnakeCaseString(this: string[], joiner: string): string;
}
15 changes: 9 additions & 6 deletions typescript/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ String.prototype.replaceAll = function(
return this.split(search).join(replacement);
};

Array.prototype.camelCaseBeforeDot = function(
Array.prototype.toCamelCaseString = function(
this: string[],
joiner: string
): string {
if (this.length <= 1) {
return this.toString().toCamelCase();
}
const res = this.slice(0, -1).map(w => w.toCamelCase());
return res.join(joiner) + joiner + this[this.length - 1];
return this.map(part => part.toCamelCase()).join(joiner);
};

Array.prototype.toSnakeCaseString = function(
this: string[],
joiner: string
): string {
return this.map(part => part.toSnakeCase()).join(joiner);
};
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ export class KeyManagementServiceClient {
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'crypto_key_name': request.cryptoKey!.name || '',
'crypto_key.name': request.cryptoKey!.name || '',
});
return this._innerApiCalls.updateCryptoKey(request, options, callback);
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ export class KeyManagementServiceClient {
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'crypto_key_version_name': request.cryptoKeyVersion!.name || '',
'crypto_key_version.name': request.cryptoKeyVersion!.name || '',
});
return this._innerApiCalls.updateCryptoKeyVersion(request, options, callback);
}
Expand Down
117 changes: 80 additions & 37 deletions typescript/test/unit/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,43 +311,86 @@ describe('util.ts', () => {
});

describe('array manipulation', () => {
it('should convert to camel case except last element', () => {
it('should convert to camelCase before dot', () => {
assert.deepStrictEqual([''].camelCaseBeforeDot('!.'), '');
assert.deepStrictEqual(['test'].camelCaseBeforeDot('!.'), 'test');
assert.deepStrictEqual(
['camelCaseString'].camelCaseBeforeDot('!.'),
'camelCaseString'
);
assert.deepStrictEqual(
['PascalCaseString'].camelCaseBeforeDot('!.'),
'pascalCaseString'
);
assert.deepStrictEqual(
['snake_case_string'].camelCaseBeforeDot('!.'),
'snakeCaseString'
);
assert.deepStrictEqual(
['kebab-case-string'].camelCaseBeforeDot('!.'),
'kebabCaseString'
);
assert.deepStrictEqual(
['random/separators-string'].camelCaseBeforeDot('!.'),
'randomSeparatorsString'
);
assert.deepStrictEqual(
['mixedType-string', 'SomewhatWeird'].camelCaseBeforeDot('!.'),
'mixedTypeString!.SomewhatWeird'
);
assert.deepStrictEqual(
['productName', 'v1p1beta1'].camelCaseBeforeDot('!.'),
'productName!.v1p1beta1'
);
assert.deepStrictEqual(
['product_key', 'lower_name', 'v1p1beta1'].camelCaseBeforeDot('!.'),
'productKey!.lowerName!.v1p1beta1'
);
});
it('should convert array to camel case string using the joiner', () => {
assert.deepStrictEqual([''].toCamelCaseString('!.'), '');
assert.deepStrictEqual(['test'].toCamelCaseString('!.'), 'test');
assert.deepStrictEqual(
['camelCaseString'].toCamelCaseString('!.'),
'camelCaseString'
);
assert.deepStrictEqual(
['PascalCaseString'].toCamelCaseString('!.'),
'pascalCaseString'
);
assert.deepStrictEqual(
['snake_case_string'].toCamelCaseString('!.'),
'snakeCaseString'
);
assert.deepStrictEqual(
['kebab-case-string'].toCamelCaseString('!.'),
'kebabCaseString'
);
assert.deepStrictEqual(
['random/separators-string'].toCamelCaseString('!.'),
'randomSeparatorsString'
);
assert.deepStrictEqual(
['mixedType-string', 'SomewhatWeird'].toCamelCaseString('!.'),
'mixedTypeString!.somewhatWeird'
);
assert.deepStrictEqual(
['productName', 'v1p1beta1'].toCamelCaseString('!.'),
'productName!.v1p1beta1'
);
assert.deepStrictEqual(
['product_key', 'lower_name', 'v1p1beta1'].toCamelCaseString('!.'),
'productKey!.lowerName!.v1p1beta1'
);
assert.deepStrictEqual(
['tableReference', 'project_id'].toCamelCaseString('!.'),
'tableReference!.projectId'
);
});

it('should convert array to snake case string using the joiner', () => {
assert.deepStrictEqual([''].toSnakeCaseString('!.'), '');
assert.deepStrictEqual(['test'].toSnakeCaseString('!.'), 'test');
assert.deepStrictEqual(
['camelCaseString'].toSnakeCaseString('!.'),
'camel_case_string'
);
assert.deepStrictEqual(
['PascalCaseString'].toSnakeCaseString('!.'),
'pascal_case_string'
);
assert.deepStrictEqual(
['snake_case_string'].toSnakeCaseString('!.'),
'snake_case_string'
);
assert.deepStrictEqual(
['kebab-case-string'].toSnakeCaseString('!.'),
'kebab_case_string'
);
assert.deepStrictEqual(
['random/separators-string'].toSnakeCaseString('!.'),
'random_separators_string'
);
assert.deepStrictEqual(
['mixedType-string', 'SomewhatWeird'].toSnakeCaseString('!.'),
'mixed_type_string!.somewhat_weird'
);
assert.deepStrictEqual(
['productName', 'v1p1beta1'].toSnakeCaseString('!.'),
'product_name!.v1p1beta1'
);
assert.deepStrictEqual(
['product_key', 'lower_name', 'v1p1beta1'].toSnakeCaseString('!.'),
'product_key!.lower_name!.v1p1beta1'
);
assert.deepStrictEqual(
['tableReference', 'project_id'].toSnakeCaseString('!.'),
'table_reference!.project_id'
);
});
});
});

0 comments on commit ad3c1e9

Please sign in to comment.