@@ -62,28 +62,49 @@ func printJSON(response map[string]interface{}) {
6262 enc .Encode (response )
6363}
6464
65+ func getItemsFromValue (v interface {}) ([]interface {}, bool ) {
66+ valueType := reflect .TypeOf (v )
67+ if valueType .Kind () == reflect .Slice {
68+ sliceItems , ok := v .([]interface {})
69+ if ! ok {
70+ return nil , false
71+ }
72+ return sliceItems , true
73+ } else if valueType .Kind () == reflect .Map {
74+ mapItem , ok := v .(map [string ]interface {})
75+ if ! ok {
76+ return nil , false
77+ }
78+ return []interface {}{mapItem }, true
79+ }
80+ return nil , false
81+ }
82+
6583func printText (response map [string ]interface {}) {
6684 format := "text"
6785 for k , v := range response {
6886 valueType := reflect .TypeOf (v )
69- if valueType .Kind () == reflect .Slice {
70- fmt .Printf ("%v:\n " , k )
71- for idx , item := range v .([]interface {}) {
72- if idx > 0 {
73- fmt .Println ("================================================================================" )
74- }
75- row , isMap := item .(map [string ]interface {})
76- if isMap {
77- for field , value := range row {
78- fmt .Printf ("%s = %v\n " , field , jsonify (value , format ))
87+ if valueType .Kind () == reflect .Slice || valueType .Kind () == reflect .Map {
88+ items , ok := getItemsFromValue (v )
89+ if ok {
90+ fmt .Printf ("%v:\n " , k )
91+ for idx , item := range items {
92+ if idx > 0 {
93+ fmt .Println ("================================================================================" )
94+ }
95+ row , isMap := item .(map [string ]interface {})
96+ if isMap {
97+ for field , value := range row {
98+ fmt .Printf ("%s = %v\n " , field , jsonify (value , format ))
99+ }
100+ } else {
101+ fmt .Printf ("%v\n " , item )
79102 }
80- } else {
81- fmt .Printf ("%v\n " , item )
82103 }
104+ return
83105 }
84- } else {
85- fmt .Printf ("%v = %v\n " , k , jsonify (v , format ))
86106 }
107+ fmt .Printf ("%v = %v\n " , k , jsonify (v , format ))
87108 }
88109}
89110
@@ -92,8 +113,8 @@ func printTable(response map[string]interface{}, filter []string) {
92113 table := tablewriter .NewWriter (os .Stdout )
93114 for k , v := range response {
94115 valueType := reflect .TypeOf (v )
95- if valueType .Kind () == reflect .Slice {
96- items , ok := v .([] interface {} )
116+ if valueType .Kind () == reflect .Slice || valueType . Kind () == reflect . Map {
117+ items , ok := getItemsFromValue ( v )
97118 if ! ok {
98119 continue
99120 }
@@ -134,7 +155,7 @@ func printColumn(response map[string]interface{}, filter []string) {
134155 for _ , v := range response {
135156 valueType := reflect .TypeOf (v )
136157 if valueType .Kind () == reflect .Slice || valueType .Kind () == reflect .Map {
137- items , ok := v .([] interface {} )
158+ items , ok := getItemsFromValue ( v )
138159 if ! ok {
139160 continue
140161 }
@@ -173,7 +194,7 @@ func printCsv(response map[string]interface{}, filter []string) {
173194 for _ , v := range response {
174195 valueType := reflect .TypeOf (v )
175196 if valueType .Kind () == reflect .Slice || valueType .Kind () == reflect .Map {
176- items , ok := v .([] interface {} )
197+ items , ok := getItemsFromValue ( v )
177198 if ! ok {
178199 continue
179200 }
@@ -207,7 +228,7 @@ func printCsv(response map[string]interface{}, filter []string) {
207228}
208229
209230func filterResponse (response map [string ]interface {}, filter []string , excludeFilter []string , outputType string ) map [string ]interface {} {
210- if ( filter == nil || len (filter ) == 0 ) && ( excludeFilter == nil || len (excludeFilter ) == 0 ) {
231+ if len (filter ) == 0 && len (excludeFilter ) == 0 {
211232 return response
212233 }
213234
@@ -224,8 +245,12 @@ func filterResponse(response map[string]interface{}, filter []string, excludeFil
224245 filteredResponse := make (map [string ]interface {})
225246
226247 for key , value := range response {
227- switch items := value .(type ) {
228- case []interface {}:
248+ valueType := reflect .TypeOf (value )
249+ if valueType .Kind () == reflect .Slice || valueType .Kind () == reflect .Map {
250+ items , ok := getItemsFromValue (value )
251+ if ! ok {
252+ continue
253+ }
229254 var filteredRows []interface {}
230255 for _ , item := range items {
231256 row , ok := item .(map [string ]interface {})
@@ -256,8 +281,7 @@ func filterResponse(response map[string]interface{}, filter []string, excludeFil
256281 filteredRows = append (filteredRows , filteredRow )
257282 }
258283 filteredResponse [key ] = filteredRows
259-
260- default :
284+ } else {
261285 filteredResponse [key ] = value
262286 }
263287 }
0 commit comments