Skip to content

Commit 4b6737f

Browse files
Merge
2 parents 72bf9b2 + 10b8556 commit 4b6737f

File tree

6 files changed

+121
-156
lines changed

6 files changed

+121
-156
lines changed

packages/firestore/lite/src/api/field_path.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { _BaseFieldPath } from '../../../src/api/field_path';
19-
import { DOCUMENT_KEY_NAME } from '../../../src/model/path';
18+
import {
19+
DOCUMENT_KEY_NAME,
20+
FieldPath as InternalFieldPath
21+
} from '../../../src/model/path';
22+
import { Code, FirestoreError } from '../../../src/util/error';
2023

2124
/**
2225
* A `FieldPath` refers to a field in a document. The path may consist of a
@@ -26,12 +29,9 @@ import { DOCUMENT_KEY_NAME } from '../../../src/model/path';
2629
* Create a `FieldPath` by providing field names. If more than one field
2730
* name is provided, the path will point to a nested field in a document.
2831
*/
29-
export class FieldPath extends _BaseFieldPath {
30-
// Note: This class is stripped down a copy of the FieldPath class in the
31-
// legacy SDK. The changes are:
32-
// - The `documentId()` static method has been removed
33-
// - Input validation is limited to errors that cannot be caught by the
34-
// TypeScript transpiler.
32+
export class FieldPath {
33+
/** Internal representation of a Firestore field path. */
34+
readonly _internalPath: InternalFieldPath;
3535

3636
/**
3737
* Creates a FieldPath from the provided field names. If more than one field
@@ -40,7 +40,17 @@ export class FieldPath extends _BaseFieldPath {
4040
* @param fieldNames A list of field names.
4141
*/
4242
constructor(...fieldNames: string[]) {
43-
super(fieldNames);
43+
for (let i = 0; i < fieldNames.length; ++i) {
44+
if (fieldNames[i].length === 0) {
45+
throw new FirestoreError(
46+
Code.INVALID_ARGUMENT,
47+
`Invalid field name at argument $(i + 1). ` +
48+
'Field names must not be empty.'
49+
);
50+
}
51+
}
52+
53+
this._internalPath = new InternalFieldPath(fieldNames);
4454
}
4555

4656
/**

packages/firestore/src/api/database.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import {
7575
} from '../util/input_validation';
7676
import { logWarn, setLogLevel as setClientLogLevel } from '../util/log';
7777
import { AutoId } from '../util/misc';
78-
import { _BaseFieldPath, FieldPath as ExternalFieldPath } from './field_path';
78+
import { FieldPath as ExpFieldPath } from '../../lite/src/api/field_path';
7979
import {
8080
CompleteFn,
8181
ErrorFn,
@@ -130,6 +130,7 @@ import {
130130
DocumentData as PublicDocumentData,
131131
DocumentReference as PublicDocumentReference,
132132
DocumentSnapshot as PublicDocumentSnapshot,
133+
FieldPath as PublicFieldPath,
133134
FirebaseFirestore as PublicFirestore,
134135
FirestoreDataConverter as PublicFirestoreDataConverter,
135136
GetOptions as PublicGetOptions,
@@ -530,28 +531,33 @@ export class Transaction implements PublicTransaction {
530531
): Transaction;
531532
update(
532533
documentRef: PublicDocumentReference<unknown>,
533-
field: string | ExternalFieldPath,
534+
field: string | PublicFieldPath,
534535
value: unknown,
535536
...moreFieldsAndValues: unknown[]
536537
): Transaction;
537538
update(
538539
documentRef: PublicDocumentReference<unknown>,
539-
fieldOrUpdateData: string | ExternalFieldPath | PublicUpdateData,
540+
fieldOrUpdateData: string | PublicFieldPath | PublicUpdateData,
540541
value?: unknown,
541542
...moreFieldsAndValues: unknown[]
542543
): Transaction {
543-
let ref;
544-
let parsed;
544+
const ref = validateReference(
545+
'Transaction.update',
546+
documentRef,
547+
this._firestore
548+
);
549+
550+
// For Compat types, we have to "extract" the underlying types before
551+
// performing validation.
552+
if (fieldOrUpdateData instanceof Compat) {
553+
fieldOrUpdateData = (fieldOrUpdateData as Compat<ExpFieldPath>)._delegate;
554+
}
545555

556+
let parsed;
546557
if (
547558
typeof fieldOrUpdateData === 'string' ||
548-
fieldOrUpdateData instanceof ExternalFieldPath
559+
fieldOrUpdateData instanceof ExpFieldPath
549560
) {
550-
ref = validateReference(
551-
'Transaction.update',
552-
documentRef,
553-
this._firestore
554-
);
555561
parsed = parseUpdateVarargs(
556562
this._dataReader,
557563
'Transaction.update',
@@ -561,11 +567,6 @@ export class Transaction implements PublicTransaction {
561567
moreFieldsAndValues
562568
);
563569
} else {
564-
ref = validateReference(
565-
'Transaction.update',
566-
documentRef,
567-
this._firestore
568-
);
569570
parsed = parseUpdateData(
570571
this._dataReader,
571572
'Transaction.update',
@@ -641,30 +642,34 @@ export class WriteBatch implements PublicWriteBatch {
641642
): WriteBatch;
642643
update(
643644
documentRef: PublicDocumentReference<unknown>,
644-
field: string | ExternalFieldPath,
645+
field: string | PublicFieldPath,
645646
value: unknown,
646647
...moreFieldsAndValues: unknown[]
647648
): WriteBatch;
648649
update(
649650
documentRef: PublicDocumentReference<unknown>,
650-
fieldOrUpdateData: string | ExternalFieldPath | PublicUpdateData,
651+
fieldOrUpdateData: string | PublicFieldPath | PublicUpdateData,
651652
value?: unknown,
652653
...moreFieldsAndValues: unknown[]
653654
): WriteBatch {
654655
this.verifyNotCommitted();
656+
const ref = validateReference(
657+
'WriteBatch.update',
658+
documentRef,
659+
this._firestore
660+
);
655661

656-
let ref;
657-
let parsed;
662+
// For Compat types, we have to "extract" the underlying types before
663+
// performing validation.
664+
if (fieldOrUpdateData instanceof Compat) {
665+
fieldOrUpdateData = (fieldOrUpdateData as Compat<ExpFieldPath>)._delegate;
666+
}
658667

668+
let parsed;
659669
if (
660670
typeof fieldOrUpdateData === 'string' ||
661-
fieldOrUpdateData instanceof ExternalFieldPath
671+
fieldOrUpdateData instanceof ExpFieldPath
662672
) {
663-
ref = validateReference(
664-
'WriteBatch.update',
665-
documentRef,
666-
this._firestore
667-
);
668673
parsed = parseUpdateVarargs(
669674
this._dataReader,
670675
'WriteBatch.update',
@@ -674,11 +679,6 @@ export class WriteBatch implements PublicWriteBatch {
674679
moreFieldsAndValues
675680
);
676681
} else {
677-
ref = validateReference(
678-
'WriteBatch.update',
679-
documentRef,
680-
this._firestore
681-
);
682682
parsed = parseUpdateData(
683683
this._dataReader,
684684
'WriteBatch.update',
@@ -830,12 +830,12 @@ export class DocumentReference<T = PublicDocumentData>
830830

831831
update(value: PublicUpdateData): Promise<void>;
832832
update(
833-
field: string | ExternalFieldPath,
833+
field: string | PublicFieldPath,
834834
value: unknown,
835835
...moreFieldsAndValues: unknown[]
836836
): Promise<void>;
837837
update(
838-
fieldOrUpdateData: string | ExternalFieldPath | PublicUpdateData,
838+
fieldOrUpdateData: string | PublicFieldPath | PublicUpdateData,
839839
value?: unknown,
840840
...moreFieldsAndValues: unknown[]
841841
): Promise<void> {
@@ -845,7 +845,7 @@ export class DocumentReference<T = PublicDocumentData>
845845
} else {
846846
return updateDoc(
847847
this._delegate,
848-
fieldOrUpdateData as string | ExternalFieldPath,
848+
fieldOrUpdateData as string | ExpFieldPath,
849849
value,
850850
...moreFieldsAndValues
851851
);
@@ -1095,7 +1095,7 @@ export class DocumentSnapshot<T = PublicDocumentData>
10951095
}
10961096

10971097
get(
1098-
fieldPath: string | ExternalFieldPath,
1098+
fieldPath: string | PublicFieldPath,
10991099
options: PublicSnapshotOptions = {}
11001100
): unknown {
11011101
if (this._document) {
@@ -1576,7 +1576,7 @@ export class Query<T = PublicDocumentData> implements PublicQuery<T> {
15761576
}
15771577

15781578
where(
1579-
field: string | ExternalFieldPath,
1579+
field: string | PublicFieldPath,
15801580
opStr: PublicWhereFilterOp,
15811581
value: unknown
15821582
): PublicQuery<T> {
@@ -1598,7 +1598,7 @@ export class Query<T = PublicDocumentData> implements PublicQuery<T> {
15981598
}
15991599

16001600
orderBy(
1601-
field: string | ExternalFieldPath,
1601+
field: string | PublicFieldPath,
16021602
directionStr?: PublicOrderByDirection
16031603
): PublicQuery<T> {
16041604
let direction: Direction;

packages/firestore/src/api/field_path.ts

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,28 @@
1717

1818
import { FieldPath as PublicFieldPath } from '@firebase/firestore-types';
1919

20+
import { FieldPath as ExpFieldPath } from '../../lite/src/api/field_path';
2021
import { FieldPath as InternalFieldPath } from '../model/path';
21-
import { Code, FirestoreError } from '../util/error';
22+
import { Compat } from '../compat/compat';
2223

2324
// The objects that are a part of this API are exposed to third-parties as
2425
// compiled javascript so we want to flag our private members with a leading
2526
// underscore to discourage their use.
2627

27-
/**
28-
* A field class base class that is shared by the lite, full and legacy SDK,
29-
* which supports shared code that deals with FieldPaths.
30-
*/
31-
// Use underscore prefix to hide this class from our Public API.
32-
// eslint-disable-next-line @typescript-eslint/naming-convention
33-
export abstract class _BaseFieldPath {
34-
/** Internal representation of a Firestore field path. */
35-
readonly _internalPath: InternalFieldPath;
36-
37-
constructor(fieldNames: string[]) {
38-
for (let i = 0; i < fieldNames.length; ++i) {
39-
if (fieldNames[i].length === 0) {
40-
throw new FirestoreError(
41-
Code.INVALID_ARGUMENT,
42-
`Invalid field name at argument $(i + 1). ` +
43-
'Field names must not be empty.'
44-
);
45-
}
46-
}
47-
48-
this._internalPath = new InternalFieldPath(fieldNames);
49-
}
50-
}
51-
5228
/**
5329
* A `FieldPath` refers to a field in a document. The path may consist of a
5430
* single field name (referring to a top-level field in the document), or a list
5531
* of field names (referring to a nested field in the document).
5632
*/
57-
export class FieldPath extends _BaseFieldPath implements PublicFieldPath {
33+
export class FieldPath extends Compat<ExpFieldPath> implements PublicFieldPath {
5834
/**
5935
* Creates a FieldPath from the provided field names. If more than one field
6036
* name is provided, the path will point to a nested field in a document.
6137
*
6238
* @param fieldNames A list of field names.
6339
*/
6440
constructor(...fieldNames: string[]) {
65-
super(fieldNames);
41+
super(new ExpFieldPath(...fieldNames));
6642
}
6743

6844
static documentId(): FieldPath {
@@ -76,37 +52,12 @@ export class FieldPath extends _BaseFieldPath implements PublicFieldPath {
7652
}
7753

7854
isEqual(other: PublicFieldPath): boolean {
79-
if (!(other instanceof FieldPath)) {
55+
if (other instanceof Compat) {
56+
other = other._delegate;
57+
}
58+
if (!(other instanceof ExpFieldPath)) {
8059
return false;
8160
}
82-
return this._internalPath.isEqual(other._internalPath);
83-
}
84-
}
85-
86-
/**
87-
* Matches any characters in a field path string that are reserved.
88-
*/
89-
const RESERVED = new RegExp('[~\\*/\\[\\]]');
90-
91-
/**
92-
* Parses a field path string into a FieldPath, treating dots as separators.
93-
*/
94-
export function fromDotSeparatedString(path: string): FieldPath {
95-
const found = path.search(RESERVED);
96-
if (found >= 0) {
97-
throw new FirestoreError(
98-
Code.INVALID_ARGUMENT,
99-
`Invalid field path (${path}). Paths must not contain ` +
100-
`'~', '*', '/', '[', or ']'`
101-
);
102-
}
103-
try {
104-
return new FieldPath(...path.split('.'));
105-
} catch (e) {
106-
throw new FirestoreError(
107-
Code.INVALID_ARGUMENT,
108-
`Invalid field path (${path}). Paths must not be empty, ` +
109-
`begin with '.', end with '.', or contain '..'`
110-
);
61+
return this._delegate._internalPath.isEqual(other._internalPath);
11162
}
11263
}

0 commit comments

Comments
 (0)