You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.4 KiB

// Copyright 2021-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { protoInt64 } from "../proto-int64.js";
import { ScalarType } from "../descriptors.js";
/**
* Returns true if both scalar values are equal.
*/
export function scalarEquals(type, a, b) {
if (a === b) {
// This correctly matches equal values except BYTES and (possibly) 64-bit integers.
return true;
}
// Special case BYTES - we need to compare each byte individually
if (type == ScalarType.BYTES) {
if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array)) {
return false;
}
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
// Special case 64-bit integers - we support number, string and bigint representation.
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (type) {
case ScalarType.UINT64:
case ScalarType.FIXED64:
case ScalarType.INT64:
case ScalarType.SFIXED64:
case ScalarType.SINT64:
// Loose comparison will match between 0n, 0 and "0".
return a == b;
}
// Anything that hasn't been caught by strict comparison or special cased
// BYTES and 64-bit integers is not equal.
return false;
}
/**
* Returns the zero value for the given scalar type.
*/
export function scalarZeroValue(type, longAsString) {
switch (type) {
case ScalarType.STRING:
return "";
case ScalarType.BOOL:
return false;
default:
// Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
// We do not use individual cases to save a few bytes code size.
return 0;
case ScalarType.DOUBLE:
case ScalarType.FLOAT:
return 0.0;
case ScalarType.INT64:
case ScalarType.UINT64:
case ScalarType.SFIXED64:
case ScalarType.FIXED64:
case ScalarType.SINT64:
return (longAsString ? "0" : protoInt64.zero);
case ScalarType.BYTES:
return new Uint8Array(0);
}
}
/**
* Returns true for a zero-value. For example, an integer has the zero-value `0`,
* a boolean is `false`, a string is `""`, and bytes is an empty Uint8Array.
*
* In proto3, zero-values are not written to the wire, unless the field is
* optional or repeated.
*/
export function isScalarZeroValue(type, value) {
switch (type) {
case ScalarType.BOOL:
return value === false;
case ScalarType.STRING:
return value === "";
case ScalarType.BYTES:
return value instanceof Uint8Array && !value.byteLength;
default:
return value == 0; // Loose comparison matches 0n, 0 and "0"
}
}