Skip to content

Commit

Permalink
Fixes for Constant.of(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkDuckworth committed Dec 13, 2024
1 parent a9a0b82 commit e07e103
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/firestore/src/lite-api/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../remote/serializer';
import { hardAssert } from '../util/assert';

import { Bytes } from './bytes';
import { documentId, FieldPath } from './field_path';
import { GeoPoint } from './geo_point';
import { Pipeline } from './pipeline';
Expand Down Expand Up @@ -2084,6 +2085,8 @@ export class Constant extends Expr {

/**
* Creates a `Constant` instance for an undefined value.
* @private
* @internal
*
* @param value The undefined value.
* @return A new `Constant` instance.
Expand Down Expand Up @@ -2115,12 +2118,12 @@ export class Constant extends Expr {
static of(value: Date): Constant;

/**
* Creates a `Constant` instance for a Uint8Array value.
* Creates a `Constant` instance for a Bytes value.
*
* @param value The Uint8Array value.
* @param value The Bytes value.
* @return A new `Constant` instance.
*/
static of(value: Uint8Array): Constant;
static of(value: Bytes): Constant;

/**
* Creates a `Constant` instance for a DocumentReference value.
Expand All @@ -2130,9 +2133,9 @@ export class Constant extends Expr {
*/
static of(value: DocumentReference): Constant;

// TODO(pipeline) if we make this public, then the Proto types should also be documented
/**
* Creates a `Constant` instance for a Firestore proto value.
* For internal use only.
* @private
* @internal
* @param value The Firestore proto value.
Expand All @@ -2154,7 +2157,7 @@ export class Constant extends Expr {
* @param value The map value.
* @return A new `Constant` instance.
*/
static of(value: Map<string, any>): Constant;
static of(value: Record<string, any>): Constant;

/**
* Creates a `Constant` instance for a VectorValue value.
Expand Down
111 changes: 111 additions & 0 deletions packages/firestore/test/integration/api/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';

import { Bytes, vector } from '../../../src/api';
import { GeoPoint } from '../../../src/lite-api/geo_point';
import { Timestamp } from '../../../src/lite-api/timestamp';
import { addEqualityMatcher } from '../../util/equality_matcher';
import { Deferred } from '../../util/promise';
import {
Expand Down Expand Up @@ -476,6 +479,114 @@ apiDescribe('Pipelines', persistence => {
);
});

it('accepts and returns all data types', async () => {
const refDate = new Date();
const refTimestamp = Timestamp.now();
const constants = [
Constant.of(1).as('number'),
Constant.of('a string').as('string'),
Constant.of(true).as('boolean'),
Constant.of(null).as('null'),
Constant.of(new GeoPoint(0.1, 0.2)).as('geoPoint'),
Constant.of(refTimestamp).as('timestamp'),
Constant.of(refDate).as('date'),
Constant.of(
Bytes.fromUint8Array(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 0]))
).as('bytes'),
Constant.of(doc(firestore, 'foo', 'bar')).as('documentReference'),
Constant.of(vector([1, 2, 3])).as('vectorValue'),
Constant.of({
'number': 1,
'string': 'a string',
'boolean': true,
'null': null,
'geoPoint': new GeoPoint(0.1, 0.2),
'timestamp': refTimestamp,
'date': refDate,
'uint8Array': Bytes.fromUint8Array(
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 0])
),
'documentReference': doc(firestore, 'foo', 'bar'),
'vectorValue': vector([1, 2, 3]),
'map': {
'number': 2,
'string': 'b string'
},
'array': [1, 'c string']
}).as('map'),
Constant.of([
1,
'a string',
true,
null,
new GeoPoint(0.1, 0.2),
refTimestamp,
refDate,
Bytes.fromUint8Array(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 0])),
doc(firestore, 'foo', 'bar'),
vector([1, 2, 3]),
{
'number': 2,
'string': 'b string'
}
]).as('array')
];

const results = await randomCol
.pipeline()
.limit(1)
.select(...constants)
.execute();

expectResults(results, {
'number': 1,
'string': 'a string',
'boolean': true,
'null': null,
'geoPoint': new GeoPoint(0.1, 0.2),
'timestamp': refTimestamp,
'date': Timestamp.fromDate(refDate),
'bytes': Bytes.fromUint8Array(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 0])),
'documentReference': doc(firestore, 'foo', 'bar'),
'vectorValue': vector([1, 2, 3]),
'map': {
'number': 1,
'string': 'a string',
'boolean': true,
'null': null,
'geoPoint': new GeoPoint(0.1, 0.2),
'timestamp': refTimestamp,
'date': Timestamp.fromDate(refDate),
'uint8Array': Bytes.fromUint8Array(
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 0])
),
'documentReference': doc(firestore, 'foo', 'bar'),
'vectorValue': vector([1, 2, 3]),
'map': {
'number': 2,
'string': 'b string'
},
'array': [1, 'c string']
},
'array': [
1,
'a string',
true,
null,
new GeoPoint(0.1, 0.2),
refTimestamp,
Timestamp.fromDate(refDate),
Bytes.fromUint8Array(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 0])),
doc(firestore, 'foo', 'bar'),
vector([1, 2, 3]),
{
'number': 2,
'string': 'b string'
}
]
});
});

it('cond works', async () => {
const results = await randomCol
.pipeline()
Expand Down

0 comments on commit e07e103

Please sign in to comment.