@@ -3,7 +3,8 @@ package xcfg
33import (
44 "errors"
55 "fmt"
6- xcast2 "github.com/coder2z/g-saber/xtransform/xcast"
6+ "github.com/coder2z/g-saber/xstring"
7+ "github.com/coder2z/g-saber/xtransform/xcast"
78 "io"
89 "io/ioutil"
910 "reflect"
@@ -94,13 +95,15 @@ func (c *Configuration) LoadFromDataSource(ds DataSource, unmarshaler Unmarshale
9495// Load ...
9596func (c * Configuration ) Load (content []byte , unmarshaler Unmarshaler ) error {
9697 configuration := make (map [string ]interface {})
97- if err := unmarshaler (content , & configuration ); err != nil {
98+ // 替换里面的环境变量 ....
99+ envContent := xstring .ExpandEnv (xcast .ToString (content ))
100+ if err := unmarshaler ([]byte (envContent ), & configuration ); err != nil {
98101 return err
99102 }
100103 return c .apply (configuration )
101104}
102105
103- // Load loads configuration from provided data source.
106+ // LoadFromReader loads configuration from provided data source.
104107func (c * Configuration ) LoadFromReader (reader io.Reader , unmarshaler Unmarshaler ) error {
105108 content , err := ioutil .ReadAll (reader )
106109 if err != nil {
@@ -189,7 +192,7 @@ func GetString(key string) string {
189192
190193// GetString returns the value associated with the key as a string.
191194func (c * Configuration ) GetString (key string ) string {
192- return xcast2 .ToString (c .Get (key ))
195+ return xcast .ToString (c .Get (key ))
193196}
194197
195198// GetBool returns the value associated with the key as a boolean with default defaultConfiguration.
@@ -199,7 +202,7 @@ func GetBool(key string) bool {
199202
200203// GetBool returns the value associated with the key as a boolean.
201204func (c * Configuration ) GetBool (key string ) bool {
202- return xcast2 .ToBool (c .Get (key ))
205+ return xcast .ToBool (c .Get (key ))
203206}
204207
205208// GetInt returns the value associated with the key as an integer with default defaultConfiguration.
@@ -209,7 +212,7 @@ func GetInt(key string) int {
209212
210213// GetInt returns the value associated with the key as an integer.
211214func (c * Configuration ) GetInt (key string ) int {
212- return xcast2 .ToInt (c .Get (key ))
215+ return xcast .ToInt (c .Get (key ))
213216}
214217
215218// GetInt64 returns the value associated with the key as an integer with default defaultConfiguration.
@@ -219,7 +222,7 @@ func GetInt64(key string) int64 {
219222
220223// GetInt64 returns the value associated with the key as an integer.
221224func (c * Configuration ) GetInt64 (key string ) int64 {
222- return xcast2 .ToInt64 (c .Get (key ))
225+ return xcast .ToInt64 (c .Get (key ))
223226}
224227
225228// GetFloat64 returns the value associated with the key as a float64 with default defaultConfiguration.
@@ -229,7 +232,7 @@ func GetFloat64(key string) float64 {
229232
230233// GetFloat64 returns the value associated with the key as a float64.
231234func (c * Configuration ) GetFloat64 (key string ) float64 {
232- return xcast2 .ToFloat64 (c .Get (key ))
235+ return xcast .ToFloat64 (c .Get (key ))
233236}
234237
235238// GetTime returns the value associated with the key as time with default defaultConfiguration.
@@ -239,7 +242,7 @@ func GetTime(key string) time.Time {
239242
240243// GetTime returns the value associated with the key as time.
241244func (c * Configuration ) GetTime (key string ) time.Time {
242- return xcast2 .ToTime (c .Get (key ))
245+ return xcast .ToTime (c .Get (key ))
243246}
244247
245248// GetDuration returns the value associated with the key as a duration with default defaultConfiguration.
@@ -249,7 +252,7 @@ func GetDuration(key string) time.Duration {
249252
250253// GetDuration returns the value associated with the key as a duration.
251254func (c * Configuration ) GetDuration (key string ) time.Duration {
252- return xcast2 .ToDuration (c .Get (key ))
255+ return xcast .ToDuration (c .Get (key ))
253256}
254257
255258// GetStringSlice returns the value associated with the key as a slice of strings with default defaultConfiguration.
@@ -259,7 +262,7 @@ func GetStringSlice(key string) []string {
259262
260263// GetStringSlice returns the value associated with the key as a slice of strings.
261264func (c * Configuration ) GetStringSlice (key string ) []string {
262- return xcast2 .ToStringSlice (c .Get (key ))
265+ return xcast .ToStringSlice (c .Get (key ))
263266}
264267
265268// GetSlice returns the value associated with the key as a slice of strings with default defaultConfiguration.
@@ -269,7 +272,7 @@ func GetSlice(key string) []interface{} {
269272
270273// GetSlice returns the value associated with the key as a slice of strings.
271274func (c * Configuration ) GetSlice (key string ) []interface {} {
272- return xcast2 .ToSlice (c .Get (key ))
275+ return xcast .ToSlice (c .Get (key ))
273276}
274277
275278// GetStringMap returns the value associated with the key as a map of interfaces with default defaultConfiguration.
@@ -279,7 +282,7 @@ func GetStringMap(key string) map[string]interface{} {
279282
280283// GetStringMap returns the value associated with the key as a map of interfaces.
281284func (c * Configuration ) GetStringMap (key string ) map [string ]interface {} {
282- return xcast2 .ToStringMap (c .Get (key ))
285+ return xcast .ToStringMap (c .Get (key ))
283286}
284287
285288// GetStringMapString returns the value associated with the key as a map of strings with default defaultConfiguration.
@@ -289,12 +292,12 @@ func GetStringMapString(key string) map[string]string {
289292
290293// GetStringMapString returns the value associated with the key as a map of strings.
291294func (c * Configuration ) GetStringMapString (key string ) map [string ]string {
292- return xcast2 .ToStringMapString (c .Get (key ))
295+ return xcast .ToStringMapString (c .Get (key ))
293296}
294297
295298// GetSliceStringMap returns the value associated with the slice of maps.
296299func (c * Configuration ) GetSliceStringMap (key string ) []map [string ]interface {} {
297- return xcast2 .ToSliceStringMap (c .Get (key ))
300+ return xcast .ToSliceStringMap (c .Get (key ))
298301}
299302
300303// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings with default defaultConfiguration.
@@ -304,7 +307,7 @@ func GetStringMapStringSlice(key string) map[string][]string {
304307
305308// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
306309func (c * Configuration ) GetStringMapStringSlice (key string ) map [string ][]string {
307- return xcast2 .ToStringMapStringSlice (c .Get (key ))
310+ return xcast .ToStringMapStringSlice (c .Get (key ))
308311}
309312
310313// UnmarshalWithExpect unmarshal key, returns expect if failed
@@ -380,7 +383,7 @@ func lookup(prefix string, target map[string]interface{}, data map[string]interf
380383 if prefix == "" {
381384 pp = k
382385 }
383- if dd , err := xcast2 .ToStringMapE (v ); err == nil {
386+ if dd , err := xcast .ToStringMapE (v ); err == nil {
384387 lookup (pp , dd , data , sep )
385388 } else {
386389 data [pp ] = v
0 commit comments