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

feat: dump ignore support key path #5380

Merged
merged 2 commits into from
Jan 9, 2025
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
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
9 changes: 8 additions & 1 deletion lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,15 @@ class EggApplication extends EggCore {
ignoreList = [];
}

let ignoreKeyPaths;
try {
ignoreKeyPaths = this.config.dump.ignoreKeyPaths;
} catch (e) {
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
Loading