-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace instanceof Message usages (#729)
This replaces the usage of `instanceof Message` (and `Message` subclasses) with the new `isMessage` function. In addition, it adds some tests to verify behavior. `isMessage` can be used to determine whether a given object is any subtype of `Message` or is a specific `Message` by passing the type. It is recommended to use this instead of `instanceof Message`. **Example usage** ```typescript import { isMessage } from "@bufbuild/protobuf"; const user = new User({ firstName: "Homer", }); isMessage(user); // true isMessage(user, User); // true isMessage(user, OtherMessageType); // false ``` View the docs [here](./docs/runtime-api.md#identifying-messages). **Perf benchmarks:** ``` // isMessage large google.protobuf.FileDescriptorSet (1061190 bytes) x 60.52 ops/sec ±1.13% (63 runs sampled) tiny docs.User (4 bytes) x 3,386,238 ops/sec ±0.19% (100 runs sampled) scalar values (102 bytes) x 630,178 ops/sec ±0.19% (96 runs sampled) repeated scalar fields (195 bytes) x 401,749 ops/sec ±0.19% (98 runs sampled) map with scalar keys and values (186 bytes) x 225,122 ops/sec ±0.21% (100 runs sampled) repeated field with 1000 messages (2000 bytes) x 7,154 ops/sec ±0.42% (96 runs sampled) map field with 1000 messages (8890 bytes) x 3,594 ops/sec ±0.21% (98 runs sampled) ``` ``` // main large google.protobuf.FileDescriptorSet (1061190 bytes) x 60.74 ops/sec ±1.27% (64 runs sampled) tiny docs.User (4 bytes) x 3,416,634 ops/sec ±0.24% (97 runs sampled) scalar values (102 bytes) x 627,019 ops/sec ±0.23% (99 runs sampled) repeated scalar fields (195 bytes) x 397,073 ops/sec ±1.53% (97 runs sampled) map with scalar keys and values (186 bytes) x 224,147 ops/sec ±0.42% (98 runs sampled) repeated field with 1000 messages (2000 bytes) x 7,162 ops/sec ±0.39% (95 runs sampled) map field with 1000 messages (8890 bytes) x 3,307 ops/sec ±0.20% (97 runs sampled) ``` --------- Co-authored-by: Timo Stamm <[email protected]>
- Loading branch information
Showing
14 changed files
with
137 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 { describe, expect, test } from "@jest/globals"; | ||
import { User as TS_User } from "./gen/ts/extra/example_pb.js"; | ||
import { User as JS_User } from "./gen/js/extra/example_pb.js"; | ||
import { isMessage } from "@bufbuild/protobuf"; | ||
|
||
describe("isMessage", () => { | ||
test("subclass of Message", () => { | ||
const user = new TS_User({ | ||
firstName: "Homer", | ||
lastName: "Simpson", | ||
}); | ||
|
||
expect(isMessage(user)).toBeTruthy(); | ||
expect(isMessage(user, TS_User)).toBeTruthy(); | ||
}); | ||
test("returns false if expected Message property is not a function", () => { | ||
const user = new TS_User({ | ||
firstName: "Homer", | ||
lastName: "Simpson", | ||
}); | ||
// @ts-expect-error: Setting to a boolean to force a failure | ||
user.toJson = false; | ||
|
||
expect(isMessage(user, TS_User)).toBeFalsy(); | ||
}); | ||
test("null returns false", () => { | ||
expect(isMessage(null)).toBeFalsy(); | ||
expect(isMessage(null, TS_User)).toBeFalsy(); | ||
}); | ||
test("non-object returns false", () => { | ||
expect(isMessage("test")).toBeFalsy(); | ||
expect(isMessage("test", TS_User)).toBeFalsy(); | ||
}); | ||
test("mixed instances", () => { | ||
const user = new TS_User({ | ||
firstName: "Homer", | ||
lastName: "Simpson", | ||
}); | ||
|
||
expect(isMessage(user, JS_User)).toBeTruthy(); | ||
}); | ||
test("type guard works as expected", () => { | ||
const user: unknown = new TS_User(); | ||
if (isMessage(user)) { | ||
expect(user.toJsonString()).toBeDefined(); | ||
} | ||
if (isMessage(user, TS_User)) { | ||
expect(user.firstName).toBeDefined(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.