@@ -271,15 +271,42 @@ func bindStructField(p Properties, v reflect.Value, str string, opt bindOption)
271271 bindValue (p , v , key , def , opt )
272272}
273273
274+ // Interpolate 处理字符串插值
275+ func Interpolate (p Properties , str string ) string {
276+
277+ start := strings .Index (str , "${" )
278+ if start < 0 {
279+ return str
280+ }
281+
282+ end := strings .IndexByte (str [start + 2 :], '}' )
283+ if end < 0 {
284+ return str
285+ }
286+
287+ end = start + 2 + end
288+ key := str [start + 2 : end ]
289+ val , ok := p .GetDefaultProperty (key , nil )
290+ if ! ok {
291+ panic (fmt .Errorf ("property \" %s\" not config" , key ))
292+ }
293+
294+ return str [:start ] + fmt .Sprint (val ) + Interpolate (p , str [end + 1 :])
295+ }
296+
274297// resolveProperty 解析属性值,查看其是否具有引用关系
275298func resolveProperty (p Properties , _ string , value interface {}) interface {} {
276- str , ok := value .(string )
277299
278- // 不是字符串或者没有使用配置引用语法
279- if ! ok || ! strings . HasPrefix ( str , "${" ) {
300+ str , ok := value .( string )
301+ if ! ok {
280302 return value
281303 }
282304
305+ // 遍历字符串看看是否包含 ${} 结构并解析
306+ if ! strings .HasPrefix (str , "${" ) {
307+ return Interpolate (p , str )
308+ }
309+
283310 key , def := parsePropertyTag (str [2 : len (str )- 1 ])
284311 if val , _ := p .GetDefaultProperty (key , def ); val != nil {
285312 return resolveProperty (p , key , val )
0 commit comments