File tree 2 files changed +23
-3
lines changed
components/dashboard/src/data
2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,21 @@ test("set and get proto message", async () => {
22
22
expect ( rehydrate ( { foo : org } ) . foo . creationTime ! . toDate ( ) ) . toStrictEqual ( now ) ;
23
23
} ) ;
24
24
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
+
25
40
function rehydrate < T > ( obj : T ) : T {
26
41
const dehydrated = dehydrate ( obj ) ;
27
42
const str = JSON . stringify ( dehydrated ) ;
Original file line number Diff line number Diff line change @@ -183,14 +183,19 @@ export function dehydrate(message: any): any {
183
183
return message ;
184
184
}
185
185
186
+ // This is used to hydrate protobuf messages from the cache
187
+ // Serialized protobuf messages follow the format: |messageName|jsonstring
186
188
export function hydrate ( value : any ) : any {
187
189
if ( value instanceof Array ) {
188
190
return value . map ( hydrate ) ;
189
191
}
190
192
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 ) ;
194
199
const constructor = supportedMessages . get ( messageName ) ;
195
200
if ( ! constructor ) {
196
201
console . error ( "unsupported message type" , messageName ) ;
You can’t perform that action at this time.
0 commit comments