Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/reflect/unsafe.js

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