Skip to content

Commit e0311c2

Browse files
Compat class for FieldPath
1 parent 9d424f5 commit e0311c2

File tree

6 files changed

+91
-121
lines changed

6 files changed

+91
-121
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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ import {
7777
} from '../util/input_validation';
7878
import { logWarn, setLogLevel as setClientLogLevel } from '../util/log';
7979
import { AutoId } from '../util/misc';
80-
import { _BaseFieldPath, FieldPath as ExternalFieldPath } from './field_path';
80+
import { FieldPath as ExternalFieldPath } from './field_path';
8181
import {
8282
CompleteFn,
8383
ErrorFn,
@@ -828,21 +828,25 @@ export class DocumentReference<T = DocumentData>
828828
...moreFieldsAndValues: unknown[]
829829
): Promise<void>;
830830
update(
831-
fieldOrUpdateData: string | ExternalFieldPath | UpdateData,
831+
fieldOrUpdateData:
832+
| string
833+
| ExternalFieldPath
834+
| Compat<ExternalFieldPath>
835+
| UpdateData,
832836
value?: unknown,
833837
...moreFieldsAndValues: unknown[]
834838
): Promise<void> {
835839
// For Compat types, we have to "extract" the underlying types before
836840
// performing validation.
837841
if (fieldOrUpdateData instanceof Compat) {
838-
fieldOrUpdateData = (fieldOrUpdateData as Compat<_BaseFieldPath>)
842+
fieldOrUpdateData = (fieldOrUpdateData as Compat<ExternalFieldPath>)
839843
._delegate;
840844
}
841845

842846
let parsed;
843847
if (
844848
typeof fieldOrUpdateData === 'string' ||
845-
fieldOrUpdateData instanceof _BaseFieldPath
849+
fieldOrUpdateData instanceof ExternalFieldPath
846850
) {
847851
parsed = parseUpdateVarargs(
848852
this._dataReader,

packages/firestore/src/api/field_path.ts

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,32 @@
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));
42+
}
43+
44+
get _internalPath(): InternalFieldPath {
45+
return this._delegate._internalPath;
6646
}
6747

6848
static documentId(): FieldPath {
@@ -82,31 +62,3 @@ export class FieldPath extends _BaseFieldPath implements PublicFieldPath {
8262
return this._internalPath.isEqual(other._internalPath);
8363
}
8464
}
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-
);
111-
}
112-
}

0 commit comments

Comments
 (0)