Skip to content

Move fieldFilter (free function) to Filter.create() #988

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 5 commits into from
Jul 13, 2018
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
3 changes: 1 addition & 2 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { FirestoreClient } from '../core/firestore_client';
import {
Bound,
Direction,
fieldFilter,
Filter,
OrderBy,
Query as InternalQuery,
Expand Down Expand Up @@ -1333,7 +1332,7 @@ export class Query implements firestore.Query {
value
);
}
const filter = fieldFilter(fieldPath, relationOp, fieldValue);
const filter = Filter.create(fieldPath, relationOp, fieldValue);
this.validateNewFilter(filter);
return new Query(this._query.addFilter(filter), this.firestore);
}
Expand Down
80 changes: 41 additions & 39 deletions packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,35 @@ export class Query {
}
}

export interface Filter {
matches(doc: Document): boolean;
canonicalId(): string;
isEqual(filter: Filter): boolean;
export abstract class Filter {
abstract matches(doc: Document): boolean;
abstract canonicalId(): string;
abstract isEqual(filter: Filter): boolean;

/**
* Creates a filter based on the provided arguments.
*/
static create(field: FieldPath, op: RelationOp, value: FieldValue): Filter {
if (value.isEqual(NullValue.INSTANCE)) {
if (op !== RelationOp.EQUAL) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. You can only perform equals comparisons on null.'
);
}
return new NullFilter(field);
} else if (value.isEqual(DoubleValue.NAN)) {
if (op !== RelationOp.EQUAL) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. You can only perform equals comparisons on NaN.'
);
}
return new NanFilter(field);
} else {
return new RelationFilter(field, op, value);
}
}
}

export class RelationOp {
Expand Down Expand Up @@ -434,12 +459,14 @@ export class RelationOp {
}
}

export class RelationFilter implements Filter {
export class RelationFilter extends Filter {
constructor(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can omit constructors that match the super class constructor (here and everywhere)..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to keep them to ensure (in this case) field, op, and value are present? Or in other words, these ctors don't match the parent ctor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes, that's true. The constructors are much more powerful at second glance.

public field: FieldPath,
public op: RelationOp,
public value: FieldValue
) {}
) {
super();
}

matches(doc: Document): boolean {
if (this.field.isKeyField()) {
Expand Down Expand Up @@ -528,8 +555,10 @@ export class RelationFilter implements Filter {
/**
* Filter that matches 'null' values.
*/
export class NullFilter implements Filter {
constructor(public field: FieldPath) {}
export class NullFilter extends Filter {
constructor(public field: FieldPath) {
super();
}

matches(doc: Document): boolean {
const val = doc.field(this.field);
Expand All @@ -556,8 +585,10 @@ export class NullFilter implements Filter {
/**
* Filter that matches 'NaN' values.
*/
export class NanFilter implements Filter {
constructor(public field: FieldPath) {}
export class NanFilter extends Filter {
constructor(public field: FieldPath) {
super();
}

matches(doc: Document): boolean {
const val = doc.field(this.field).value();
Expand All @@ -581,35 +612,6 @@ export class NanFilter implements Filter {
}
}

/**
* Creates a filter based on the provided arguments.
*/
export function fieldFilter(
field: FieldPath,
op: RelationOp,
value: FieldValue
): Filter {
if (value.isEqual(NullValue.INSTANCE)) {
if (op !== RelationOp.EQUAL) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. You can only perform equals ' + 'comparisons on null.'
);
}
return new NullFilter(field);
} else if (value.isEqual(DoubleValue.NAN)) {
if (op !== RelationOp.EQUAL) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. You can only perform equals ' + 'comparisons on NaN.'
);
}
return new NanFilter(field);
} else {
return new RelationFilter(field, op, value);
}
}

/**
* The direction of sorting in an order by.
*/
Expand Down
3 changes: 1 addition & 2 deletions packages/firestore/test/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { DatabaseId } from '../../src/core/database_info';
import {
Bound,
Direction,
fieldFilter,
Filter,
OrderBy,
Query,
Expand Down Expand Up @@ -175,7 +174,7 @@ export function blob(...bytes: number[]): Blob {
export function filter(path: string, op: string, value: AnyJs): Filter {
const dataValue = wrap(value);
const operator = RelationOp.fromString(op);
return fieldFilter(field(path), operator, dataValue);
return Filter.create(field(path), operator, dataValue);
}

export function setMutation(
Expand Down