Skip to content

Commit

Permalink
feat: dump ignore support key path
Browse files Browse the repository at this point in the history
  • Loading branch information
gxkl committed Jan 9, 2025
1 parent 682950f commit 2e7afed
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 7 deletions.
31 changes: 25 additions & 6 deletions lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ module.exports = {
safeParseURL,
};

function convertObject(obj, ignore) {
if (!is.array(ignore)) ignore = [ ignore ];
function convertObject(obj, ignore, ignoreKeyPaths) {
if (!is.array(ignore)) {
ignore = [ ignore ];
}
if (!is.array(ignoreKeyPaths)) {
ignoreKeyPaths = ignoreKeyPaths ? [ ignoreKeyPaths ] : [];
}
_convertObject(obj, ignore, ignoreKeyPaths, '');
}

function _convertObject(obj, ignore, ignoreKeyPaths, keyPath) {
for (const key of Object.keys(obj)) {
obj[key] = convertValue(key, obj[key], ignore);
obj[key] = convertValue(key, obj[key], ignore, ignoreKeyPaths, keyPath ? `${keyPath}.${key}` : key);
}
return obj;
}

function convertValue(key, value, ignore) {
function convertValue(key, value, ignore, ignoreKeyPaths, keyPath) {
if (is.nullOrUndefined(value)) return value;

let hit = false;
let hitKeyPath = false;
for (const matchKey of ignore) {
if (is.string(matchKey) && matchKey === key) {
hit = true;
Expand All @@ -30,15 +40,24 @@ function convertValue(key, value, ignore) {
break;
}
}
if (!hit) {
for (const matchKeyPath of ignoreKeyPaths) {
if (is.string(matchKeyPath) && keyPath === matchKeyPath) {
hitKeyPath = true;
break;
}
}
if (!hit && !hitKeyPath) {
if (is.symbol(value) || is.regExp(value)) return value.toString();
if (is.primitive(value) || is.array(value)) return value;
}

// only convert recursively when it's a plain object,
// o = {}
if (Object.getPrototypeOf(value) === Object.prototype) {
return convertObject(value, ignore);
if (hitKeyPath) {
return '<Object>';
}
return _convertObject(value, ignore, ignoreKeyPaths, keyPath);
}

// support class
Expand Down
4 changes: 3 additions & 1 deletion lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,10 @@ class EggApplication extends EggCore {
ignoreList = [];
}

const ignoreKeyPaths = this.config.dump.ignoreKeyPaths;

const json = extend(true, {}, { config: this.config, plugins: this.loader.allPlugins, appInfo: this.loader.appInfo });
utils.convertObject(json, ignoreList);
utils.convertObject(json, ignoreList, ignoreKeyPaths ? Object.keys(ignoreKeyPaths) : []);
return {
config: json,
meta: this.loader.configMeta,
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/apps/dump-ignore-key-path/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
exports.withKeyPaths = {
inner: {
key1: {
nested: true,
},
},
key2: 'str',
};

exports.dump = {
ignoreKeyPaths: {
'config.withKeyPaths.key2': true,
},
};

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.dump = {
ignoreKeyPaths: {
'config.withKeyPaths.inner.key1': true,
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/dump-ignore-key-path/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.static = false;
3 changes: 3 additions & 0 deletions test/fixtures/apps/dump-ignore-key-path/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "dumpconfig"
}
60 changes: 60 additions & 0 deletions test/lib/core/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,66 @@ describe('test/lib/core/utils.test.js', () => {
assert(obj.anonymousClassWithPropName === '<Class anonymousClassWithPropName>');
assert(obj[''] === '<Class anonymous>');
});

it('should support keyPath', () => {
const obj = {
plainObj: 'Plain',
Id: 1,
recursiveObj: {
value1: 'string',
value2: 1,
innerObj: {
key1: true,
},
},
arr: [
{
v1: 'str',
},
],
};
utils.convertObject(obj, [], [ 'id', 'recursiveObj.value2', 'recursiveObj.innerObj', 'arr' ]);
assert.deepEqual(obj, {
plainObj: 'Plain',
Id: 1,
recursiveObj: {
value1: 'string',
value2: '<Number>',
innerObj: '<Object>',
},
arr: '<Array>',
});
});

it('should hit key and keyPath simultaneously work', () => {
const obj = {
recursiveObj: {
value1: 'string',
value2: 1,
innerObj: {
key1: true,
},
},
arr: [
{
v1: 'str',
},
],
};
utils.convertObject(
obj,
[ 'arr', 'value2', 'innerObj' ],
[ 'recursiveObj.value2', 'recursiveObj.innerObj', 'arr' ]
);
assert.deepEqual(obj, {
recursiveObj: {
value1: 'string',
value2: '<Number>',
innerObj: '<Object>',
},
arr: '<Array>',
});
});
});

describe('safeParseURL()', () => {
Expand Down
20 changes: 20 additions & 0 deletions test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,26 @@ describe('test/lib/egg.test.js', () => {
});
});

describe('dumpConfig() ignore key path', () => {
const baseDir = utils.getFilepath('apps/dump-ignore-key-path');
let app;
before(() => {
app = utils.app('apps/dump-ignore-key-path');
return app.ready();
});
after(() => app.close());

it('should ignore key path', () => {
const json = require(path.join(baseDir, 'run/application_config.json'));
assert.deepEqual(json.config.withKeyPaths, {
inner: {
key1: '<Object>',
},
key2: '<String len: 3>',
});
});
});

describe('custom config from env', () => {
let app;
let baseDir;
Expand Down

0 comments on commit 2e7afed

Please sign in to comment.