@@ -26,15 +26,6 @@ import { ObjectConfiguration } from '../../base/Configuration';
26
26
import { Autowire } from './Autowire' ;
27
27
import { NotFoundError } from '../../utils/errorFactory' ;
28
28
29
-
30
- // 基础模版,用于 {{xxx.xx}} 这种形式的属性注入
31
- function tpl ( s : string , props : any ) : string {
32
- return _ . template ( s , {
33
- // use `{{` and `}}` as delimiters
34
- interpolate : / { { ( [ \s \S ] + ?) } } / g
35
- } ) ( props ) ;
36
- }
37
-
38
29
/**
39
30
* 所有解析器基类
40
31
*/
@@ -49,11 +40,11 @@ class BaseManagedResolver implements IManagedResolver {
49
40
throw new Error ( 'not implement' ) ;
50
41
}
51
42
52
- resolve ( managed : IManagedInstance , props : any ) : any {
43
+ resolve ( managed : IManagedInstance ) : any {
53
44
throw new Error ( 'not implement' ) ;
54
45
}
55
46
56
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
47
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
57
48
throw new Error ( 'not implement' ) ;
58
49
}
59
50
}
@@ -66,13 +57,13 @@ class JSONResolver extends BaseManagedResolver {
66
57
return KEYS . JSON_ELEMENT ;
67
58
}
68
59
69
- resolve ( managed : IManagedInstance , props : any ) : any {
60
+ resolve ( managed : IManagedInstance ) : any {
70
61
const mjson = < ManagedJSON > managed ;
71
- return JSON . parse ( tpl ( mjson . value , props ) ) ;
62
+ return JSON . parse ( this . _factory . tpl ( mjson . value ) ) ;
72
63
}
73
64
74
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
75
- return this . resolve ( managed , props ) ;
65
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
66
+ return this . resolve ( managed ) ;
76
67
}
77
68
}
78
69
@@ -89,40 +80,40 @@ class ValueResolver extends BaseManagedResolver {
89
80
* @param managed 类型接口
90
81
* @param props 注入的属性值
91
82
*/
92
- _resolveCommon ( managed : IManagedInstance , props : any ) : any {
83
+ _resolveCommon ( managed : IManagedInstance ) : any {
93
84
const mv = < ManagedValue > managed ;
94
85
switch ( mv . valueType ) {
95
86
case VALUE_TYPE . STRING :
96
87
case VALUE_TYPE . TEMPLATE :
97
- return tpl ( mv . value , props ) ;
88
+ return this . _factory . tpl ( mv . value ) ;
98
89
case VALUE_TYPE . NUMBER :
99
- return Number ( tpl ( mv . value , props ) ) ;
90
+ return Number ( this . _factory . tpl ( mv . value ) ) ;
100
91
case VALUE_TYPE . INTEGER :
101
- return parseInt ( tpl ( mv . value , props ) , 10 ) ;
92
+ return parseInt ( this . _factory . tpl ( mv . value ) , 10 ) ;
102
93
case VALUE_TYPE . DATE :
103
- return new Date ( tpl ( mv . value , props ) ) ;
94
+ return new Date ( this . _factory . tpl ( mv . value ) ) ;
104
95
case VALUE_TYPE . BOOLEAN :
105
96
return mv . value === 'true' ;
106
97
}
107
98
108
99
return mv . value ;
109
100
}
110
101
111
- resolve ( managed : IManagedInstance , props : any ) : any {
102
+ resolve ( managed : IManagedInstance ) : any {
112
103
const mv = < ManagedValue > managed ;
113
104
if ( mv . valueType === VALUE_TYPE . MANAGED ) {
114
105
return this . _factory . resolveManaged ( mv . value ) ;
115
106
} else {
116
- return this . _resolveCommon ( managed , props ) ;
107
+ return this . _resolveCommon ( managed ) ;
117
108
}
118
109
}
119
110
120
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
111
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
121
112
const mv = < ManagedValue > managed ;
122
113
if ( mv . valueType === VALUE_TYPE . MANAGED ) {
123
114
return await this . _factory . resolveManagedAsync ( mv . value ) ;
124
115
} else {
125
- return this . _resolveCommon ( managed , props ) ;
116
+ return this . _resolveCommon ( managed ) ;
126
117
}
127
118
}
128
119
}
@@ -135,14 +126,14 @@ class RefResolver extends BaseManagedResolver {
135
126
return KEYS . REF_ELEMENT ;
136
127
}
137
128
138
- resolve ( managed : IManagedInstance , props : any ) : any {
129
+ resolve ( managed : IManagedInstance ) : any {
139
130
const mr = < ManagedReference > managed ;
140
- return this . _factory . context . get ( mr . name , null ) ;
131
+ return this . _factory . context . get ( mr . name ) ;
141
132
}
142
133
143
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
134
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
144
135
const mr = < ManagedReference > managed ;
145
- return await this . _factory . context . getAsync ( mr . name , null ) ;
136
+ return await this . _factory . context . getAsync ( mr . name ) ;
146
137
}
147
138
}
148
139
@@ -154,7 +145,7 @@ class ListResolver extends BaseManagedResolver {
154
145
return KEYS . LIST_ELEMENT ;
155
146
}
156
147
157
- resolve ( managed : IManagedInstance , props : any ) : any {
148
+ resolve ( managed : IManagedInstance ) : any {
158
149
const ml = < ManagedList > managed ;
159
150
const arr = [ ] ;
160
151
for ( let i = 0 ; i < ml . length ; i ++ ) {
@@ -163,7 +154,7 @@ class ListResolver extends BaseManagedResolver {
163
154
return arr ;
164
155
}
165
156
166
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
157
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
167
158
const ml = < ManagedList > managed ;
168
159
const arr = [ ] ;
169
160
for ( let i = 0 ; i < ml . length ; i ++ ) {
@@ -181,7 +172,7 @@ class SetResolver extends BaseManagedResolver {
181
172
return KEYS . SET_ELEMENT ;
182
173
}
183
174
184
- resolve ( managed : IManagedInstance , props : any ) : any {
175
+ resolve ( managed : IManagedInstance ) : any {
185
176
const ms = < ManagedSet > managed ;
186
177
const s = new Set ( ) ;
187
178
for ( let item of ms ) {
@@ -190,7 +181,7 @@ class SetResolver extends BaseManagedResolver {
190
181
return s ;
191
182
}
192
183
193
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
184
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
194
185
const ms = < ManagedSet > managed ;
195
186
const s = new Set ( ) ;
196
187
for ( let item of ms ) {
@@ -208,7 +199,7 @@ class MapResolver extends BaseManagedResolver {
208
199
return KEYS . MAP_ELEMENT ;
209
200
}
210
201
211
- resolve ( managed : IManagedInstance , props : any ) : any {
202
+ resolve ( managed : IManagedInstance ) : any {
212
203
const mm = < ManagedMap > managed ;
213
204
const m = new Map ( ) ;
214
205
for ( let key of mm . keys ( ) ) {
@@ -217,7 +208,7 @@ class MapResolver extends BaseManagedResolver {
217
208
return m ;
218
209
}
219
210
220
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
211
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
221
212
const mm = < ManagedMap > managed ;
222
213
const m = new Map ( ) ;
223
214
for ( let key of mm . keys ( ) ) {
@@ -235,7 +226,7 @@ class PropertiesResolver extends BaseManagedResolver {
235
226
return KEYS . PROPS_ELEMENT ;
236
227
}
237
228
238
- resolve ( managed : IManagedInstance , props : any ) : any {
229
+ resolve ( managed : IManagedInstance ) : any {
239
230
const m = < ManagedProperties > managed ;
240
231
const cfg = new ObjectConfiguration ( ) ;
241
232
const keys = m . keys ( ) ;
@@ -246,7 +237,7 @@ class PropertiesResolver extends BaseManagedResolver {
246
237
return cfg ;
247
238
}
248
239
249
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
240
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
250
241
const m = < ManagedProperties > managed ;
251
242
const cfg = new ObjectConfiguration ( ) ;
252
243
const keys = m . keys ( ) ;
@@ -266,12 +257,12 @@ class PropertyResolver extends BaseManagedResolver {
266
257
return KEYS . PROPERTY_ELEMENT ;
267
258
}
268
259
269
- resolve ( managed : IManagedInstance , props : any ) : any {
260
+ resolve ( managed : IManagedInstance ) : any {
270
261
const mp = < ManagedProperty > managed ;
271
262
return this . _factory . resolveManaged ( mp . value ) ;
272
263
}
273
264
274
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
265
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
275
266
const mp = < ManagedProperty > managed ;
276
267
return await this . _factory . resolveManagedAsync ( mp . value ) ;
277
268
}
@@ -285,12 +276,12 @@ class ObjectResolver extends BaseManagedResolver {
285
276
return KEYS . OBJECT_ELEMENT ;
286
277
}
287
278
288
- resolve ( managed : IManagedInstance , props : any ) : any {
279
+ resolve ( managed : IManagedInstance ) : any {
289
280
const mo = < ManagedObject > managed ;
290
281
return this . _factory . create ( mo . definition , null ) ;
291
282
}
292
283
293
- async resolveAsync ( managed : IManagedInstance , props : any ) : Promise < any > {
284
+ async resolveAsync ( managed : IManagedInstance ) : Promise < any > {
294
285
const mo = < ManagedObject > managed ;
295
286
return await this . _factory . createAsync ( mo . definition , null ) ;
296
287
}
@@ -328,6 +319,20 @@ export class ManagedResolverFactory {
328
319
}
329
320
return this . _props ;
330
321
}
322
+ /**
323
+ * 用于解析模版化的值
324
+ * example: {{aaa.bbb.ccc}}
325
+ * @param value 配置的模版值
326
+ */
327
+ tpl ( value ) {
328
+ if ( value && value . indexOf ( '{{' ) > - 1 ) {
329
+ return _ . template ( value , {
330
+ // use `{{` and `}}` as delimiters
331
+ interpolate : / { { ( [ \s \S ] + ?) } } / g
332
+ } ) ( this . props ) ;
333
+ }
334
+ return value ;
335
+ }
331
336
332
337
registerResolver ( resolver : IManagedResolver ) {
333
338
this . resolvers . set ( resolver . type , resolver ) ;
@@ -337,14 +342,14 @@ export class ManagedResolverFactory {
337
342
if ( ! this . resolvers . has ( managed . type ) ) {
338
343
throw new Error ( `${ managed . type } resolver is not exists!` ) ;
339
344
}
340
- return this . resolvers . get ( managed . type ) . resolve ( managed , this . props ) ;
345
+ return this . resolvers . get ( managed . type ) . resolve ( managed ) ;
341
346
}
342
347
343
348
async resolveManagedAsync ( managed : IManagedInstance ) : Promise < any > {
344
349
if ( ! this . resolvers . has ( managed . type ) ) {
345
350
throw new Error ( `${ managed . type } resolver is not exists!` ) ;
346
351
}
347
- return await this . resolvers . get ( managed . type ) . resolveAsync ( managed , this . props ) ;
352
+ return await this . resolvers . get ( managed . type ) . resolveAsync ( managed ) ;
348
353
}
349
354
350
355
/**
0 commit comments