Skip to content

Commit 15f2090

Browse files
Remove JSON.stringify
1 parent a92a5c8 commit 15f2090

File tree

7 files changed

+58
-17
lines changed

7 files changed

+58
-17
lines changed

packages/firestore/src/model/field_value.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ export class ObjectValue extends FieldValue {
614614
}
615615

616616
toString(): string {
617-
return JSON.stringify(this.value());
617+
return this.internalValue.toString();
618618
}
619619

620620
private child(childName: string): FieldValue | undefined {
@@ -688,6 +688,7 @@ export class ArrayValue extends FieldValue {
688688
}
689689

690690
toString(): string {
691-
return JSON.stringify(this.value());
691+
const descriptions = this.internalValue.map(v => v.toString());
692+
return `[${descriptions.join(',')}]`;
692693
}
693694
}

packages/firestore/src/model/path.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const DOCUMENT_KEY_NAME = '__name__';
2323
/**
2424
* Path represents an ordered sequence of string segments.
2525
*/
26-
export abstract class Path {
26+
abstract class Path {
2727
private segments: string[];
2828
private offset: number;
2929
private len: number;
@@ -32,6 +32,14 @@ export abstract class Path {
3232
this.init(segments, offset, length);
3333
}
3434

35+
/**
36+
* Returns a String representation.
37+
*
38+
* Implementing classes are required to provide idempotent implementations as
39+
* the String representation is used to obtain canonical Query IDs.
40+
*/
41+
abstract toString(): string;
42+
3543
/**
3644
* An initialization method that can be called from outside the constructor.
3745
* We need this so that we can have a non-static construct method that returns
@@ -73,10 +81,6 @@ export abstract class Path {
7381
return this.len;
7482
}
7583

76-
toJSON(): string[] {
77-
return this.segments.slice(this.offset, this.limit());
78-
}
79-
8084
isEqual(other: Path): boolean {
8185
return Path.comparator(this, other) === 0;
8286
}

packages/firestore/src/util/sorted_map.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ export class SortedMap<K, V> {
143143
});
144144
}
145145

146+
toString(): string {
147+
const descriptions: string[] = [];
148+
this.inorderTraversal((k, v) => {
149+
descriptions.push(`${k}:${v}`);
150+
return false;
151+
});
152+
return `{${descriptions.join(', ')}}`;
153+
}
154+
146155
// Traverses the map in reverse key order and calls the specified action
147156
// function for each key/value pair. If action returns true, traversal is
148157
// aborted.

packages/firestore/test/integration/api/query.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,13 @@ apiDescribe('Queries', persistence => {
703703
ref: ref.firestore.doc('f/c'),
704704
geoPoint: new GeoPoint(0, 0),
705705
buffer: Blob.fromBase64String('Zm9v'),
706-
time: Timestamp.now()
706+
time: Timestamp.now(),
707+
array: [
708+
ref.firestore.doc('f/c'),
709+
new GeoPoint(0, 0),
710+
Blob.fromBase64String('Zm9v'),
711+
Timestamp.now()
712+
]
707713
};
708714
await ref.add({ data });
709715

packages/firestore/test/unit/core/query.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,27 @@ describe('Query', () => {
383383
// .addOrderBy(orderBy(DOCUMENT_KEY_NAME, 'desc'))
384384
// .withUpperBound(lip3, 'exclusive');
385385

386+
const relativeReference = ref('project1/database1', 'col/doc');
387+
const absoluteReference = ref(
388+
'project1/database1',
389+
'projects/project1/databases/database1/documents/col/doc',
390+
5
391+
);
392+
393+
const q16a = Query.atPath(path('foo')).addFilter(
394+
filter('object', '==', { ref: relativeReference })
395+
);
396+
const q16b = Query.atPath(path('foo')).addFilter(
397+
filter('object', '==', { ref: absoluteReference })
398+
);
399+
400+
const q17a = Query.atPath(path('foo')).addFilter(
401+
filter('array', '==', [relativeReference])
402+
);
403+
const q17b = Query.atPath(path('foo')).addFilter(
404+
filter('array', '==', [absoluteReference])
405+
);
406+
386407
const queries = [
387408
[q1a, q1b],
388409
[q2a, q2b],
@@ -396,9 +417,11 @@ describe('Query', () => {
396417
[q10a],
397418
[q11a],
398419
[q12a],
399-
[q13a]
420+
[q13a],
400421
//[q14a],
401422
//[q15a],
423+
[q16a, q16b],
424+
[q17a, q17b]
402425
];
403426

404427
expectEqualitySets(queries, (q1, q2) => {

packages/firestore/test/unit/model/path.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ describe('Path', () => {
171171
expect(abc.isPrefixOf(ba)).to.equal(false);
172172
});
173173

174-
it('respects offset during JSON serialization', () => {
175-
const path1 = new ResourcePath(['c']);
176-
const path2 = new ResourcePath(['a', 'b', 'c'], 2);
177-
expect(JSON.stringify(path1)).to.equal(JSON.stringify(path2));
178-
});
179-
180174
it('can be constructed from field path.', () => {
181175
const path = FieldPath.fromServerFormat('foo\\..bar\\\\.baz');
182176
expect(path.toArray()).to.deep.equal(['foo.', 'bar\\', 'baz']);

packages/firestore/test/util/helpers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,14 @@ export function version(v: TestSnapshotVersion): SnapshotVersion {
105105
return SnapshotVersion.fromMicroseconds(v);
106106
}
107107

108-
export function ref(dbIdStr: string, keyStr: string): DocumentKeyReference {
108+
export function ref(
109+
dbIdStr: string,
110+
keyStr: string,
111+
offset?: number
112+
): DocumentKeyReference {
109113
const [project, database] = dbIdStr.split('/', 2);
110114
const dbId = new DatabaseId(project, database);
111-
return new DocumentKeyReference(dbId, key(keyStr));
115+
return new DocumentKeyReference(dbId, new DocumentKey(path(keyStr, offset)));
112116
}
113117

114118
export function doc(

0 commit comments

Comments
 (0)