Skip to content

Commit b73c20a

Browse files
Fix logic that hydrates protobuf msgs and add test (#19162)
1 parent c5d296f commit b73c20a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

components/dashboard/src/data/setup.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ test("set and get proto message", async () => {
2222
expect(rehydrate({ foo: org }).foo.creationTime!.toDate()).toStrictEqual(now);
2323
});
2424

25+
test("set and get proto message that include | in the message value", async () => {
26+
const now = new Date();
27+
const org = new Organization({
28+
creationTime: Timestamp.fromDate(now),
29+
id: "test-id",
30+
name: "test-name|more",
31+
slug: "test-slug",
32+
});
33+
34+
expect(rehydrate(org).creationTime!.toDate()).toStrictEqual(now);
35+
expect(rehydrate([org])[0].creationTime!.toDate()).toStrictEqual(now);
36+
expect(rehydrate({ foo: org }).foo.creationTime!.toDate()).toStrictEqual(now);
37+
expect(rehydrate(org).name).toStrictEqual("test-name|more");
38+
});
39+
2540
function rehydrate<T>(obj: T): T {
2641
const dehydrated = dehydrate(obj);
2742
const str = JSON.stringify(dehydrated);

components/dashboard/src/data/setup.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,19 @@ export function dehydrate(message: any): any {
183183
return message;
184184
}
185185

186+
// This is used to hydrate protobuf messages from the cache
187+
// Serialized protobuf messages follow the format: |messageName|jsonstring
186188
export function hydrate(value: any): any {
187189
if (value instanceof Array) {
188190
return value.map(hydrate);
189191
}
190192
if (typeof value === "string" && value.startsWith("|") && value.lastIndexOf("|") > 1) {
191-
const separatorIdx = value.lastIndexOf("|");
192-
const messageName = value.substring(1, separatorIdx);
193-
const json = value.substring(separatorIdx + 1);
193+
// Remove the leading |
194+
const trimmedVal = value.substring(1);
195+
// Find the first | after the leading | to get the message name
196+
const separatorIdx = trimmedVal.indexOf("|");
197+
const messageName = trimmedVal.substring(0, separatorIdx);
198+
const json = trimmedVal.substring(separatorIdx + 1);
194199
const constructor = supportedMessages.get(messageName);
195200
if (!constructor) {
196201
console.error("unsupported message type", messageName);

0 commit comments

Comments
 (0)