| 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 { scalarEquals } from "./reflect/scalar.js"; |
| 15 |
import { reflect } from "./reflect/reflect.js"; |
| 16 |
import { ScalarType, } from "./descriptors.js"; |
| 17 |
import { anyUnpack } from "./wkt/index.js"; |
| 18 |
import { createExtensionContainer, getExtension } from "./extensions.js"; |
| 19 |
/** |
| 20 |
* Compare two messages of the same type for equality. |
| 21 |
* |
| 22 |
* Fields are compared one by one, but only when set: a field set in one message |
| 23 |
* but not the other makes them unequal, while fields set in both are compared |
| 24 |
* by value. Extensions and unknown fields are disregarded by default. |
| 25 |
* |
| 26 |
* Float and double values are compared with IEEE semantics: NaN does not equal |
| 27 |
* NaN, and -0 equals 0 (with the exception for singular fields with implicit |
| 28 |
* presence: -0 is set, +0 is unset). |
| 29 |
*/ |
| 30 |
export function equals(schema, a, b, options) { |
| 31 |
if (a.$typeName != schema.typeName || b.$typeName != schema.typeName) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
if (a === b) { |
| 35 |
return true; |
| 36 |
} |
| 37 |
return reflectEquals(reflect(schema, a), reflect(schema, b), options); |
| 38 |
} |
| 39 |
function reflectEquals(a, b, opts) { |
| 40 |
if (a.desc.typeName === "google.protobuf.Any" && (opts === null || opts === void 0 ? void 0 : opts.unpackAny) == true) { |
| 41 |
return anyUnpackedEquals(a.message, b.message, opts); |
| 42 |
} |
| 43 |
for (const f of a.fields) { |
| 44 |
if (!fieldEquals(f, a, b, opts)) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
} |
| 48 |
if ((opts === null || opts === void 0 ? void 0 : opts.unknown) == true && !unknownEquals(a, b, opts.registry)) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
if ((opts === null || opts === void 0 ? void 0 : opts.extensions) == true && !extensionsEquals(a, b, opts)) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
return true; |
| 55 |
} |
| 56 |
// TODO(tstamm) add an option to consider NaN equal to NaN? |
| 57 |
function fieldEquals(f, a, b, opts) { |
| 58 |
if (!a.isSet(f) && !b.isSet(f)) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
if (!a.isSet(f) || !b.isSet(f)) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
switch (f.fieldKind) { |
| 65 |
case "scalar": |
| 66 |
return scalarEquals(f.scalar, a.get(f), b.get(f)); |
| 67 |
case "enum": |
| 68 |
return a.get(f) === b.get(f); |
| 69 |
case "message": |
| 70 |
return reflectEquals(a.get(f), b.get(f), opts); |
| 71 |
case "map": { |
| 72 |
// TODO(tstamm) can't we compare sizes first? |
| 73 |
const mapA = a.get(f); |
| 74 |
const mapB = b.get(f); |
| 75 |
const keys = []; |
| 76 |
for (const k of mapA.keys()) { |
| 77 |
if (!mapB.has(k)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
keys.push(k); |
| 81 |
} |
| 82 |
for (const k of mapB.keys()) { |
| 83 |
if (!mapA.has(k)) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
for (const key of keys) { |
| 88 |
const va = mapA.get(key); |
| 89 |
const vb = mapB.get(key); |
| 90 |
if (va === vb) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
switch (f.mapKind) { |
| 94 |
case "enum": |
| 95 |
return false; |
| 96 |
case "message": |
| 97 |
if (!reflectEquals(va, vb, opts)) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
break; |
| 101 |
case "scalar": |
| 102 |
if (!scalarEquals(f.scalar, va, vb)) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
break; |
| 109 |
} |
| 110 |
case "list": { |
| 111 |
const listA = a.get(f); |
| 112 |
const listB = b.get(f); |
| 113 |
if (listA.size != listB.size) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
for (let i = 0; i < listA.size; i++) { |
| 117 |
const va = listA.get(i); |
| 118 |
const vb = listB.get(i); |
| 119 |
if (va === vb) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
switch (f.listKind) { |
| 123 |
case "enum": |
| 124 |
return false; |
| 125 |
case "message": |
| 126 |
if (!reflectEquals(va, vb, opts)) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
break; |
| 130 |
case "scalar": |
| 131 |
if (!scalarEquals(f.scalar, va, vb)) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
return true; |
| 141 |
} |
| 142 |
function anyUnpackedEquals(a, b, opts) { |
| 143 |
if (a.typeUrl !== b.typeUrl) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
const unpackedA = anyUnpack(a, opts.registry); |
| 147 |
const unpackedB = anyUnpack(b, opts.registry); |
| 148 |
if (unpackedA && unpackedB) { |
| 149 |
const schema = opts.registry.getMessage(unpackedA.$typeName); |
| 150 |
if (schema) { |
| 151 |
return equals(schema, unpackedA, unpackedB, opts); |
| 152 |
} |
| 153 |
} |
| 154 |
return scalarEquals(ScalarType.BYTES, a.value, b.value); |
| 155 |
} |
| 156 |
function unknownEquals(a, b, registry) { |
| 157 |
function getTrulyUnknown(msg, registry) { |
| 158 |
var _a; |
| 159 |
const u = (_a = msg.getUnknown()) !== null && _a !== void 0 ? _a : []; |
| 160 |
return registry |
| 161 |
? u.filter((uf) => !registry.getExtensionFor(msg.desc, uf.no)) |
| 162 |
: u; |
| 163 |
} |
| 164 |
const unknownA = getTrulyUnknown(a, registry); |
| 165 |
const unknownB = getTrulyUnknown(b, registry); |
| 166 |
if (unknownA.length != unknownB.length) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
for (let i = 0; i < unknownA.length; i++) { |
| 170 |
const a = unknownA[i]; |
| 171 |
const b = unknownB[i]; |
| 172 |
if (a.no != b.no) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
if (a.wireType != b.wireType) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
if (!scalarEquals(ScalarType.BYTES, a.data, b.data)) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
} |
| 182 |
return true; |
| 183 |
} |
| 184 |
function extensionsEquals(a, b, opts) { |
| 185 |
function getSetExtensions(msg, registry) { |
| 186 |
var _a; |
| 187 |
return ((_a = msg.getUnknown()) !== null && _a !== void 0 ? _a : []) |
| 188 |
.map((uf) => registry.getExtensionFor(msg.desc, uf.no)) |
| 189 |
.filter((e) => e != undefined) |
| 190 |
.filter((e, index, arr) => arr.indexOf(e) === index); |
| 191 |
} |
| 192 |
const extensionsA = getSetExtensions(a, opts.registry); |
| 193 |
const extensionsB = getSetExtensions(b, opts.registry); |
| 194 |
if (extensionsA.length != extensionsB.length || |
| 195 |
extensionsA.some((e) => !extensionsB.includes(e))) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
for (const extension of extensionsA) { |
| 199 |
const [containerA, field] = createExtensionContainer(extension, getExtension(a.message, extension)); |
| 200 |
const [containerB] = createExtensionContainer(extension, getExtension(b.message, extension)); |
| 201 |
if (!fieldEquals(field, containerA, containerB, opts)) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
} |
| 205 |
return true; |
| 206 |
} |