From 4ee19598d50466c24b800e053014967d03796701 Mon Sep 17 00:00:00 2001 From: "Y.V" <5196666qwe@gmail.com> Date: Mon, 1 Jul 2019 10:00:17 +0800 Subject: [PATCH] feat: @config(opt) decorator opt accept dot natation --- packages/midway-web/src/loader/webLoader.ts | 4 +- packages/midway-web/src/utils.ts | 28 +++++++++++ packages/midway-web/test/enhance.test.ts | 14 ++++++ .../src/app/controller/config.ts | 50 +++++++++++++++++++ .../src/config/config.default.ts | 3 ++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 packages/midway-web/test/fixtures/enhance/base-app-decorator/src/app/controller/config.ts diff --git a/packages/midway-web/src/loader/webLoader.ts b/packages/midway-web/src/loader/webLoader.ts index 1d7f015dec3e..5ce08e47b412 100644 --- a/packages/midway-web/src/loader/webLoader.ts +++ b/packages/midway-web/src/loader/webLoader.ts @@ -15,7 +15,7 @@ import { getClassMetadata, getMethodDataFromClass, getProviderId, listModule } f import { ContainerLoader, MidwayHandlerKey } from 'midway-core'; import * as path from 'path'; import { MidwayLoaderOptions, WebMiddleware } from '../interface'; -import { isTypeScriptEnvironment } from '../utils'; +import { isTypeScriptEnvironment, safelyGet } from '../utils'; import { EggAppInfo } from 'egg'; const debug = require('debug')(`midway:loader:${process.pid}`); @@ -173,7 +173,7 @@ export class MidwayWebLoader extends EggLoader { // register handler for container this.containerLoader.registerAllHook(MidwayHandlerKey.CONFIG, (key) => { - return this.config[key]; + return safelyGet(key, this.config); }); this.containerLoader.registerAllHook(MidwayHandlerKey.PLUGIN, (key) => { diff --git a/packages/midway-web/src/utils.ts b/packages/midway-web/src/utils.ts index 05bdbb4f713c..cb09d80fdfc7 100644 --- a/packages/midway-web/src/utils.ts +++ b/packages/midway-web/src/utils.ts @@ -45,3 +45,31 @@ export function getMethodNames(obj) { export function isTypeScriptEnvironment() { return !!require.extensions['.ts']; } + +/** + * safelyGet(['a','b'],{a: {b: 2}}) // => 2 + * safelyGet(['a','b'],{c: {b: 2}}) // => undefined + * safelyGet('a.b',{a: {b: 2}}) // => 2 + * safelyGet('a.b',{c: {b: 2}}) // => undefined + */ +export function safelyGet(list, obj) { + if (arguments.length === 1) { return _obj => safelyGet(list, _obj); } + + if (obj === null || obj === undefined) { + return undefined; + } + let willReturn = obj; + let counter = 0; + + const pathArrValue = typeof list === 'string' ? list.split('.') : list; + + while (counter < pathArrValue.length) { + if (willReturn === null || willReturn === undefined) { + return undefined; + } + willReturn = willReturn[ pathArrValue[ counter ] ]; + counter++; + } + + return willReturn; +} diff --git a/packages/midway-web/test/enhance.test.ts b/packages/midway-web/test/enhance.test.ts index c36e1d2b9f63..34c56130b8a2 100644 --- a/packages/midway-web/test/enhance.test.ts +++ b/packages/midway-web/test/enhance.test.ts @@ -158,6 +158,20 @@ describe('/test/enhance.test.ts', () => { .expect('service,hello,a,b', done); }); + it('should config controller be ok', done => { + done = pedding(2, done); + + request(app.callback()) + .get('/config/test') + .expect(200) + .expect({ a: 1, b: true, c: 2}, done); + + request(app.callback()) + .get('/config/test2') + .expect(200) + .expect({ bucLogin: false, plugin2: true }, done); + }); + it('should param controller be ok ', async () => { // done = pedding(11, done); diff --git a/packages/midway-web/test/fixtures/enhance/base-app-decorator/src/app/controller/config.ts b/packages/midway-web/test/fixtures/enhance/base-app-decorator/src/app/controller/config.ts new file mode 100644 index 000000000000..7c070e841a69 --- /dev/null +++ b/packages/midway-web/test/fixtures/enhance/base-app-decorator/src/app/controller/config.ts @@ -0,0 +1,50 @@ +import {provide, inject} from 'injection'; +import { controller, get, config } from '../../../../../../../src'; + +@provide() +@controller('/config') +export class ConfigController { + + @inject('ctx') + ctx: any; + + // should be 1 + @config('hello.a') + a: number; + + // should be 2 + @config('hello.e.f') + c: number; + + // should be undefined + @config('hello.f') + d: number; + + @config('plugins') + plugins: any; + + b: boolean; + + constructor( + // should be true + @config('plugins.plugin2') pluginFlag: boolean + ) { + this.b = pluginFlag; + } + + @get('/test') + async test() { + const data = { + a: this.a, + b: this.b, + c: this.c, + d: this.d, + }; + this.ctx.body = data; + } + + @get('/test2') + async test2() { + this.ctx.body = this.plugins; + } +} diff --git a/packages/midway-web/test/fixtures/enhance/base-app-decorator/src/config/config.default.ts b/packages/midway-web/test/fixtures/enhance/base-app-decorator/src/config/config.default.ts index 182fd4cc1002..41fe6826d7fe 100644 --- a/packages/midway-web/test/fixtures/enhance/base-app-decorator/src/config/config.default.ts +++ b/packages/midway-web/test/fixtures/enhance/base-app-decorator/src/config/config.default.ts @@ -4,6 +4,9 @@ export const hello = { a: 1, b: 2, d: [1, 2, 3], + e: { + f: 2 + } }; export const plugins = {