| 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 { unsafeClear, unsafeIsSet } from "./reflect/unsafe.js"; |
| 15 |
/** |
| 16 |
* Returns true if the field is set. |
| 17 |
* |
| 18 |
* - Scalar and enum fields with implicit presence (proto3): |
| 19 |
* Set if not a zero value. |
| 20 |
* |
| 21 |
* - Scalar and enum fields with explicit presence (proto2, oneof): |
| 22 |
* Set if a value was set when creating or parsing the message, or when a |
| 23 |
* value was assigned to the field's property. |
| 24 |
* |
| 25 |
* - Message fields: |
| 26 |
* Set if the property is not undefined. |
| 27 |
* |
| 28 |
* - List and map fields: |
| 29 |
* Set if not empty. |
| 30 |
*/ |
| 31 |
export function isFieldSet(message, field) { |
| 32 |
return (field.parent.typeName == message.$typeName && unsafeIsSet(message, field)); |
| 33 |
} |
| 34 |
/** |
| 35 |
* Resets the field, so that isFieldSet() will return false. |
| 36 |
*/ |
| 37 |
export function clearField(message, field) { |
| 38 |
if (field.parent.typeName == message.$typeName) { |
| 39 |
unsafeClear(message, field); |
| 40 |
} |
| 41 |
} |