Skip to content

Make Document.field() nullable #1927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
const value = this._document.data.field(
fieldPathFromArgument('DocumentSnapshot.get', fieldPath)
);
if (value !== undefined) {
if (value !== null) {
return this.convertValue(
value,
FieldValueOptions.fromSnapshotOptions(
Expand Down Expand Up @@ -1648,7 +1648,7 @@ export class Query implements firestore.Query {
'" is an uncommitted server timestamp. (Since the value of ' +
'this field is unknown, you cannot start/end a query with it.)'
);
} else if (value !== undefined) {
} else if (value !== null) {
components.push(value);
} else {
const field = orderBy.field.canonicalString();
Expand Down
9 changes: 3 additions & 6 deletions packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,7 @@ export class Query {
private matchesOrderBy(doc: Document): boolean {
for (const orderBy of this.explicitOrderBy) {
// order by key always matches
if (
!orderBy.field.isKeyField() &&
doc.field(orderBy.field) === undefined
) {
if (!orderBy.field.isKeyField() && doc.field(orderBy.field) === null) {
return false;
}
}
Expand Down Expand Up @@ -572,7 +569,7 @@ export class FieldFilter extends Filter {

// Only compare types with matching backend order (such as double and int).
return (
other !== undefined &&
other !== null &&
this.value.typeOrder === other.typeOrder &&
this.matchesComparison(other.compareTo(this.value))
);
Expand Down Expand Up @@ -676,7 +673,7 @@ export class InFilter extends FieldFilter {
matches(doc: Document): boolean {
const arrayValue = this.value;
const other = doc.field(this.field);
return other !== undefined && arrayValue.contains(other);
return other !== null && arrayValue.contains(other);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Document extends MaybeDocument {
this.hasCommittedMutations = !!options.hasCommittedMutations;
}

field(path: FieldPath): FieldValue | undefined {
field(path: FieldPath): FieldValue | null {
return this.data.field(path);
}

Expand Down Expand Up @@ -114,7 +114,7 @@ export class Document extends MaybeDocument {
static compareByField(field: FieldPath, d1: Document, d2: Document): number {
const v1 = d1.field(field);
const v2 = d2.field(field);
if (v1 !== undefined && v2 !== undefined) {
if (v1 !== null && v2 !== null) {
return v1.compareTo(v2);
} else {
return fail("Trying to compare documents on fields that don't exist");
Expand Down
10 changes: 5 additions & 5 deletions packages/firestore/src/model/field_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,17 +589,17 @@ export class ObjectValue extends FieldValue {
}

contains(path: FieldPath): boolean {
return this.field(path) !== undefined;
return this.field(path) !== null;
}

field(path: FieldPath): FieldValue | undefined {
field(path: FieldPath): FieldValue | null {
assert(!path.isEmpty(), "Can't get field of empty path");
let field: FieldValue | undefined = this;
let field: FieldValue | null = this;
path.forEach((pathSegment: string) => {
if (field instanceof ObjectValue) {
field = field.internalValue.get(pathSegment) || undefined;
field = field.internalValue.get(pathSegment);
} else {
field = undefined;
field = null;
}
});
return field;
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/model/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class FieldMask {
return data;
} else {
const newValue = data.field(fieldMaskPath);
if (newValue !== undefined) {
if (newValue !== null) {
filteredObject = filteredObject.set(fieldMaskPath, newValue);
}
}
Expand Down Expand Up @@ -511,7 +511,7 @@ export class PatchMutation extends Mutation {
this.fieldMask.fields.forEach(fieldPath => {
if (!fieldPath.isEmpty()) {
const newValue = this.data.field(fieldPath);
if (newValue !== undefined) {
if (newValue !== null) {
data = data.set(fieldPath, newValue);
} else {
data = data.delete(fieldPath);
Expand Down Expand Up @@ -671,7 +671,7 @@ export class TransformMutation extends Mutation {
const transform = fieldTransform.transform;
let previousValue: FieldValue | null = null;
if (baseDoc instanceof Document) {
previousValue = baseDoc.field(fieldTransform.field) || null;
previousValue = baseDoc.field(fieldTransform.field);
}
transformResults.push(
transform.applyToRemoteDocument(
Expand Down Expand Up @@ -703,7 +703,7 @@ export class TransformMutation extends Mutation {

let previousValue: FieldValue | null = null;
if (baseDoc instanceof Document) {
previousValue = baseDoc.field(fieldTransform.field) || null;
previousValue = baseDoc.field(fieldTransform.field);
}

transformResults.push(
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/test/unit/model/field_value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ describe('FieldValue', () => {
fieldValue.StringValue
);

expect(objValue.field(field('foo.a.b'))).to.equal(undefined);
expect(objValue.field(field('bar'))).to.equal(undefined);
expect(objValue.field(field('bar.a'))).to.equal(undefined);
expect(objValue.field(field('foo.a.b'))).to.be.null;
expect(objValue.field(field('bar'))).to.be.null;
expect(objValue.field(field('bar.a'))).to.be.null;

expect(objValue.field(field('foo'))!.value()).to.deep.equal({
a: 1,
Expand Down