Skip to content

Commit efdbcce

Browse files
committed
Addressing PR feedback.
1 parent 48a8b86 commit efdbcce

File tree

12 files changed

+1150
-1107
lines changed

12 files changed

+1150
-1107
lines changed

common/api-review/firestore-lite.api.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface AggregateSpec {
4343

4444
// @public
4545
export type AggregateSpecData<T extends AggregateSpec> = {
46-
[P in keyof T as TrimBackticks<P>]: T[P] extends AggregateField<infer U> ? U : never;
46+
[P in keyof T]: T[P] extends AggregateField<infer U> ? U : never;
4747
};
4848

4949
// @public
@@ -401,9 +401,6 @@ export interface TransactionOptions {
401401
readonly maxAttempts?: number;
402402
}
403403

404-
// @public
405-
export type TrimBackticks<T> = T extends `\`${infer Body}\`` ? Body : T;
406-
407404
// @public
408405
export type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
409406

common/api-review/firestore.api.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface AggregateSpec {
4343

4444
// @public
4545
export type AggregateSpecData<T extends AggregateSpec> = {
46-
[P in keyof T as TrimBackticks<P>]: T[P] extends AggregateField<infer U> ? U : never;
46+
[P in keyof T]: T[P] extends AggregateField<infer U> ? U : never;
4747
};
4848

4949
// @public
@@ -575,9 +575,6 @@ export interface TransactionOptions {
575575
readonly maxAttempts?: number;
576576
}
577577

578-
// @public
579-
export type TrimBackticks<T> = T extends `\`${infer Body}\`` ? Body : T;
580-
581578
// @public
582579
export type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
583580

packages/firestore/lite/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export {
4343
AggregateSpec,
4444
AggregateSpecData,
4545
AggregateQuerySnapshot,
46-
AggregateType,
47-
TrimBackticks
46+
AggregateType
4847
} from '../src/lite-api/aggregate_types';
4948

5049
export { FirestoreSettings as Settings } from '../src/lite-api/settings';

packages/firestore/src/api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export {
3030
AggregateFieldType,
3131
AggregateSpec,
3232
AggregateSpecData,
33-
TrimBackticks,
3433
AggregateQuerySnapshot,
3534
AggregateType
3635
} from './lite-api/aggregate_types';

packages/firestore/src/api/aggregate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { AggregateImpl } from '../core/aggregate';
2020
import { firestoreClientRunAggregateQuery } from '../core/firestore_client';
2121
import { count } from '../lite-api/aggregate';
2222
import { AggregateQuerySnapshot } from '../lite-api/aggregate_types';
23+
import { AggregateAlias } from '../model/aggregate_alias';
2324
import { ObjectValue } from '../model/object_value';
2425
import { cast } from '../util/input_validation';
2526
import { mapToArray } from '../util/obj';
@@ -109,7 +110,7 @@ export function getAggregateFromServer<T extends AggregateSpec>(
109110

110111
const internalAggregates = mapToArray(aggregateSpec, (aggregate, alias) => {
111112
return new AggregateImpl(
112-
alias,
113+
new AggregateAlias(alias),
113114
aggregate._aggregateType,
114115
aggregate._internalFieldPath
115116
);

packages/firestore/src/core/aggregate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { AggregateAlias } from '../model/aggregate_alias';
1819
import { FieldPath } from '../model/path';
1920

2021
/**
@@ -28,7 +29,7 @@ export type AggregateType = 'count' | 'avg' | 'sum';
2829
*/
2930
export interface Aggregate {
3031
readonly fieldPath?: FieldPath;
31-
readonly alias: string;
32+
readonly alias: AggregateAlias;
3233
readonly aggregateType: AggregateType;
3334
}
3435

@@ -37,7 +38,7 @@ export interface Aggregate {
3738
*/
3839
export class AggregateImpl implements Aggregate {
3940
constructor(
40-
readonly alias: string,
41+
readonly alias: AggregateAlias,
4142
readonly aggregateType: AggregateType,
4243
readonly fieldPath?: FieldPath
4344
) {}

packages/firestore/src/lite-api/aggregate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { deepEqual } from '@firebase/util';
1919

2020
import { AggregateImpl } from '../core/aggregate';
21+
import { AggregateAlias } from '../model/aggregate_alias';
2122
import { ObjectValue } from '../model/object_value';
2223
import { invokeRunAggregationQueryRpc } from '../remote/datastore';
2324
import { cast } from '../util/input_validation';
@@ -95,7 +96,7 @@ export function getAggregate<T extends AggregateSpec>(
9596

9697
const internalAggregates = mapToArray(aggregateSpec, (aggregate, alias) => {
9798
return new AggregateImpl(
98-
alias,
99+
new AggregateAlias(alias),
99100
aggregate._aggregateType,
100101
aggregate._internalFieldPath
101102
);

packages/firestore/src/lite-api/aggregate_types.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,9 @@ export interface AggregateSpec {
6565
* from the input `AggregateSpec`.
6666
*/
6767
export type AggregateSpecData<T extends AggregateSpec> = {
68-
[P in keyof T as TrimBackticks<P>]: T[P] extends AggregateField<infer U>
69-
? U
70-
: never;
68+
[P in keyof T]: T[P] extends AggregateField<infer U> ? U : never;
7169
};
7270

73-
/**
74-
* Removes enclosing backticks.
75-
* type Foo = '`Foo`'
76-
* type TrimmedFoo = TrimBackticks<Foo>; // 'Foo'
77-
*/
78-
export type TrimBackticks<T> = T extends `\`${infer Body}\`` ? Body : T;
79-
8071
/**
8172
* The results of executing an aggregation query.
8273
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const aliasRegExp = /^[_a-zA-Z][_a-zA-Z0-9]*(?:\.[_a-zA-Z][_a-zA-Z0-9]*)*$/;
19+
20+
/**
21+
* An alias for aggregation results.
22+
* @internal
23+
*/
24+
export class AggregateAlias {
25+
/**
26+
* @internal
27+
* @param alias Un-escaped alias representation
28+
*/
29+
constructor(private alias: string) {}
30+
31+
/**
32+
* Returns true if the string could be used as an alias.
33+
*/
34+
private static isValidAlias(value: string): boolean {
35+
return aliasRegExp.test(value);
36+
}
37+
38+
/**
39+
* Return an escaped and quoted string representation of the alias.
40+
*/
41+
canonicalString(): string {
42+
let alias = this.alias.replace(/\\/g, '\\\\').replace(/`/g, '\\`');
43+
if (!AggregateAlias.isValidAlias(alias)) {
44+
alias = '`' + alias + '`';
45+
}
46+
return alias;
47+
}
48+
}

packages/firestore/src/remote/serializer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,19 +915,19 @@ export function toRunAggregationQueryRequest(
915915
aggregates.forEach(aggregate => {
916916
if (aggregate.aggregateType === 'count') {
917917
aggregations.push({
918-
alias: aggregate.alias,
918+
alias: aggregate.alias.canonicalString(),
919919
count: {}
920920
});
921921
} else if (aggregate.aggregateType === 'avg') {
922922
aggregations.push({
923-
alias: aggregate.alias,
923+
alias: aggregate.alias.canonicalString(),
924924
avg: {
925925
field: toFieldPathReference(aggregate.fieldPath!)
926926
}
927927
});
928928
} else if (aggregate.aggregateType === 'sum') {
929929
aggregations.push({
930-
alias: aggregate.alias,
930+
alias: aggregate.alias.canonicalString(),
931931
sum: {
932932
field: toFieldPathReference(aggregate.fieldPath!)
933933
}

0 commit comments

Comments
 (0)