Skip to content

Commit a974933

Browse files
committed
Tests and fixes
1 parent cc466c2 commit a974933

File tree

15 files changed

+203
-74
lines changed

15 files changed

+203
-74
lines changed

common/api-review/firestore-lite.api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,16 @@ export function updateDoc<AppModelType, DbModelType extends DocumentData>(refere
460460
// @public
461461
export function updateDoc<AppModelType, DbModelType extends DocumentData>(reference: DocumentReference<AppModelType, DbModelType>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;
462462

463+
// @public
464+
export function vector(values?: number[]): VectorValue;
465+
466+
// @public
467+
export class VectorValue {
468+
/* Excluded from this release type: __constructor */
469+
isEqual(other: VectorValue): boolean;
470+
toArray(): number[];
471+
}
472+
463473
// @public
464474
export function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: unknown): QueryFieldFilterConstraint;
465475

common/api-review/firestore.api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,16 @@ export function updateDoc<AppModelType, DbModelType extends DocumentData>(refere
745745
// @public
746746
export function updateDoc<AppModelType, DbModelType extends DocumentData>(reference: DocumentReference<AppModelType, DbModelType>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;
747747

748+
// @public
749+
export function vector(values?: number[]): VectorValue;
750+
751+
// @public
752+
export class VectorValue {
753+
/* Excluded from this release type: __constructor */
754+
isEqual(other: VectorValue): boolean;
755+
toArray(): number[];
756+
}
757+
748758
// @public
749759
export function waitForPendingWrites(firestore: Firestore): Promise<void>;
750760

packages/firestore/lite/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export {
127127
arrayRemove,
128128
arrayUnion,
129129
serverTimestamp,
130-
deleteField
130+
deleteField,
131+
vector
131132
} from '../src/lite-api/field_value_impl';
132133

133134
export {
@@ -138,6 +139,8 @@ export {
138139
snapshotEqual
139140
} from '../src/lite-api/snapshot';
140141

142+
export { VectorValue } from '../src/lite-api/vector_value';
143+
141144
export { WriteBatch, writeBatch } from '../src/lite-api/write_batch';
142145

143146
export { TransactionOptions } from '../src/lite-api/transaction_options';

packages/firestore/src/api.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,12 @@ export {
172172
arrayUnion,
173173
deleteField,
174174
increment,
175-
serverTimestamp
175+
serverTimestamp,
176+
vector
176177
} from './api/field_value_impl';
177178

179+
export { VectorValue } from './lite-api/vector_value';
180+
178181
export { LogLevelString as LogLevel, setLogLevel } from './util/log';
179182

180183
export { Bytes } from './api/bytes';

packages/firestore/src/api/field_value_impl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export {
2020
arrayRemove,
2121
arrayUnion,
2222
serverTimestamp,
23-
deleteField
23+
deleteField,
24+
vector
2425
} from '../lite-api/field_value_impl';

packages/firestore/src/index/firestore_index_value_writer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
*/
1717

1818
import { DocumentKey } from '../model/document_key';
19-
import { isVectorValue, VECTOR_MAP_VECTORS_KEY } from '../model/map_type';
2019
import {
2120
normalizeByteString,
2221
normalizeNumber,
2322
normalizeTimestamp
2423
} from '../model/normalize';
25-
import { isMaxValue } from '../model/values';
24+
import {
25+
isVectorValue,
26+
VECTOR_MAP_VECTORS_KEY,
27+
isMaxValue
28+
} from '../model/values';
2629
import { ArrayValue, MapValue, Value } from '../protos/firestore_proto_api';
2730
import { fail } from '../util/assert';
2831
import { isNegativeZero } from '../util/types';

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { ParseContext } from '../api/parse_context';
2626
import { DatabaseId } from '../core/database_info';
2727
import { DocumentKey } from '../model/document_key';
2828
import { FieldMask } from '../model/field_mask';
29-
import { vectorValue } from '../model/map_type';
3029
import {
3130
FieldTransform,
3231
Mutation,
@@ -42,6 +41,11 @@ import {
4241
NumericIncrementTransformOperation,
4342
ServerTimestampTransform
4443
} from '../model/transform_operation';
44+
import {
45+
TYPE_KEY,
46+
VECTOR_MAP_VECTORS_KEY,
47+
VECTOR_VALUE_SENTINEL
48+
} from '../model/values';
4549
import { newSerializer } from '../platform/serializer';
4650
import {
4751
MapValue as ProtoMapValue,
@@ -903,16 +907,39 @@ function parseScalarValue(
903907
value._key.path
904908
)
905909
};
906-
}
907-
if (value instanceof VectorValue) {
908-
return vectorValue(value);
910+
} else if (value instanceof VectorValue) {
911+
return parseVectorValue(value);
909912
} else {
910913
throw context.createError(
911914
`Unsupported field value: ${valueDescription(value)}`
912915
);
913916
}
914917
}
915918

919+
/**
920+
* Creates a new VectorValue proto value (using the internal format).
921+
*/
922+
export function parseVectorValue(value: VectorValue): ProtoValue {
923+
const mapValue: ProtoMapValue = {
924+
fields: {
925+
[TYPE_KEY]: {
926+
stringValue: VECTOR_VALUE_SENTINEL
927+
},
928+
[VECTOR_MAP_VECTORS_KEY]: {
929+
arrayValue: {
930+
values: value.toArray().map(value => {
931+
return {
932+
doubleValue: value
933+
};
934+
})
935+
}
936+
}
937+
}
938+
};
939+
940+
return { mapValue };
941+
}
942+
916943
/**
917944
* Checks whether an object looks like a JSON object that should be converted
918945
* into a struct. Normal class/prototype instances are considered to look like

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { DocumentData } from '@firebase/firestore-types';
1919

2020
import { DatabaseId } from '../core/database_info';
2121
import { DocumentKey } from '../model/document_key';
22-
import { VECTOR_MAP_VECTORS_KEY } from '../model/map_type';
2322
import {
2423
normalizeByteString,
2524
normalizeNumber,
@@ -31,7 +30,7 @@ import {
3130
getPreviousValue
3231
} from '../model/server_timestamps';
3332
import { TypeOrder } from '../model/type_order';
34-
import { typeOrder } from '../model/values';
33+
import { VECTOR_MAP_VECTORS_KEY, typeOrder } from '../model/values';
3534
import {
3635
ApiClientObjectMap,
3736
ArrayValue as ProtoArrayValue,

packages/firestore/src/model/map_type.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/firestore/src/model/values.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
LatLng,
2222
MapValue,
2323
Timestamp,
24+
Value as ProtoValue,
2425
Value
2526
} from '../protos/firestore_proto_api';
2627
import { fail } from '../util/assert';
@@ -29,7 +30,6 @@ import { forEach, objectSize } from '../util/obj';
2930
import { isNegativeZero } from '../util/types';
3031

3132
import { DocumentKey } from './document_key';
32-
import { isVectorValue, VECTOR_MAP_VECTORS_KEY } from './map_type';
3333
import {
3434
normalizeByteString,
3535
normalizeNumber,
@@ -42,6 +42,7 @@ import {
4242
} from './server_timestamps';
4343
import { TypeOrder } from './type_order';
4444

45+
export const TYPE_KEY = '__type__';
4546
const MAX_VALUE_TYPE = '__max__';
4647
export const MAX_VALUE: Value = {
4748
mapValue: {
@@ -51,6 +52,9 @@ export const MAX_VALUE: Value = {
5152
}
5253
};
5354

55+
export const VECTOR_VALUE_SENTINEL = '__vector__';
56+
export const VECTOR_MAP_VECTORS_KEY = 'value';
57+
5458
export const MIN_VALUE: Value = {
5559
nullValue: 'NULL_VALUE'
5660
};
@@ -615,6 +619,12 @@ export function isMapValue(
615619
return !!value && 'mapValue' in value;
616620
}
617621

622+
/** Returns true if `value` is a VetorValue. */
623+
export function isVectorValue(value: ProtoValue | null): boolean {
624+
const type = (value?.mapValue?.fields || {})[TYPE_KEY]?.stringValue;
625+
return type === VECTOR_VALUE_SENTINEL;
626+
}
627+
618628
/** Creates a deep copy of `source`. */
619629
export function deepClone(source: Value): Value {
620630
if (source.geoPointValue) {
@@ -650,6 +660,17 @@ export function isMaxValue(value: Value): boolean {
650660
);
651661
}
652662

663+
export const MIN_VECTOR_VALUE = {
664+
mapValue: {
665+
fields: {
666+
[TYPE_KEY]: { stringValue: VECTOR_VALUE_SENTINEL },
667+
[VECTOR_MAP_VECTORS_KEY]: {
668+
arrayValue: {}
669+
}
670+
}
671+
}
672+
};
673+
653674
/** Returns the lowest value for the given value type (inclusive). */
654675
export function valuesGetLowerBound(value: Value): Value {
655676
if ('nullValue' in value) {
@@ -671,6 +692,9 @@ export function valuesGetLowerBound(value: Value): Value {
671692
} else if ('arrayValue' in value) {
672693
return { arrayValue: {} };
673694
} else if ('mapValue' in value) {
695+
if (isVectorValue(value)) {
696+
return MIN_VECTOR_VALUE;
697+
}
674698
return { mapValue: {} };
675699
} else {
676700
return fail('Invalid value type: ' + JSON.stringify(value));
@@ -696,8 +720,11 @@ export function valuesGetUpperBound(value: Value): Value {
696720
} else if ('geoPointValue' in value) {
697721
return { arrayValue: {} };
698722
} else if ('arrayValue' in value) {
699-
return { mapValue: {} };
723+
return MIN_VECTOR_VALUE;
700724
} else if ('mapValue' in value) {
725+
if (isVectorValue(value)) {
726+
return { mapValue: {} };
727+
}
701728
return MAX_VALUE;
702729
} else {
703730
return fail('Invalid value type: ' + JSON.stringify(value));

0 commit comments

Comments
 (0)