|
|
@ -2,7 +2,7 @@ package config |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"regexp" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/go-kratos/kratos/v2/encoding" |
|
|
|
"github.com/go-kratos/kratos/v2/encoding" |
|
|
@ -81,7 +81,7 @@ func defaultDecoder(src *KeyValue, target map[string]interface{}) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// defaultResolver resolve placeholder in map value,
|
|
|
|
// defaultResolver resolve placeholder in map value,
|
|
|
|
// placeholder format in ${key:default} or $key.
|
|
|
|
// placeholder format in ${key:default}.
|
|
|
|
func defaultResolver(input map[string]interface{}) error { |
|
|
|
func defaultResolver(input map[string]interface{}) error { |
|
|
|
mapper := func(name string) string { |
|
|
|
mapper := func(name string) string { |
|
|
|
args := strings.SplitN(strings.TrimSpace(name), ":", 2) |
|
|
|
args := strings.SplitN(strings.TrimSpace(name), ":", 2) |
|
|
@ -99,7 +99,7 @@ func defaultResolver(input map[string]interface{}) error { |
|
|
|
for k, v := range sub { |
|
|
|
for k, v := range sub { |
|
|
|
switch vt := v.(type) { |
|
|
|
switch vt := v.(type) { |
|
|
|
case string: |
|
|
|
case string: |
|
|
|
sub[k] = os.Expand(vt, mapper) |
|
|
|
sub[k] = expand(vt, mapper) |
|
|
|
case map[string]interface{}: |
|
|
|
case map[string]interface{}: |
|
|
|
if err := resolve(vt); err != nil { |
|
|
|
if err := resolve(vt); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
@ -108,7 +108,7 @@ func defaultResolver(input map[string]interface{}) error { |
|
|
|
for i, iface := range vt { |
|
|
|
for i, iface := range vt { |
|
|
|
switch it := iface.(type) { |
|
|
|
switch it := iface.(type) { |
|
|
|
case string: |
|
|
|
case string: |
|
|
|
vt[i] = os.Expand(it, mapper) |
|
|
|
vt[i] = expand(it, mapper) |
|
|
|
case map[string]interface{}: |
|
|
|
case map[string]interface{}: |
|
|
|
if err := resolve(it); err != nil { |
|
|
|
if err := resolve(it); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
@ -122,3 +122,17 @@ func defaultResolver(input map[string]interface{}) error { |
|
|
|
} |
|
|
|
} |
|
|
|
return resolve(input) |
|
|
|
return resolve(input) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func expand(s string, mapping func(string) string) string { |
|
|
|
|
|
|
|
r, err := regexp.Compile(`\${(.*?)}`) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return s |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
re := r.FindAllStringSubmatch(s, -1) |
|
|
|
|
|
|
|
for _, i := range re { |
|
|
|
|
|
|
|
if len(i) == 2 { |
|
|
|
|
|
|
|
s = strings.ReplaceAll(s, i[0], mapping(i[1])) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return s |
|
|
|
|
|
|
|
} |
|
|
|