Skip to content

Commit

Permalink
fix: support getter defaultValue (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored Oct 10, 2019
1 parent aeaff15 commit a0c55c3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 35 deletions.
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

0 comments on commit a0c55c3

Please sign in to comment.