@@ -206,51 +206,72 @@ func printCsv(response map[string]interface{}, filter []string) {
206206 enc .Flush ()
207207}
208208
209- func filterResponse (response map [string ]interface {}, filter []string , outputType string ) map [string ]interface {} {
210- if filter == nil || len (filter ) == 0 {
209+ func filterResponse (response map [string ]interface {}, filter []string , excludeFilter []string , outputType string ) map [string ]interface {} {
210+ // If no filtering is required, return the original response.
211+ if len (filter ) == 0 && len (excludeFilter ) == 0 {
211212 return response
212213 }
214+
215+ // Create a set for excludeFilter to improve lookup performance.
216+ excludeSet := make (map [string ]struct {}, len (excludeFilter ))
217+ for _ , key := range excludeFilter {
218+ excludeSet [key ] = struct {}{}
219+ }
220+
221+ // Create a set for filter keys for fast lookup.
222+ filterSet := make (map [string ]struct {}, len (filter ))
223+ for _ , key := range filter {
224+ filterSet [key ] = struct {}{}
225+ }
226+
213227 filteredResponse := make (map [string ]interface {})
214- for k , v := range response {
215- valueType := reflect .TypeOf (v )
216- if valueType .Kind () == reflect .Slice || valueType .Kind () == reflect .Map {
217- items , ok := v .([]interface {})
218- if ! ok {
219- continue
220- }
228+
229+ for key , value := range response {
230+ // Process only slice/map values
231+ switch items := value .(type ) {
232+ case []interface {}:
221233 var filteredRows []interface {}
222234 for _ , item := range items {
223235 row , ok := item .(map [string ]interface {})
224- if ! ok || len (row ) < 1 {
236+ if ! ok || len (row ) == 0 {
225237 continue
226238 }
239+
227240 filteredRow := make (map [string ]interface {})
228- for _ , filterKey := range filter {
229- for field := range row {
230- if filterKey == field {
231- filteredRow [field ] = row [field ]
241+
242+ if len (filter ) > 0 {
243+ // Include only keys that exist in filterSet
244+ for filterKey := range filterSet {
245+ if val , exists := row [filterKey ]; exists {
246+ filteredRow [filterKey ] = val
247+ } else if outputType == config .COLUMN || outputType == config .CSV || outputType == config .TABLE {
248+ filteredRow [filterKey ] = "" // Ensure all filter keys exist in row
232249 }
233250 }
234- if outputType == config .COLUMN || outputType == config .CSV || outputType == config .TABLE {
235- if _ , ok := filteredRow [filterKey ]; ! ok {
236- filteredRow [filterKey ] = ""
251+ } else {
252+ // Exclude keys from excludeFilter
253+ for field , val := range row {
254+ if _ , excluded := excludeSet [field ]; ! excluded {
255+ filteredRow [field ] = val
237256 }
238257 }
239258 }
259+
240260 filteredRows = append (filteredRows , filteredRow )
241261 }
242- filteredResponse [k ] = filteredRows
243- } else {
244- filteredResponse [k ] = v
245- continue
246- }
262+ filteredResponse [key ] = filteredRows
247263
264+ default :
265+ // If it's not a slice, copy as is
266+ filteredResponse [key ] = value
267+ }
248268 }
269+
249270 return filteredResponse
250271}
251272
252- func printResult (outputType string , response map [string ]interface {}, filter []string ) {
253- response = filterResponse (response , filter , outputType )
273+ func printResult (outputType string , response map [string ]interface {}, filter []string , excludeFilter [] string ) {
274+ response = filterResponse (response , filter , excludeFilter , outputType )
254275 switch outputType {
255276 case config .JSON :
256277 printJSON (response )
0 commit comments