@@ -167,12 +167,26 @@ func (c *Config) GetStringSliceValueImmediately(key string, defaultValue []strin
167167 return defaultValue
168168 }
169169
170- v , ok := value .([]string )
171- if ! ok {
172- log .Debugf ("convert to []string fail ! source type:%T" , value )
173- return defaultValue
170+ // 尝试直接转换为 []string
171+ if v , ok := value .([]string ); ok {
172+ return v
174173 }
175- return v
174+
175+ // 尝试从 []interface{} 转换为 []string (YAML 数组解析后的类型)
176+ if ifaceSlice , ok := value .([]interface {}); ok {
177+ result := make ([]string , 0 , len (ifaceSlice ))
178+ for _ , item := range ifaceSlice {
179+ if s , ok := item .(string ); ok {
180+ result = append (result , s )
181+ } else {
182+ result = append (result , fmt .Sprintf ("%v" , item ))
183+ }
184+ }
185+ return result
186+ }
187+
188+ log .Debugf ("convert to []string fail ! source type:%T" , value )
189+ return defaultValue
176190}
177191
178192// GetIntSliceValueImmediately 获取配置值([]int),立即返回,初始化未完成直接返回错误
@@ -182,12 +196,43 @@ func (c *Config) GetIntSliceValueImmediately(key string, defaultValue []int) []i
182196 return defaultValue
183197 }
184198
185- v , ok := value .([]int )
186- if ! ok {
187- log .Debugf ("convert to []int fail ! source type:%T" , value )
188- return defaultValue
199+ // 尝试直接转换为 []int
200+ if v , ok := value .([]int ); ok {
201+ return v
189202 }
190- return v
203+
204+ // 尝试从 []interface{} 转换为 []int (YAML 数组解析后的类型)
205+ if ifaceSlice , ok := value .([]interface {}); ok {
206+ result := make ([]int , 0 , len (ifaceSlice ))
207+ for _ , item := range ifaceSlice {
208+ switch v := item .(type ) {
209+ case int :
210+ result = append (result , v )
211+ case float64 :
212+ // JSON/YAML 数字默认解析为 float64
213+ // 验证值是否为整数(无小数部分)
214+ if v != float64 (int (v )) {
215+ log .Debugf ("convert to []int fail! float64 value has fractional part: %v" , v )
216+ return defaultValue
217+ }
218+ result = append (result , int (v ))
219+ case string :
220+ if i , err := strconv .Atoi (v ); err == nil {
221+ result = append (result , i )
222+ } else {
223+ log .Debugf ("convert to []int fail! value:%s, source type:%T" , v , v )
224+ return defaultValue
225+ }
226+ default :
227+ log .Debugf ("convert to []int fail! value:%v, source type:%T" , item , item )
228+ return defaultValue
229+ }
230+ }
231+ return result
232+ }
233+
234+ log .Debugf ("convert to []int fail ! source type:%T" , value )
235+ return defaultValue
191236}
192237
193238// GetSliceValueImmediately 获取配置值([]interface),立即返回,初始化未完成直接返回错误
@@ -318,16 +363,31 @@ func (c *Config) GetStringSliceValue(key, separator string, defaultValue []strin
318363 return defaultValue
319364 }
320365
321- v , ok := value .([]string )
322- if ! ok {
323- s , ok := value .(string )
324- if ! ok {
325- log .Debugf ("convert to []string fail ! source type:%T" , value )
326- return defaultValue
366+ // 尝试直接转换为 []string
367+ if v , ok := value .([]string ); ok {
368+ return v
369+ }
370+
371+ // 尝试从 []interface{} 转换为 []string (YAML 数组解析后的类型)
372+ if ifaceSlice , ok := value .([]interface {}); ok {
373+ result := make ([]string , 0 , len (ifaceSlice ))
374+ for _ , item := range ifaceSlice {
375+ if s , ok := item .(string ); ok {
376+ result = append (result , s )
377+ } else {
378+ result = append (result , fmt .Sprintf ("%v" , item ))
379+ }
327380 }
381+ return result
382+ }
383+
384+ // 尝试从字符串分割
385+ if s , ok := value .(string ); ok {
328386 return strings .Split (s , separator )
329387 }
330- return v
388+
389+ log .Debugf ("convert to []string fail ! source type:%T" , value )
390+ return defaultValue
331391}
332392
333393// GetIntSliceValue 获取配置值([]int)
@@ -337,23 +397,56 @@ func (c *Config) GetIntSliceValue(key, separator string, defaultValue []int) []i
337397 return defaultValue
338398 }
339399
340- v , ok := value .([]int )
341- if ! ok {
342- sl := c .GetStringSliceValue (key , separator , nil )
343- if sl == nil {
344- return defaultValue
345- }
346- v = make ([]int , 0 , len (sl ))
347- for index := range sl {
348- i , err := strconv .Atoi (sl [index ])
349- if err != nil {
350- log .Debugf ("convert to []int fail! value:%s, source type:%T" , sl [index ], sl [index ])
400+ // 尝试直接转换为 []int
401+ if v , ok := value .([]int ); ok {
402+ return v
403+ }
404+
405+ // 尝试从 []interface{} 转换为 []int (YAML 数组解析后的类型)
406+ if ifaceSlice , ok := value .([]interface {}); ok {
407+ result := make ([]int , 0 , len (ifaceSlice ))
408+ for _ , item := range ifaceSlice {
409+ switch v := item .(type ) {
410+ case int :
411+ result = append (result , v )
412+ case float64 :
413+ // JSON/YAML 数字默认解析为 float64
414+ // 验证值是否为整数(无小数部分)
415+ if v != float64 (int (v )) {
416+ log .Debugf ("convert to []int fail! float64 value has fractional part: %v" , v )
417+ return defaultValue
418+ }
419+ result = append (result , int (v ))
420+ case string :
421+ if i , err := strconv .Atoi (v ); err == nil {
422+ result = append (result , i )
423+ } else {
424+ log .Debugf ("convert to []int fail! value:%s, source type:%T" , v , v )
425+ return defaultValue
426+ }
427+ default :
428+ log .Debugf ("convert to []int fail! value:%v, source type:%T" , item , item )
351429 return defaultValue
352430 }
353- v = append (v , i )
354431 }
432+ return result
355433 }
356- return v
434+
435+ // 尝试从字符串分割后转换
436+ sl := c .GetStringSliceValue (key , separator , nil )
437+ if sl == nil {
438+ return defaultValue
439+ }
440+ result := make ([]int , 0 , len (sl ))
441+ for index := range sl {
442+ i , err := strconv .Atoi (sl [index ])
443+ if err != nil {
444+ log .Debugf ("convert to []int fail! value:%s, source type:%T" , sl [index ], sl [index ])
445+ return defaultValue
446+ }
447+ result = append (result , i )
448+ }
449+ return result
357450}
358451
359452// GetSliceValue 获取配置值([]interface)
0 commit comments