|
| 1 | +/*! |
| 2 | + * Copyright 2024 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { VectorValue } from '../lite-api/vector_value'; |
| 18 | +import { |
| 19 | + Value as ProtoValue, |
| 20 | + MapValue as ProtoMapValue |
| 21 | +} from '../protos/firestore_proto_api'; |
| 22 | + |
| 23 | +const TYPE_KEY = '__type__'; |
| 24 | +const VECTOR_VALUE_SENTINEL = '__vector__'; |
| 25 | +export const VECTOR_MAP_VECTORS_KEY = 'value'; |
| 26 | + |
| 27 | +export function isVectorValue(value: ProtoValue | null): boolean { |
| 28 | + const type = (value?.mapValue?.fields || {})[TYPE_KEY]?.stringValue; |
| 29 | + return type === VECTOR_VALUE_SENTINEL; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Creates a new VectorValue proto value (using the internal format). |
| 34 | + */ |
| 35 | +export function vectorValue(value: VectorValue): ProtoValue { |
| 36 | + const mapValue: ProtoMapValue = { |
| 37 | + fields: { |
| 38 | + [TYPE_KEY]: { |
| 39 | + stringValue: VECTOR_VALUE_SENTINEL |
| 40 | + }, |
| 41 | + [VECTOR_MAP_VECTORS_KEY]: { |
| 42 | + arrayValue: { |
| 43 | + values: value.toArray().map(value => { |
| 44 | + return { |
| 45 | + doubleValue: value |
| 46 | + }; |
| 47 | + }) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + return { mapValue }; |
| 54 | +} |
0 commit comments