Skip to content

Commit be8a0cc

Browse files
authored
Support MESSAGE_TYPE in MessageType.equals (#729)
1 parent 3f14440 commit be8a0cc

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

packages/runtime/spec/message-type.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,19 @@ describe('MessageType', () => {
9999
expect(msg).toEqual(exp);
100100
})
101101

102+
describe('equals()', () => {
103+
it('decides on wrong MESSAGE_TYPE', () => {
104+
const A: MessageType<any> = new MessageType<any>('.test.A', [
105+
{no: 1, name: 'string_field', kind: "scalar", T: ScalarType.STRING},
106+
]);
107+
const B: MessageType<any> = new MessageType<any>('.test.B', [
108+
{no: 1, name: 'string_field', kind: "scalar", T: ScalarType.STRING},
109+
]);
110+
const a = A.create();
111+
const b = B.create();
112+
expect(A.equals(a, b)).toBe(false);
113+
});
114+
});
115+
102116
});
103117

packages/runtime/src/message-type.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {reflectionEquals} from "./reflection-equals";
1818
import type {UnknownMessage} from "./unknown-types";
1919
import {binaryWriteOptions} from "./binary-writer";
2020
import {binaryReadOptions} from "./binary-reader";
21+
import { containsMessageType } from "./reflection-contains-message-type";
2122

2223
const baseDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf({})) as Record<typeof MESSAGE_TYPE, unknown>;
2324
const messageTypeDescriptor = baseDescriptors[MESSAGE_TYPE] = {} as {value?: unknown};
@@ -126,9 +127,26 @@ export class MessageType<T extends object> implements IMessageType<T> {
126127
* Determines whether two message of the same type have the same field values.
127128
* Checks for deep equality, traversing repeated fields, oneof groups, maps
128129
* and messages recursively.
130+
*
129131
* Will also return true if both messages are `undefined`.
132+
*
133+
* This method checks the MESSAGE_TYPE symbol property added by create()
134+
* since v2.0.3: It returns false if either of the two messages has the wrong
135+
* type.
130136
*/
131137
equals(a: T | undefined, b: T | undefined): boolean {
138+
if (a === b) {
139+
return true;
140+
}
141+
if (!a || !b) {
142+
return false;
143+
}
144+
if (containsMessageType(a) && a[MESSAGE_TYPE].typeName !== this.typeName) {
145+
return false;
146+
}
147+
if (containsMessageType(b) && b[MESSAGE_TYPE].typeName !== this.typeName) {
148+
return false;
149+
}
132150
return reflectionEquals(this, a as UnknownMessage | undefined, b as UnknownMessage | undefined);
133151
}
134152

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Duration} from "../gen/google/protobuf/duration";
2+
import {Timestamp} from "../gen/google/protobuf/timestamp";
3+
4+
5+
describe('MessageType', function () {
6+
it('equals()', () => {
7+
const dur = Duration.create();
8+
const tim = Timestamp.create();
9+
expect( Duration.equals(dur, tim)).toBe(false);
10+
});
11+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Duration} from "../gen/google/protobuf/duration";
2+
import {Timestamp} from "../gen/google/protobuf/timestamp";
3+
4+
// Copied from test-default/message-type.spec.ts. Do not edit.
5+
6+
describe('MessageType', function () {
7+
it('equals()', () => {
8+
const dur = Duration.create();
9+
const tim = Timestamp.create();
10+
expect( Duration.equals(dur, tim)).toBe(false);
11+
});
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Duration} from "../gen/google/protobuf/duration";
2+
import {Timestamp} from "../gen/google/protobuf/timestamp";
3+
4+
// Copied from test-default/message-type.spec.ts. Do not edit.
5+
6+
describe('MessageType', function () {
7+
it('equals()', () => {
8+
const dur = Duration.create();
9+
const tim = Timestamp.create();
10+
expect( Duration.equals(dur, tim)).toBe(false);
11+
});
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Duration} from "../gen/google/protobuf/duration";
2+
import {Timestamp} from "../gen/google/protobuf/timestamp";
3+
4+
// Copied from test-default/message-type.spec.ts. Do not edit.
5+
6+
describe('MessageType', function () {
7+
it('equals()', () => {
8+
const dur = Duration.create();
9+
const tim = Timestamp.create();
10+
expect( Duration.equals(dur, tim)).toBe(false);
11+
});
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Duration} from "../gen/google/protobuf/duration";
2+
import {Timestamp} from "../gen/google/protobuf/timestamp";
3+
4+
// Copied from test-default/message-type.spec.ts. Do not edit.
5+
6+
describe('MessageType', function () {
7+
it('equals()', () => {
8+
const dur = Duration.create();
9+
const tim = Timestamp.create();
10+
expect( Duration.equals(dur, tim)).toBe(false);
11+
});
12+
});

0 commit comments

Comments
 (0)