| 1 |
// Copyright 2021-2026 Buf Technologies, Inc. |
| 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 |
import { isScalarZeroValue, scalarZeroValue } from "./scalar.js"; |
| 15 |
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name = $number; |
| 16 |
const IMPLICIT = 2; |
| 17 |
export const unsafeLocal = Symbol.for("reflect unsafe local"); |
| 18 |
/** |
| 19 |
* Return the selected field of a oneof group. |
| 20 |
* |
| 21 |
* @private |
| 22 |
*/ |
| 23 |
export function unsafeOneofCase( |
| 24 |
// biome-ignore lint/suspicious/noExplicitAny: `any` is the best choice for dynamic access |
| 25 |
target, oneof) { |
| 26 |
const c = target[oneof.localName].case; |
| 27 |
if (c === undefined) { |
| 28 |
return c; |
| 29 |
} |
| 30 |
return oneof.fields.find((f) => f.localName === c); |
| 31 |
} |
| 32 |
/** |
| 33 |
* Returns true if the field is set. |
| 34 |
* |
| 35 |
* @private |
| 36 |
*/ |
| 37 |
export function unsafeIsSet( |
| 38 |
// biome-ignore lint/suspicious/noExplicitAny: `any` is the best choice for dynamic access |
| 39 |
target, field) { |
| 40 |
const name = field.localName; |
| 41 |
if (field.oneof) { |
| 42 |
return target[field.oneof.localName].case === name; |
| 43 |
} |
| 44 |
if (field.presence != IMPLICIT) { |
| 45 |
// Fields with explicit presence have properties on the prototype chain |
| 46 |
// for default / zero values (except for proto3). |
| 47 |
return (target[name] !== undefined && |
| 48 |
Object.prototype.hasOwnProperty.call(target, name)); |
| 49 |
} |
| 50 |
switch (field.fieldKind) { |
| 51 |
case "list": |
| 52 |
return target[name].length > 0; |
| 53 |
case "map": |
| 54 |
return Object.keys(target[name]).length > 0; |
| 55 |
case "scalar": |
| 56 |
return !isScalarZeroValue(field.scalar, target[name]); |
| 57 |
case "enum": |
| 58 |
return target[name] !== field.enum.values[0].number; |
| 59 |
} |
| 60 |
throw new Error("message field with implicit presence"); |
| 61 |
} |
| 62 |
/** |
| 63 |
* Returns true if the field is set, but only for singular fields with explicit |
| 64 |
* presence (proto2). |
| 65 |
* |
| 66 |
* @private |
| 67 |
*/ |
| 68 |
export function unsafeIsSetExplicit(target, localName) { |
| 69 |
return (Object.prototype.hasOwnProperty.call(target, localName) && |
| 70 |
target[localName] !== undefined); |
| 71 |
} |
| 72 |
/** |
| 73 |
* Return a field value, respecting oneof groups. |
| 74 |
* |
| 75 |
* @private |
| 76 |
*/ |
| 77 |
export function unsafeGet(target, field) { |
| 78 |
if (field.oneof) { |
| 79 |
const oneof = target[field.oneof.localName]; |
| 80 |
if (oneof.case === field.localName) { |
| 81 |
return oneof.value; |
| 82 |
} |
| 83 |
return undefined; |
| 84 |
} |
| 85 |
return target[field.localName]; |
| 86 |
} |
| 87 |
/** |
| 88 |
* Set a field value, respecting oneof groups. |
| 89 |
* |
| 90 |
* @private |
| 91 |
*/ |
| 92 |
export function unsafeSet(target, field, value) { |
| 93 |
if (field.oneof) { |
| 94 |
target[field.oneof.localName] = { |
| 95 |
case: field.localName, |
| 96 |
value: value, |
| 97 |
}; |
| 98 |
} |
| 99 |
else { |
| 100 |
target[field.localName] = value; |
| 101 |
} |
| 102 |
} |
| 103 |
/** |
| 104 |
* Resets the field, so that unsafeIsSet() will return false. |
| 105 |
* |
| 106 |
* @private |
| 107 |
*/ |
| 108 |
export function unsafeClear( |
| 109 |
// biome-ignore lint/suspicious/noExplicitAny: `any` is the best choice for dynamic access |
| 110 |
target, field) { |
| 111 |
const name = field.localName; |
| 112 |
if (field.oneof) { |
| 113 |
const oneofLocalName = field.oneof.localName; |
| 114 |
if (target[oneofLocalName].case === name) { |
| 115 |
target[oneofLocalName] = { case: undefined }; |
| 116 |
} |
| 117 |
} |
| 118 |
else if (field.presence != IMPLICIT) { |
| 119 |
// Fields with explicit presence have properties on the prototype chain |
| 120 |
// for default / zero values (except for proto3). By deleting their own |
| 121 |
// property, the field is reset. |
| 122 |
delete target[name]; |
| 123 |
} |
| 124 |
else { |
| 125 |
switch (field.fieldKind) { |
| 126 |
case "map": |
| 127 |
target[name] = {}; |
| 128 |
break; |
| 129 |
case "list": |
| 130 |
target[name] = []; |
| 131 |
break; |
| 132 |
case "enum": |
| 133 |
target[name] = field.enum.values[0].number; |
| 134 |
break; |
| 135 |
case "scalar": |
| 136 |
target[name] = scalarZeroValue(field.scalar, field.longAsString); |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
} |