Skip to content

Commit 54cbb4f

Browse files
committed
fix(orap): incorrect bigint parse
parse bigint or other types when save to cache
1 parent a2e45f4 commit 54cbb4f

File tree

6 files changed

+130
-4
lines changed

6 files changed

+130
-4
lines changed

packages/orap/src/task/verse.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Milliseconds } from '@ora-io/utils'
2-
import { composeFns, isJsonString, isString, stripPrefix } from '@ora-io/utils'
2+
import { argParser, composeFns, isJsonString, isString, stripPrefix } from '@ora-io/utils'
33
import type { TaskFlow } from '../flow'
44
import { HandleSuccessMiddleware } from '../middlewares/HandleSuccessMiddleware'
55
import { HandleFailedMiddleware } from '../middlewares/private'
@@ -97,12 +97,13 @@ export class TaskRaplized extends TaskStorable {
9797
* @returns
9898
*/
9999
toString() {
100-
return this.stringify(this.eventLog)
100+
const res = argParser.parse(this.eventLog)
101+
return this.stringify(res)
101102
}
102103

103104
fromString(jsonString: string) {
104105
if (isJsonString(jsonString))
105-
this.eventLog = JSON.parse(jsonString)
106+
this.eventLog = argParser.serialize(JSON.parse(jsonString))
106107

107108
return this
108109
}

packages/utils/src/args/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './parser'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { ArgParser } from './parser'
3+
4+
describe('ArgParser', () => {
5+
it('should parse and serialize basic types', () => {
6+
const parser = new ArgParser()
7+
const args = [42, 'hello', true, BigInt(12345678901234567890n)]
8+
const parsedArgs = parser.parse(args)
9+
const serializedArgs = parser.serialize(parsedArgs)
10+
expect(serializedArgs).toEqual(args)
11+
})
12+
13+
it('should parse and serialize nested structures', () => {
14+
const parser = new ArgParser()
15+
const args = [42, 'hello', true, { a: 1, b: 'world', c: [1, 2, { d: 'nested' }] }]
16+
const parsedArgs = parser.parse(args)
17+
const serializedArgs = parser.serialize(parsedArgs)
18+
expect(serializedArgs).toEqual(args)
19+
})
20+
21+
it('should handle empty arrays and objects', () => {
22+
const parser = new ArgParser()
23+
const args = [[], {}]
24+
const parsedArgs = parser.parse(args)
25+
const serializedArgs = parser.serialize(parsedArgs)
26+
expect(serializedArgs).toEqual(args)
27+
})
28+
29+
it('should handle complex nested structures with bigint', () => {
30+
const parser = new ArgParser()
31+
const args = [
32+
42,
33+
'hello',
34+
true,
35+
{
36+
a: 1,
37+
b: 'world',
38+
c: [1, 2, { d: 'nested', e: BigInt(9876543210987654321n) }],
39+
},
40+
BigInt(12345678901234567890n),
41+
]
42+
const parsedArgs = parser.parse(args)
43+
const serializedArgs = parser.serialize(parsedArgs)
44+
expect(serializedArgs).toEqual(args)
45+
})
46+
})

packages/utils/src/args/parser.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-disable no-prototype-builtins */
2+
/* eslint-disable no-case-declarations */
3+
export interface ParsedArg {
4+
value: any
5+
type: string
6+
}
7+
8+
export class ArgParser {
9+
parse(args: any[]): ParsedArg[] {
10+
return args.map(arg => this._parse(arg))
11+
}
12+
13+
private _parse(arg: any): ParsedArg {
14+
const type = typeof arg
15+
if (Array.isArray(arg)) {
16+
return {
17+
value: arg.map(item => this._parse(item)),
18+
type: 'array',
19+
}
20+
}
21+
else if (type === 'object' && arg !== null) {
22+
const parsedObject: { [key: string]: ParsedArg } = {}
23+
for (const key in arg) {
24+
if (arg.hasOwnProperty(key))
25+
parsedObject[key] = this._parse(arg[key])
26+
}
27+
return {
28+
value: parsedObject,
29+
type: 'object',
30+
}
31+
}
32+
else if (type === 'bigint') {
33+
return {
34+
value: arg.toString(),
35+
type: 'bigint',
36+
}
37+
}
38+
else {
39+
return {
40+
value: arg,
41+
type,
42+
}
43+
}
44+
}
45+
46+
serialize(parsedArgs: ParsedArg[]): any[] {
47+
return parsedArgs.map(parsedArg => this._serialize(parsedArg))
48+
}
49+
50+
private _serialize(parsedArg: ParsedArg): any {
51+
switch (parsedArg.type) {
52+
case 'string':
53+
return String(parsedArg.value)
54+
case 'number':
55+
return Number(parsedArg.value)
56+
case 'boolean':
57+
return Boolean(parsedArg.value)
58+
case 'bigint':
59+
return BigInt(parsedArg.value)
60+
case 'array':
61+
return (parsedArg.value as ParsedArg[]).map(item => this._serialize(item))
62+
case 'object':
63+
const serializedObject: { [key: string]: any } = {}
64+
const value = parsedArg.value as { [key: string]: ParsedArg }
65+
for (const key in value) {
66+
if (value.hasOwnProperty(key))
67+
serializedObject[key] = this._serialize(value[key])
68+
}
69+
return serializedObject
70+
default:
71+
return parsedArg.value
72+
}
73+
}
74+
}
75+
76+
export const argParser = new ArgParser()

packages/utils/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export * from './common'
22
export * from './store'
33
export * from './w3'
4+
export * from './args'
5+
46
export type * from './types'
57
export * from '@murongg/utils'

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es2018",
3+
"target": "ESNext",
44
"module": "esnext",
55
"lib": ["esnext"],
66
"moduleResolution": "node",

0 commit comments

Comments
 (0)