Skip to content

Commit

Permalink
fix(orap): incorrect bigint parse
Browse files Browse the repository at this point in the history
parse bigint or other types when save to cache
  • Loading branch information
murongg committed Dec 12, 2024
1 parent a2e45f4 commit 54cbb4f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packages/orap/src/task/verse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Milliseconds } from '@ora-io/utils'
import { composeFns, isJsonString, isString, stripPrefix } from '@ora-io/utils'
import { argParser, composeFns, isJsonString, isString, stripPrefix } from '@ora-io/utils'
import type { TaskFlow } from '../flow'
import { HandleSuccessMiddleware } from '../middlewares/HandleSuccessMiddleware'
import { HandleFailedMiddleware } from '../middlewares/private'
Expand Down Expand Up @@ -97,12 +97,13 @@ export class TaskRaplized extends TaskStorable {
* @returns
*/
toString() {
return this.stringify(this.eventLog)
const res = argParser.parse(this.eventLog)
return this.stringify(res)
}

fromString(jsonString: string) {
if (isJsonString(jsonString))
this.eventLog = JSON.parse(jsonString)
this.eventLog = argParser.serialize(JSON.parse(jsonString))

return this
}
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/args/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './parser'
46 changes: 46 additions & 0 deletions packages/utils/src/args/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it } from 'vitest'
import { ArgParser } from './parser'

describe('ArgParser', () => {
it('should parse and serialize basic types', () => {
const parser = new ArgParser()
const args = [42, 'hello', true, BigInt(12345678901234567890n)]
const parsedArgs = parser.parse(args)
const serializedArgs = parser.serialize(parsedArgs)
expect(serializedArgs).toEqual(args)
})

it('should parse and serialize nested structures', () => {
const parser = new ArgParser()
const args = [42, 'hello', true, { a: 1, b: 'world', c: [1, 2, { d: 'nested' }] }]
const parsedArgs = parser.parse(args)
const serializedArgs = parser.serialize(parsedArgs)
expect(serializedArgs).toEqual(args)
})

it('should handle empty arrays and objects', () => {
const parser = new ArgParser()
const args = [[], {}]
const parsedArgs = parser.parse(args)
const serializedArgs = parser.serialize(parsedArgs)
expect(serializedArgs).toEqual(args)
})

it('should handle complex nested structures with bigint', () => {
const parser = new ArgParser()
const args = [
42,
'hello',
true,
{
a: 1,
b: 'world',
c: [1, 2, { d: 'nested', e: BigInt(9876543210987654321n) }],
},
BigInt(12345678901234567890n),
]
const parsedArgs = parser.parse(args)
const serializedArgs = parser.serialize(parsedArgs)
expect(serializedArgs).toEqual(args)
})
})
76 changes: 76 additions & 0 deletions packages/utils/src/args/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-case-declarations */
export interface ParsedArg {
value: any
type: string
}

export class ArgParser {
parse(args: any[]): ParsedArg[] {
return args.map(arg => this._parse(arg))
}

private _parse(arg: any): ParsedArg {
const type = typeof arg
if (Array.isArray(arg)) {
return {
value: arg.map(item => this._parse(item)),
type: 'array',
}
}
else if (type === 'object' && arg !== null) {
const parsedObject: { [key: string]: ParsedArg } = {}
for (const key in arg) {
if (arg.hasOwnProperty(key))
parsedObject[key] = this._parse(arg[key])
}
return {
value: parsedObject,
type: 'object',
}
}
else if (type === 'bigint') {
return {
value: arg.toString(),
type: 'bigint',
}
}
else {
return {
value: arg,
type,
}
}
}

serialize(parsedArgs: ParsedArg[]): any[] {
return parsedArgs.map(parsedArg => this._serialize(parsedArg))
}

private _serialize(parsedArg: ParsedArg): any {
switch (parsedArg.type) {
case 'string':
return String(parsedArg.value)
case 'number':
return Number(parsedArg.value)
case 'boolean':
return Boolean(parsedArg.value)
case 'bigint':
return BigInt(parsedArg.value)
case 'array':
return (parsedArg.value as ParsedArg[]).map(item => this._serialize(item))
case 'object':
const serializedObject: { [key: string]: any } = {}
const value = parsedArg.value as { [key: string]: ParsedArg }
for (const key in value) {
if (value.hasOwnProperty(key))
serializedObject[key] = this._serialize(value[key])
}
return serializedObject
default:
return parsedArg.value
}
}
}

export const argParser = new ArgParser()
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './common'
export * from './store'
export * from './w3'
export * from './args'

export type * from './types'
export * from '@murongg/utils'
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2018",
"target": "ESNext",
"module": "esnext",
"lib": ["esnext"],
"moduleResolution": "node",
Expand Down

0 comments on commit 54cbb4f

Please sign in to comment.