Skip to content

Fix logic that hydrates protobuf msgs and add test #19162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions components/dashboard/src/data/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ test("set and get proto message", async () => {
expect(rehydrate({ foo: org }).foo.creationTime!.toDate()).toStrictEqual(now);
});

test("set and get proto message that include | in the message value", async () => {
const now = new Date();
const org = new Organization({
creationTime: Timestamp.fromDate(now),
id: "test-id",
name: "test-name|more",
slug: "test-slug",
});

expect(rehydrate(org).creationTime!.toDate()).toStrictEqual(now);
expect(rehydrate([org])[0].creationTime!.toDate()).toStrictEqual(now);
expect(rehydrate({ foo: org }).foo.creationTime!.toDate()).toStrictEqual(now);
expect(rehydrate(org).name).toStrictEqual("test-name|more");
});

function rehydrate<T>(obj: T): T {
const dehydrated = dehydrate(obj);
const str = JSON.stringify(dehydrated);
Expand Down
11 changes: 8 additions & 3 deletions components/dashboard/src/data/setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,19 @@ export function dehydrate(message: any): any {
return message;
}

// This is used to hydrate protobuf messages from the cache
// Serialized protobuf messages follow the format: |messageName|jsonstring
export function hydrate(value: any): any {
if (value instanceof Array) {
return value.map(hydrate);
}
if (typeof value === "string" && value.startsWith("|") && value.lastIndexOf("|") > 1) {
const separatorIdx = value.lastIndexOf("|");
const messageName = value.substring(1, separatorIdx);
const json = value.substring(separatorIdx + 1);
// Remove the leading |
const trimmedVal = value.substring(1);
// Find the first | after the leading | to get the message name
const separatorIdx = trimmedVal.indexOf("|");
const messageName = trimmedVal.substring(0, separatorIdx);
const json = trimmedVal.substring(separatorIdx + 1);
const constructor = supportedMessages.get(messageName);
if (!constructor) {
console.error("unsupported message type", messageName);
Expand Down