Skip to content

Commit 50a65e4

Browse files
committed
API tweaks from review. Selectable, FilterCondition, and Accumulator are abstract classes not interfaces. Removed extraneous Pipeline.sort overload
1 parent 0522ea7 commit 50a65e4

File tree

4 files changed

+200
-182
lines changed

4 files changed

+200
-182
lines changed

packages/firestore/src/core/pipeline-util.ts

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,13 @@ import {
2929
gt,
3030
lte,
3131
gte,
32-
Expr,
3332
eq,
3433
Or
3534
} from '../lite-api/expressions';
3635
import { Pipeline } from '../lite-api/pipeline';
3736
import { doc } from '../lite-api/reference';
3837
import { isNanValue, isNullValue } from '../model/values';
39-
import {
40-
ArrayValue as ProtoArrayValue,
41-
Function as ProtoFunction,
42-
LatLng as ProtoLatLng,
43-
MapValue as ProtoMapValue,
44-
Pipeline as ProtoPipeline,
45-
Timestamp as ProtoTimestamp,
46-
Value as ProtoValue
47-
} from '../protos/firestore_proto_api';
4838
import { fail } from '../util/assert';
49-
import { isPlainObject } from '../util/input_validation';
5039

5140
import { Bound } from './bound';
5241
import {
@@ -67,131 +56,6 @@ import {
6756

6857
/* eslint @typescript-eslint/no-explicit-any: 0 */
6958

70-
function isITimestamp(obj: any): obj is ProtoTimestamp {
71-
if (typeof obj !== 'object' || obj === null) {
72-
return false; // Must be a non-null object
73-
}
74-
if (
75-
'seconds' in obj &&
76-
(obj.seconds === null ||
77-
typeof obj.seconds === 'number' ||
78-
typeof obj.seconds === 'string') &&
79-
'nanos' in obj &&
80-
(obj.nanos === null || typeof obj.nanos === 'number')
81-
) {
82-
return true;
83-
}
84-
85-
return false;
86-
}
87-
function isILatLng(obj: any): obj is ProtoLatLng {
88-
if (typeof obj !== 'object' || obj === null) {
89-
return false; // Must be a non-null object
90-
}
91-
if (
92-
'latitude' in obj &&
93-
(obj.latitude === null || typeof obj.latitude === 'number') &&
94-
'longitude' in obj &&
95-
(obj.longitude === null || typeof obj.longitude === 'number')
96-
) {
97-
return true;
98-
}
99-
100-
return false;
101-
}
102-
function isIArrayValue(obj: any): obj is ProtoArrayValue {
103-
if (typeof obj !== 'object' || obj === null) {
104-
return false; // Must be a non-null object
105-
}
106-
if ('values' in obj && (obj.values === null || Array.isArray(obj.values))) {
107-
return true;
108-
}
109-
110-
return false;
111-
}
112-
function isIMapValue(obj: any): obj is ProtoMapValue {
113-
if (typeof obj !== 'object' || obj === null) {
114-
return false; // Must be a non-null object
115-
}
116-
if ('fields' in obj && (obj.fields === null || isPlainObject(obj.fields))) {
117-
return true;
118-
}
119-
120-
return false;
121-
}
122-
function isIFunction(obj: any): obj is ProtoFunction {
123-
if (typeof obj !== 'object' || obj === null) {
124-
return false; // Must be a non-null object
125-
}
126-
if (
127-
'name' in obj &&
128-
(obj.name === null || typeof obj.name === 'string') &&
129-
'args' in obj &&
130-
(obj.args === null || Array.isArray(obj.args))
131-
) {
132-
return true;
133-
}
134-
135-
return false;
136-
}
137-
138-
function isIPipeline(obj: any): obj is ProtoPipeline {
139-
if (typeof obj !== 'object' || obj === null) {
140-
return false; // Must be a non-null object
141-
}
142-
if ('stages' in obj && (obj.stages === null || Array.isArray(obj.stages))) {
143-
return true;
144-
}
145-
146-
return false;
147-
}
148-
149-
export function isFirestoreValue(obj: any): obj is ProtoValue {
150-
if (typeof obj !== 'object' || obj === null) {
151-
return false; // Must be a non-null object
152-
}
153-
154-
// Check optional properties and their types
155-
if (
156-
('nullValue' in obj &&
157-
(obj.nullValue === null || obj.nullValue === 'NULL_VALUE')) ||
158-
('booleanValue' in obj &&
159-
(obj.booleanValue === null || typeof obj.booleanValue === 'boolean')) ||
160-
('integerValue' in obj &&
161-
(obj.integerValue === null ||
162-
typeof obj.integerValue === 'number' ||
163-
typeof obj.integerValue === 'string')) ||
164-
('doubleValue' in obj &&
165-
(obj.doubleValue === null || typeof obj.doubleValue === 'number')) ||
166-
('timestampValue' in obj &&
167-
(obj.timestampValue === null || isITimestamp(obj.timestampValue))) ||
168-
('stringValue' in obj &&
169-
(obj.stringValue === null || typeof obj.stringValue === 'string')) ||
170-
('bytesValue' in obj &&
171-
(obj.bytesValue === null || obj.bytesValue instanceof Uint8Array)) ||
172-
('referenceValue' in obj &&
173-
(obj.referenceValue === null ||
174-
typeof obj.referenceValue === 'string')) ||
175-
('geoPointValue' in obj &&
176-
(obj.geoPointValue === null || isILatLng(obj.geoPointValue))) ||
177-
('arrayValue' in obj &&
178-
(obj.arrayValue === null || isIArrayValue(obj.arrayValue))) ||
179-
('mapValue' in obj &&
180-
(obj.mapValue === null || isIMapValue(obj.mapValue))) ||
181-
('fieldReferenceValue' in obj &&
182-
(obj.fieldReferenceValue === null ||
183-
typeof obj.fieldReferenceValue === 'string')) ||
184-
('functionValue' in obj &&
185-
(obj.functionValue === null || isIFunction(obj.functionValue))) ||
186-
('pipelineValue' in obj &&
187-
(obj.pipelineValue === null || isIPipeline(obj.pipelineValue)))
188-
) {
189-
return true;
190-
}
191-
192-
return false;
193-
}
194-
19559
export function toPipelineFilterCondition(f: FilterInternal): FilterCondition {
19660
if (f instanceof FieldFilterInternal) {
19761
const field = Field.of(f.field.toString());
@@ -355,7 +219,7 @@ function whereConditionsFromCursor(
355219
bound: Bound,
356220
orderings: Ordering[],
357221
position: 'before' | 'after'
358-
): Expr {
222+
): FilterCondition {
359223
const cursors = bound.position.map(value => Constant._fromProto(value));
360224
const filterFunc = position === 'before' ? lt : gt;
361225
const filterInclusiveFunc = position === 'before' ? lte : gte;

packages/firestore/src/lite-api/expressions.ts

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
/* eslint @typescript-eslint/no-explicit-any: 0 */
1919

20-
import { isFirestoreValue } from '../core/pipeline-util';
2120
import {
2221
DOCUMENT_KEY_NAME,
2322
FieldPath as InternalFieldPath
@@ -30,6 +29,7 @@ import {
3029
UserData
3130
} from '../remote/serializer';
3231
import { hardAssert } from '../util/assert';
32+
import { isFirestoreValue } from '../util/proto';
3333

3434
import { documentId, FieldPath } from './field_path';
3535
import { GeoPoint } from './geo_point';
@@ -44,45 +44,6 @@ import {
4444
} from './user_data_reader';
4545
import { VectorValue } from './vector_value';
4646

47-
/**
48-
* @beta
49-
*
50-
* An interface that represents a selectable expression.
51-
*/
52-
export interface Selectable extends Expr {
53-
selectable: true;
54-
}
55-
56-
/**
57-
* @beta
58-
*
59-
* An interface that represents a filter condition.
60-
*/
61-
export interface FilterCondition extends Expr {
62-
filterable: true;
63-
}
64-
65-
/**
66-
* @beta
67-
*
68-
* An interface that represents an accumulator.
69-
*/
70-
export interface Accumulator extends Expr {
71-
accumulator: true;
72-
/**
73-
* @private
74-
* @internal
75-
*/
76-
_toProto(serializer: JsonProtoSerializer): ProtoValue;
77-
}
78-
79-
/**
80-
* @beta
81-
*
82-
* An accumulator target, which is an expression with an alias that also implements the Accumulator interface.
83-
*/
84-
export type AccumulatorTarget = ExprWithAlias<Accumulator>;
85-
8647
/**
8748
* @beta
8849
*
@@ -1821,10 +1782,50 @@ export abstract class Expr implements ProtoSerializable<ProtoValue>, UserData {
18211782
abstract _readUserData(dataReader: UserDataReader): void;
18221783
}
18231784

1785+
/**
1786+
* @beta
1787+
*
1788+
* An interface that represents a selectable expression.
1789+
*/
1790+
export abstract class Selectable extends Expr {
1791+
selectable: true = true;
1792+
}
1793+
1794+
/**
1795+
* @beta
1796+
*
1797+
* An interface that represents a filter condition.
1798+
*/
1799+
export abstract class FilterCondition extends Expr {
1800+
filterable: true = true;
1801+
}
1802+
1803+
/**
1804+
* @beta
1805+
*
1806+
* An interface that represents an accumulator.
1807+
*/
1808+
export abstract class Accumulator extends Expr {
1809+
accumulator: true = true;
1810+
1811+
/**
1812+
* @private
1813+
* @internal
1814+
*/
1815+
abstract _toProto(serializer: JsonProtoSerializer): ProtoValue;
1816+
}
1817+
1818+
/**
1819+
* @beta
1820+
*
1821+
* An accumulator target, which is an expression with an alias that also implements the Accumulator interface.
1822+
*/
1823+
export type AccumulatorTarget = ExprWithAlias<Accumulator>;
1824+
18241825
/**
18251826
* @beta
18261827
*/
1827-
export class ExprWithAlias<T extends Expr> extends Expr implements Selectable {
1828+
export class ExprWithAlias<T extends Expr> extends Selectable {
18281829
exprType: ExprType = 'ExprWithAlias';
18291830
selectable = true as const;
18301831

@@ -1897,7 +1898,7 @@ class ListOfExprs extends Expr {
18971898
* const cityField = Field.of("address.city");
18981899
* ```
18991900
*/
1900-
export class Field extends Expr implements Selectable {
1901+
export class Field extends Selectable {
19011902
exprType: ExprType = 'Field';
19021903
selectable = true as const;
19031904

@@ -1927,7 +1928,6 @@ export class Field extends Expr implements Selectable {
19271928
*/
19281929
static of(name: string): Field;
19291930
static of(path: FieldPath): Field;
1930-
static of(pipeline: Pipeline, name: string): Field;
19311931
static of(
19321932
pipelineOrName: Pipeline | string | FieldPath,
19331933
name?: string
@@ -1974,7 +1974,7 @@ export class Field extends Expr implements Selectable {
19741974
/**
19751975
* @beta
19761976
*/
1977-
export class Fields extends Expr implements Selectable {
1977+
export class Fields extends Selectable {
19781978
exprType: ExprType = 'Field';
19791979
selectable = true as const;
19801980

packages/firestore/src/lite-api/pipeline.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,6 @@ export class Pipeline implements ProtoSerializable<ExecutePipelineRequest> {
620620
* @return A new {@code Pipeline} object with this stage appended to the stage list.
621621
*/
622622
sort(...orderings: Ordering[]): Pipeline;
623-
sort(options: { orderings: Ordering[] }): Pipeline;
624623
sort(
625624
optionsOrOrderings:
626625
| Ordering

0 commit comments

Comments
 (0)