Skip to content
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

fix: support getter defaultValue #21

Merged
merged 1 commit into from
Oct 10, 2019
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
4 changes: 1 addition & 3 deletions .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module.exports = {
write: true,
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
],
exclude: [],
devdep: [
'autod',
'egg-bin',
Expand Down
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ node_js:
- '8'
- '10'
- '12'
before_install:
- npm i npminstall -g
install:
- npm i npminstall && npminstall
- npminstall
script:
- npm run ci
after_script:
Expand Down
43 changes: 21 additions & 22 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ module.exports = (info, version, classMap, options = {}) => {
return compile(uniqueId, info, classMap, version, options);
};

function compileProp(gen, info, key, classInfo, version, options) {
const attr = Object.create(null, Object.getOwnPropertyDescriptors(classInfo[key]));
if (has(attr, 'typeAliasIndex') && Array.isArray(info.generic)) {
attr.type = info.generic[attr.typeAliasIndex].type;
if (info.generic[attr.typeAliasIndex].generic) {
attr.generic = info.generic[attr.typeAliasIndex].generic;
}
}
const uniqueId = utils.normalizeUniqId(attr, version);
const desc = Object.getOwnPropertyDescriptor(attr, 'defaultValue');
if (!desc) {
gen('compile(\'%s\', %j, classMap, version, %j)(obj[\'%s\'], encoder, appClassMap);', uniqueId, attr, options, key);
} else {
Object.defineProperty(attr, 'defaultValue', Object.assign({}, desc, { enumerable: false }));
const dv = desc.get ? `({ ${utils.getterStringify(desc.get)} }).defaultValue` : JSON.stringify(desc.value);
gen('compile(\'%s\', %j, classMap, version, %j)(obj[\'%s\'] ? obj[\'%s\'] : %s, encoder, appClassMap);', uniqueId, attr, options, key, key, dv);
}
}

function compile(uniqueId, info, classMap, version, options) {
let encodeFn = cache.get(uniqueId);
if (encodeFn) return encodeFn;
Expand All @@ -83,10 +102,6 @@ function compile(uniqueId, info, classMap, version, options) {
const classInfo = classMap && classMap[type];

const gen = codegen([ 'obj', 'encoder', 'appClassMap' ], 'encode');
// 默认值
if (info.defaultValue) {
gen('if (obj == null) { obj = %j; }', info.defaultValue);
}
if (info.isArray) {
gen('if (obj == null) { return encoder.writeNull(); }');
const arrayDepth = info.arrayDepth || 1;
Expand Down Expand Up @@ -170,15 +185,7 @@ function compile(uniqueId, info, classMap, version, options) {
gen('encoder.writeType(\'%s\');', type);
for (const key of keys) {
gen('encoder.writeString(\'%s\');', key);
const attr = Object.assign({}, classInfo[key]);
if (has(attr, 'typeAliasIndex') && Array.isArray(info.generic)) {
attr.type = info.generic[attr.typeAliasIndex].type;
if (info.generic[attr.typeAliasIndex].generic) {
attr.generic = info.generic[attr.typeAliasIndex].generic;
}
}
const uniqueId = utils.normalizeUniqId(attr, version);
gen('compile(\'%s\', %j, classMap, version, %j)(obj[\'%s\'], encoder, appClassMap);', uniqueId, attr, options, key);
compileProp(gen, info, key, classInfo, version, options);
}
gen('encoder.byteBuffer.put(0x7a);');
} else {
Expand All @@ -191,15 +198,7 @@ function compile(uniqueId, info, classMap, version, options) {
gen('encoder._writeObjectBegin(\'%s\'); }', type);

for (const key of keys) {
const attr = Object.assign({}, classInfo[key]);
if (has(attr, 'typeAliasIndex') && Array.isArray(info.generic)) {
attr.type = info.generic[attr.typeAliasIndex].type;
if (info.generic[attr.typeAliasIndex].generic) {
attr.generic = info.generic[attr.typeAliasIndex].generic;
}
}
const uniqueId = utils.normalizeUniqId(attr, version);
gen('compile(\'%s\', %j, classMap, version, %j)(obj[\'%s\'], encoder, appClassMap);', uniqueId, attr, options, key);
compileProp(gen, info, key, classInfo, version, options);
}
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ exports.normalizeType = type => {
}
return type;
};

const REG_GETTER = /^function\s+get/g;

exports.getterStringify = fn => {
return fn.toString().replace(REG_GETTER, 'get');
};
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@
"benchmark": "^2.1.4",
"contributors": "^0.5.1",
"egg-bin": "^4.13.0",
"egg-ci": "^1.12.0",
"enums": "^1.0.1",
"eslint": "^6.0.1",
"eslint-config-egg": "^7.4.1",
"egg-ci": "^1.13.0",
"enums": "^1.0.3",
"eslint": "^6.5.1",
"eslint-config-egg": "^7.5.1",
"js-to-java": "^2.6.1",
"long": "^4.0.0",
"mkdirp": "^0.5.1",
"mm": "^2.5.0",
"rimraf": "^2.6.3"
"mz-modules": "^2.1.0"
},
"engines": {
"node": ">= 8.0.0"
Expand Down
45 changes: 45 additions & 0 deletions test/edge_case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assert = require('assert');
const encode = require('../').encode;
const hessian = require('hessian.js-1');
const sleep = require('mz-modules/sleep');
const classMap = require('./fixtures/edge_class_map');

describe('test/edge_case.test.js', () => {
Expand Down Expand Up @@ -221,5 +222,49 @@ describe('test/edge_case.test.js', () => {
const res = hessian.decode(buf, version);
assert.deepStrictEqual(res, { name: 'PRERELEASING' });
});

it('should support getter defaultValue', async () => {
const classMap = {
'com.test.TestClass': {
bizTime: {
type: 'java.util.Date',
get defaultValue() {
return new Date();
},
},
a: {
type: 'com.test.TestClass2',
},
},
'com.test.TestClass2': {
bizTime: {
type: 'java.util.Date',
get defaultValue() {
return new Date();
},
},
},
};
const obj = {
$class: 'com.test.TestClass',
$: { a: {} },
};
const buf1 = encode(obj, version, classMap);
const res1 = hessian.decode(buf1, version);
assert(res1.bizTime instanceof Date);
assert(res1.a && res1.a.bizTime instanceof Date);
console.log(res1);

await sleep(1000);

const buf2 = encode(obj, version, classMap);
const res2 = hessian.decode(buf2, version);
assert(res2.bizTime instanceof Date);
assert(res2.a && res2.a.bizTime instanceof Date);
console.log(res2);

assert(res2.bizTime.getTime() - res1.bizTime.getTime() >= 1000);
assert(res2.a.bizTime.getTime() - res1.a.bizTime.getTime() >= 1000);
});
});
});
6 changes: 3 additions & 3 deletions test/hessian.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

const path = require('path');
const Long = require('long');
const assert = require('assert');
const java = require('js-to-java');
const hessian = require('hessian.js-1');
const encode = require('../').encode;
const classMap = require('./fixtures/class_map');
const mkdirp = require('mkdirp');
const path = require('path');
const mkdirp = require('mz-modules/mkdirp');
const compile = require('../lib/compile');
const rimraf = require('rimraf');
const rimraf = require('mz-modules/rimraf');
const fs = require('fs');
const mm = require('mm');

Expand Down