|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import {ParseContext} from "../api/parse_context"; |
| 16 | +import { |
| 17 | + parseData, |
| 18 | +} from "../lite-api/user_data_reader"; |
| 19 | +import { ObjectValue } from '../model/object_value'; |
| 20 | +import { FieldPath } from '../model/path'; |
| 21 | +import {ApiClientObjectMap, Value} from "../protos/firestore_proto_api"; |
| 22 | +import {isPlainObject} from "../util/input_validation"; |
| 23 | +import { mapToArray } from '../util/obj'; |
| 24 | +export type OptionsDefinitions = Record<string, OptionDefinition>; |
| 25 | +export interface OptionDefinition { |
| 26 | + serverName: string; |
| 27 | + nestedOptions?: OptionsDefinitions; |
| 28 | +} |
| 29 | + |
| 30 | +export class OptionsUtil { |
| 31 | + constructor(private optionDefinitions: OptionsDefinitions) {} |
| 32 | + |
| 33 | + private _getKnownOptions( |
| 34 | + options: Record<string, unknown>, |
| 35 | + context: ParseContext |
| 36 | + ): ObjectValue { |
| 37 | + const knownOptions: ObjectValue = ObjectValue.empty(); |
| 38 | + |
| 39 | + // SERIALIZE KNOWN OPTIONS |
| 40 | + for (const knownOptionKey in this.optionDefinitions) { |
| 41 | + if (this.optionDefinitions.hasOwnProperty(knownOptionKey)) { |
| 42 | + const optionDefinition: OptionDefinition = |
| 43 | + this.optionDefinitions[knownOptionKey]; |
| 44 | + |
| 45 | + if (knownOptionKey in options) { |
| 46 | + const optionValue: unknown = options[knownOptionKey]; |
| 47 | + let protoValue: Value | undefined = undefined; |
| 48 | + |
| 49 | + if (optionDefinition.nestedOptions && isPlainObject(optionValue)) { |
| 50 | + const nestedUtil = new OptionsUtil(optionDefinition.nestedOptions); |
| 51 | + protoValue = { |
| 52 | + mapValue: { |
| 53 | + fields: nestedUtil.getOptionsProto(context, optionValue), |
| 54 | + }, |
| 55 | + }; |
| 56 | + } else if (optionValue) { |
| 57 | + protoValue = parseData(optionValue, context) ?? undefined; |
| 58 | + } |
| 59 | + |
| 60 | + if (protoValue) { |
| 61 | + knownOptions.set( |
| 62 | + FieldPath.fromServerFormat(optionDefinition.serverName), |
| 63 | + protoValue |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return knownOptions; |
| 71 | + } |
| 72 | + |
| 73 | + getOptionsProto( |
| 74 | + context: ParseContext, |
| 75 | + knownOptions: Record<string, unknown>, |
| 76 | + optionsOverride?: Record<string, unknown> |
| 77 | + ): ApiClientObjectMap<Value> | undefined { |
| 78 | + const result: ObjectValue = this._getKnownOptions(knownOptions, context); |
| 79 | + |
| 80 | + // APPLY OPTIONS OVERRIDES |
| 81 | + if (optionsOverride) { |
| 82 | + const optionsMap = new Map( |
| 83 | + mapToArray(optionsOverride, (value, key) => [ |
| 84 | + FieldPath.fromServerFormat(key), |
| 85 | + value !== undefined ? parseData(value, context) : null, |
| 86 | + ]) |
| 87 | + ); |
| 88 | + result.setAll(optionsMap); |
| 89 | + } |
| 90 | + |
| 91 | + // Return MapValue from `result` or empty map value |
| 92 | + return result.value.mapValue.fields ?? {}; |
| 93 | + } |
| 94 | +} |
0 commit comments