mirror of
https://github.com/valitydev/frontend-thrift-codegen.git
synced 2024-11-06 02:15:17 +00:00
Test config. Fix i64 out of range (#36)
This commit is contained in:
parent
760847927d
commit
1cf173f272
2
.github/workflows/pr.yaml
vendored
2
.github/workflows/pr.yaml
vendored
@ -12,3 +12,5 @@ jobs:
|
||||
- run: npm ci
|
||||
- name: Prettier check
|
||||
run: npm run prettier:check
|
||||
- name: Run tests
|
||||
run: npm run test
|
||||
|
76
__tests__/create-thrift-instance.test.ts
Normal file
76
__tests__/create-thrift-instance.test.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import Int64 from '@vality/thrift-ts/lib/int64';
|
||||
import { createThriftInstance } from '../tools/utils/create-thrift-instance';
|
||||
|
||||
describe('createThriftInstance', () => {
|
||||
// Codegen Rational mock. Copied from result of codegeneration.
|
||||
function Rational(args) {
|
||||
this.p = null;
|
||||
this.q = null;
|
||||
if (args) {
|
||||
if (args.p !== undefined && args.p !== null) {
|
||||
this.p = args.p;
|
||||
}
|
||||
if (args.q !== undefined && args.q !== null) {
|
||||
this.q = args.q;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const instanceContext = {
|
||||
base: {
|
||||
Rational,
|
||||
},
|
||||
};
|
||||
|
||||
const metadata = [
|
||||
{
|
||||
path: 'base.thrift',
|
||||
name: 'base',
|
||||
ast: {
|
||||
struct: {
|
||||
Rational: [
|
||||
{
|
||||
type: 'i64',
|
||||
name: 'p',
|
||||
},
|
||||
{
|
||||
type: 'i64',
|
||||
name: 'q',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
test('create out of range integer', () => {
|
||||
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
const value = {
|
||||
p: 1000,
|
||||
q: 1000000000000000000,
|
||||
};
|
||||
|
||||
expect(() =>
|
||||
createThriftInstance(metadata, instanceContext, 'base', 'Rational', value)
|
||||
).toThrowError('Number is out of range');
|
||||
expect(errorSpy).toHaveBeenCalled();
|
||||
|
||||
errorSpy.mockRestore();
|
||||
});
|
||||
|
||||
test('create in range integer', () => {
|
||||
const value = {
|
||||
p: 1000,
|
||||
q: 900719925474099,
|
||||
};
|
||||
|
||||
const result = createThriftInstance(metadata, instanceContext, 'base', 'Rational', value);
|
||||
|
||||
const expected = new Rational({
|
||||
p: new Int64(1000),
|
||||
q: new Int64(900719925474099),
|
||||
});
|
||||
expect(result).toStrictEqual(expected);
|
||||
});
|
||||
});
|
46
__tests__/thrift-instance-to-object.test.ts
Normal file
46
__tests__/thrift-instance-to-object.test.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import Int64 from '@vality/thrift-ts/lib/int64';
|
||||
import { thriftInstanceToObject } from '../tools/utils/thrift-instance-to-object';
|
||||
|
||||
describe('thriftInstanceToObject', () => {
|
||||
const metadata = [
|
||||
{
|
||||
path: 'base.thrift',
|
||||
name: 'base',
|
||||
ast: {
|
||||
struct: {
|
||||
Rational: [
|
||||
{
|
||||
type: 'i64',
|
||||
name: 'p',
|
||||
},
|
||||
{
|
||||
type: 'i64',
|
||||
name: 'q',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
test('convert large i64 to number', () => {
|
||||
const value = {
|
||||
p: new Int64(1000),
|
||||
q: new Int64(1000000000000000000),
|
||||
};
|
||||
|
||||
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
|
||||
const result = thriftInstanceToObject(metadata, 'base', 'Rational', value);
|
||||
|
||||
const expected = {
|
||||
p: 1000,
|
||||
q: 1000000000000000000,
|
||||
};
|
||||
|
||||
expect(warnSpy).toHaveBeenCalled();
|
||||
expect(result).toStrictEqual(expected);
|
||||
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
});
|
6
jest.config.js
Normal file
6
jest.config.js
Normal file
@ -0,0 +1,6 @@
|
||||
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
testMatch: ['**/__tests__/**/*.test.ts'],
|
||||
};
|
6521
package-lock.json
generated
6521
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -5,7 +5,8 @@
|
||||
"scripts": {
|
||||
"prettier:check": "prettier \"**\" --list-different --ignore-unknown",
|
||||
"prettier:write": "prettier \"**\" --write --ignore-unknown",
|
||||
"codegen": "bin/index.js"
|
||||
"codegen": "bin/index.js",
|
||||
"test": "jest"
|
||||
},
|
||||
"author": "Vality",
|
||||
"license": "Apache-2.0",
|
||||
@ -30,6 +31,9 @@
|
||||
"yargs": "17.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "2.5.1"
|
||||
"@types/jest": "29.5.0",
|
||||
"jest": "29.5.0",
|
||||
"prettier": "2.5.1",
|
||||
"ts-jest": "29.0.5"
|
||||
}
|
||||
}
|
||||
|
@ -10,14 +10,21 @@ import {
|
||||
} from './namespace-type';
|
||||
import { ThriftAstMetadata } from './types';
|
||||
|
||||
const toInt64 = (value: number): Int64 => {
|
||||
if (value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
|
||||
throw new Error('Number is out of range');
|
||||
}
|
||||
return new Int64(value);
|
||||
};
|
||||
|
||||
export function createThriftInstance<V>(
|
||||
metadata: ThriftAstMetadata[],
|
||||
instanceContext: any,
|
||||
namespaceName: string,
|
||||
indefiniteType: ValueType,
|
||||
value: V,
|
||||
value: any,
|
||||
include?: JsonAST['include']
|
||||
): V {
|
||||
): any {
|
||||
if (isThriftObject(value)) {
|
||||
return value;
|
||||
}
|
||||
@ -47,7 +54,7 @@ export function createThriftInstance<V>(
|
||||
} else if (isPrimitiveType(type)) {
|
||||
switch (type) {
|
||||
case 'i64':
|
||||
return new Int64(value as any) as any;
|
||||
return toInt64(value);
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
|
@ -8,6 +8,15 @@ import {
|
||||
} from './namespace-type';
|
||||
import { ThriftAstMetadata } from './types';
|
||||
|
||||
const toNumber = (value: Int64): number => {
|
||||
const noImprecise = value.toNumber();
|
||||
if (noImprecise === Infinity) {
|
||||
console.warn('Very large positive or negative i64 number. Result will be imprecise.');
|
||||
return value.toNumber(true);
|
||||
}
|
||||
return noImprecise;
|
||||
};
|
||||
|
||||
export function thriftInstanceToObject(
|
||||
metadata: ThriftAstMetadata[],
|
||||
namespaceName: string,
|
||||
@ -46,7 +55,7 @@ export function thriftInstanceToObject(
|
||||
} else if (isPrimitiveType(type)) {
|
||||
switch (type) {
|
||||
case 'i64':
|
||||
return (value as unknown as Int64).toNumber() as unknown;
|
||||
return toNumber(value);
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
|
7
tsconfig.json
Normal file
7
tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user