parseStringSliceAny coerces a raw any value into a []string. It accepts a []string (returned as-is), []any (string elements extracted), or nil (returns nil). Non-string elements inside a []any are skipped. The log parameter is optional; pass nil to suppress debug output about skipped items. Bare st
(raw any, debugLog *logger.Logger)
| 68 | // if s, ok := raw.(string); ok { return []string{s} } |
| 69 | // return parseStringSliceAny(raw, debugLog) |
| 70 | func parseStringSliceAny(raw any, debugLog *logger.Logger) []string { |
| 71 | if raw == nil { |
| 72 | return nil |
| 73 | } |
| 74 | switch v := raw.(type) { |
| 75 | case []string: |
| 76 | // Already the right type — return directly without copying. |
| 77 | return v |
| 78 | case []any: |
| 79 | result := make([]string, 0, len(v)) |
| 80 | skipped := 0 |
| 81 | for _, item := range v { |
| 82 | if s, ok := item.(string); ok { |
| 83 | result = append(result, s) |
| 84 | } else { |
| 85 | skipped++ |
| 86 | if debugLog != nil { |
| 87 | debugLog.Printf("parseStringSliceAny: skipping non-string item: %T", item) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | if skipped > 0 && debugLog == nil { |
| 92 | parseHelpersLog.Printf("parseStringSliceAny: skipped %d non-string item(s) from []any of length %d", skipped, len(v)) |
| 93 | } |
| 94 | return result |
| 95 | default: |
| 96 | if debugLog != nil { |
| 97 | debugLog.Printf("parseStringSliceAny: unexpected type %T, ignoring", raw) |
| 98 | } else { |
| 99 | parseHelpersLog.Printf("parseStringSliceAny: unexpected type %T, returning nil", raw) |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | } |